Header Image

Jak vyrobit (téměř) cokoliv

Week 7

Weekly tasks

  1. Measure the power consumption of an output device (e.g. LED, motor)
  2. Calibrate the sensor (show the conversion relationship between the measured and indicated value in a graph or table).
  3. Connect an output device that you have not used yet
  4. Design a program that connects at least one input and one output device

Used tools

Arduino nano, Arduino IDE, Servo

The work

This week I tried to get to know arduino better and connect Servo to it. Servo was chosed mainly because in my final project I want to use it and to know how it works wont hurt. In my final project the Servo would work when touch sensor is activated. But now I didn't have it so I did simple button.

Arduino IDE script

As before I found some examples of scripts for Servo and modified it a bit. In this case also library Servo.h was used (it is in Arduino IDE natively)


    #include 

        Servo myServo;  // Create a Servo object
        const int buttonPin = 2;  // Button connected to pin D2
        int lastButtonState = HIGH;  
        bool servoPosition = false;  
        void setup() {
            myServo.attach(9);  // Servo connected to pin D9
            pinMode(buttonPin, INPUT_PULLUP);  
            myServo.write(0);  // Start at 0 degrees
        }
        
        void loop() {
            int buttonState = digitalRead(buttonPin);
            
            if (buttonState == LOW && lastButtonState == HIGH) {  // Button pressed
                servoPosition = !servoPosition;  // Toggle position
                myServo.write(servoPosition ? 180 : 0);  // Move servo
                delay(300);  // Debounce delay
            }
        
            lastButtonState = buttonState;  // Update last button state
        }
      
    

Final result

The final process
Scheme of wiring