// Define the time duration for actions (1 second)
int timer = 200;
// Define pins for LEDs, buzzer, and buttons
int ledPin1 = 1;
int ledPin2 = 2;
int ledPin3 = 3;
const int buzzer = 4;
const int button1 = 8;
const int button2 = 9;
const int button3 = 10;
// Custom sound function
void playRoboticSound() {
// Play a custom sound using the buzzer
for (int i = 0; i < 3; i++) {
tone(buzzer, 200); // Generate a 200 Hz tone on the buzzer
delay(200); // Wait for 200 milliseconds
noTone(buzzer); // Turn off the buzzer
delay(200); // Wait for another 200 milliseconds
}
}
void setup() {
// Initialize pins as OUTPUT or INPUT_PULLUP
for (int thisPin = 1; thisPin < 5; thisPin++) {
pinMode(thisPin, OUTPUT); // Set LED pins as OUTPUT
pinMode(buzzer, OUTPUT); // Set the buzzer pin as OUTPUT
pinMode(button1, INPUT_PULLUP); // Set button1 pin as INPUT_PULLUP
pinMode(button2, INPUT_PULLUP); // Set button2 pin as INPUT_PULLUP
pinMode(button3, INPUT_PULLUP); // Set button3 pin as INPUT_PULLUP
}
}
void loop() {
// Check if the red button is pressed
int value1 = digitalRead(button1);
if (value1 == 0) { // If button1 is pressed (active LOW)
// Turn on LEDs one by one
for (int thisPin = 1; thisPin < 5; thisPin++) {
digitalWrite(thisPin, HIGH); // Turn on the LED
delay(timer); // Wait for the specified timer duration
if (thisPin == 4) {
playRoboticSound(); // Use the custom sound function
}
digitalWrite(thisPin, LOW); // Turn off the LED
}
// Turn off LEDs in reverse order
for (int thisPin = 4; thisPin >= 1; thisPin--) {
digitalWrite(thisPin, HIGH); // Turn on the LED
delay(timer); // Wait for the specified timer duration
digitalWrite(thisPin, LOW); // Turn off the LED
break; // Break out of the loop
}
}
// Check if the green button is pressed
int value2 = digitalRead(button2);
if (value2 == 0) { // If button2 is pressed (active LOW)
// Turn on LEDs one by one
for (int thisPin = 1; thisPin < 5; thisPin++) {
digitalWrite(thisPin, HIGH); // Turn on the LED
delay(timer); // Wait for the specified timer duration
if (thisPin == 4) {
playRoboticSound(); // Use the custom sound function
}
digitalWrite(thisPin, LOW); // Turn off the LED
}
// Turn off LEDs in reverse order
for (int thisPin = 4; thisPin >= 1; thisPin--) {
digitalWrite(thisPin, HIGH); // Turn on the LED
delay(timer); // Wait for the specified timer duration
digitalWrite(thisPin, LOW); // Turn off the LED
}
}
// Check if the Blue button is pressed
int value3 = digitalRead(button3);
if (value3 == 0) { // If button3 is pressed (active LOW)
digitalWrite(ledPin1, HIGH); // Turn on LED1
digitalWrite(ledPin2, HIGH); // Turn on LED2
delay(timer); // Wait for the specified timer duration
digitalWrite(ledPin1, LOW); // Turn off LED1
digitalWrite(ledPin2, LOW); // Turn off LED2
digitalWrite(ledPin3, HIGH); // Turn on LED3
playRoboticSound(); // Use the custom sound function
digitalWrite(ledPin3, LOW); // Turn off LED3
// Turn off the LED light 3
digitalWrite(ledPin3, LOW);
}
}