#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9; // set the pin for the servo
int button_01 = 2;
int button_02 = 3;
int button_03 = 13;
int i = 0;
int servoAngles[10] = {10, 90, 30, 120, 60, 150, 90, 180, 100, 20}; // initialize array of servo angles
bool state_1 = 0;
bool state_2 = 0;
bool state_3 = 0;
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(button_01, INPUT_PULLUP); // enable internal pull-up resistors for the buttons
pinMode(button_02, INPUT_PULLUP);
pinMode(button_03, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
// code for button 1
if (digitalRead(button_01) == LOW && state_1 == 0) { // check if button 1 is pressed and not already processed
state_1 = 1; // set state to 1 to indicate button press is processed
delay(50); // Add a short delay to prevent multiple executions
if (i < 9) {
i++; // increment index in array
} else {
i = 0; // reset index to 0 if it reaches the end of the array
}
Serial.print("Selected angle: ");
Serial.println(servoAngles[i]);
delay(100); // Add a delay to prevent multiple executions
state_1 = 0; // set state back to 0 to indicate processing is completed
}
// code for button 2
if (digitalRead(button_02) == LOW && state_2 == 0) { // check if button 2 is pressed and not already processed
state_2 = 1; // set state to 1 to indicate button press is processed
delay(50); // Add a short delay to prevent multiple executions
if (i > 0) {
i--; // decrement index in array
} else {
i = 9; // set index to the end of the array if it reaches 0
}
Serial.print("Selected angle: ");
Serial.println(servoAngles[i]);
delay(100); // Add a delay to prevent multiple executions
state_2 = 0; // set state back to 0 to indicate processing is completed
}
// code for button 3
if (digitalRead(button_03) == LOW && state_3 == 0) { // check if button 3 is pressed and not already processed
state_3 = 1; // set state to 1 to indicate button press is processed
myservo.write(servoAngles[i]);
Serial.print("Set angle: ");
Serial.println(servoAngles[i]);
delay(100); // Add a delay to prevent multiple executions
state_3 = 0; // set state back to 0 to indicate processing is completed
}
}