#include <Servo.h>
Servo myServo; // Create a servo object
int button1State = 0; // Variable to store button 1 state
int button2State = 0; // Variable to store button 2 state
int button1Pin = 7; // Pin for button 1 (clockwise)
int button2Pin = 8; // Pin for button 2 (counterclockwise)
int servoPin = 3; // Pin for servo motor
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
myServo.attach(servoPin); // Attach the servo to the pin
}
void loop() {
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == LOW) { // Reversed logic when using pullup mode (internal resistor on Arduino NANO)
myServo.write(0);// Rotate servo clockwise
}
else if (button2State == LOW)
{
myServo.write(180);// Rotate servo counterclockwise
}
else
{
myServo.write(90);// Stop servo
}
delay(50); // Add a small delay to debounce buttons
}
Connect battery to
VIN/GND pins on Arduino.
Connect buttons to GND/Digital pins on Arduino.