int timer = 150; // The higher the number, the slower the timing.
int buzzerPin = 9; // Pin for the buzzer
int buttonPin = 10; // Pin for the button
int buttonState = 0; // Variable to store the button state
void setup() {
// Use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as OUTPUT
pinMode(buttonPin, INPUT); // Set button pin as INPUT
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed to activate the sequence
if (buttonState == HIGH) {
// Loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// Turn the pin on:
digitalWrite(thisPin, HIGH);
// Activate the buzzer
digitalWrite(buzzerPin, HIGH);
delay(timer);
// Turn the pin off:
digitalWrite(thisPin, LOW);
// Deactivate the buzzer
digitalWrite(buzzerPin, LOW);
}
// Loop from the highest pin to the lowest:
for (int thisPin = 7; thisPin >= 2; thisPin--) {
// Turn the pin on:
digitalWrite(thisPin, HIGH);
// Activate the buzzer
digitalWrite(buzzerPin, HIGH);
delay(timer);
// Turn the pin off:
digitalWrite(thisPin, LOW);
// Deactivate the buzzer
digitalWrite(buzzerPin, LOW);
}
}
}