#include <Servo.h>
Servo myservo; // Create a Servo object
int t1 = 0;
int t2 = 0;
int k1 = 2; // Input pin 1
int k2 = 3; // Input pin 2
int pos = 90; // Start at 90 degrees (mid position)
int val = 2; // Step size for the servo movement
void setup() {
pinMode(k1, INPUT); // Set k1 as input
pinMode(k2, INPUT); // Set k2 as input
myservo.attach(9); // Attach the servo to pin 9
myservo.write(pos); // Initialize the servo position
}
void loop() {
t1 = digitalRead(k1); // Read the state of button k1
t2 = digitalRead(k2); // Read the state of button k2
// If button k1 is pressed, move the servo forward
if (t1 == HIGH) {
pos = pos + val; // Increase the position by val
if (pos > 180) { // Ensure the position doesn't exceed 180 degrees
pos = 180;
}
myservo.write(pos); // Set the new servo position
delay(10); // Small delay to allow the servo to move
}
// If button k2 is pressed, move the servo backward
else if (t2 == HIGH) {
pos = pos - val; // Decrease the position by val
if (pos < 0) { // Ensure the position doesn't go below 0 degrees
pos = 0;
}
myservo.write(pos); // Set the new servo position
delay(10); // Small delay to allow the servo to move
}
}