#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9;
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);
pinMode(button_01, INPUT); // set the mode for the buttons as INPUT
pinMode(button_02, INPUT);
pinMode(button_03, INPUT);
Serial.begin(9600);
}
void loop() {
// code for button 1
if (digitalRead(button_01) == HIGH && 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(500); // add a delay to prevent multiple executions
// set state back to 0 to indicate processing is completed
}
if (digitalRead(button_01) == LOW){
state_1 = 0;
}
// code for button 2
if (digitalRead(button_02) == HIGH && 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(500); // add a delay to prevent multiple executions
// set state back to 0 to indicate processing is completed
}
if (digitalRead(button_02) == LOW){
state_2 = 0;
}
// code for button 3
if (digitalRead(button_03) == HIGH && 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
Serial.print("Set angle: ");
myservo.write(servoAngles[i]); // write the selected angle to the servo
Serial.println(servoAngles[i]);
delay(500); // add a delay to prevent multiple executions
// set state back to 0 to indicate processing is completed
}
if (digitalRead(button_03) == LOW){
state_3 = 0;
}
}