#include <Servo.h>
const int servoButtonPin = 2;
const int ledButtonPin = 3;
const int toneButtonPin = 4;
const int ledPin = 5;
const int servoPin1 = 6;
const int servoPin2 = 7;
int servoButtonState = 0;
int lastServoButtonState = 0;
int ledButtonState = 0;
int lastLedButtonState = 0;
int toneButtonState = 0;
int lastToneButtonState = 0;
Servo servo1;
Servo servo2;
bool ledOn = false;
void setup() {
pinMode(servoButtonPin, INPUT_PULLUP);
pinMode(ledButtonPin, INPUT_PULLUP);
pinMode(toneButtonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
servo1.attach(servoPin1);
servo2.attach(servoPin2);
}
void loop() {
servoButtonState = digitalRead(servoButtonPin);
if (servoButtonState != lastServoButtonState) {
if (servoButtonState == LOW) {
servo1.write(0);
servo2.write(180);
} else {
// If the servo button is released (HIGH state), move both servos back to their initial positions
servo1.write(90);
servo2.write(90);
}
delay(50);
}
lastServoButtonState = servoButtonState;
ledButtonState = digitalRead(ledButtonPin);
// Check if the LED button state has changed
if (ledButtonState != lastLedButtonState) {
if (ledButtonState == LOW) {
ledOn = !ledOn;
digitalWrite(ledPin, ledOn ? HIGH : LOW);
}
delay(50);
}
lastLedButtonState = ledButtonState;
// Read the state of the button for playing tone
toneButtonState = digitalRead(toneButtonPin);
if (toneButtonState != lastToneButtonState) {
if (toneButtonState == LOW) {
tone(8, 1000, 500); // Pin 8, frequency 1000Hz, duration 500ms
} else {
noTone(8);
}
delay(50);
}
lastToneButtonState = toneButtonState;
}