#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
int potPin1 = A0; // Potentiometer 1 analog input pin
int potPin2 = A1; // Potentiometer 2 analog input pin
int buttonPin2 = 2; // Button input pin for servo action
int buttonPin3 = 3; // Button input pin for servo reattachment
int servo3Pin = 11; // Servo 3 control pin
bool buttonState2 = false;
bool lastButtonState2 = false;
bool buttonState3 = false;
bool lastButtonState3 = false;
bool inputBlocked = false;
int buttonPressCount = 0;
void setup() {
servo1.attach(9); // Servo 1 control pin
servo2.attach(10); // Servo 2 control pin
servo3.attach(servo3Pin); // Servo 3 control pin
servo3.write(0);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
void loop() {
if (!inputBlocked) {
int angle1 = map(analogRead(potPin1), 0, 1023, 0, 180); // Read potentiometer 1 angle
int angle2 = map(analogRead(potPin2), 0, 1023, 0, 180); // Read potentiometer 2 angle
servo1.write(angle1); // Set servo 1 angle
servo2.write(angle2); // Set servo 2 angle
}
buttonState2 = digitalRead(buttonPin2); // Read the state of button D2
if (buttonState2 != lastButtonState2) {
if (buttonState2 == LOW) {
buttonPressCount++; // Increment button press count
if (buttonPressCount == 1) {
// Button D2 was pressed for the first time, move servo3 quickly from angle 0 to 180 and back to 0
for (int angle = 0; angle <= 180; angle++) {
servo3.write(angle);
delay(1);
}
for (int angle = 180; angle >= 0; angle--) {
servo3.write(angle);
delay(5);
}
inputBlocked = true; // Block input after the button is pressed
}
}
lastButtonState2 = buttonState2;
}
if (inputBlocked) {
// Set all blocked inputs to a fixed value (0 in this example)
analogWrite(potPin1, 0);
analogWrite(potPin2, 0);
buttonState3 = digitalRead(buttonPin3); // Read the state of button D3
if (buttonState3 != lastButtonState3) {
if (buttonState3 == LOW) {
// Button D3 was pressed, reattach all servos
servo1.attach(9);
servo2.attach(10);
servo3.attach(servo3Pin);
inputBlocked = false; // Unblock input
buttonPressCount = 0; // Reset button press count
}
}
lastButtonState3 = buttonState3;
}
}