volatile int count = 1; // Must be volatile to use with interrupts
const int button = 2; // Use pin 2 or 3 for interrupt
const int led[3] = {12, 4, 3};
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP); // Button with pull-up resistor
// Set LED pins to OUTPUT
for (int i = 0; i < 3; i++) {
pinMode(led[i], OUTPUT);
}
// Attach interrupt to button pin
attachInterrupt(digitalPinToInterrupt(button), counter, FALLING);
}
void loop() {
switch (count) {
case 1:
clockwise();
break;
case 2:
anticlockwise();
break;
case 3:
blink();
break;
case 4:
// Run all sequences in order
clockwise();
anticlockwise();
blink();
break;
default:
break;
}
}
void clockwise() {
Serial.println("Clockwise: " + String(count));
for (int i = 0; i < 3; i++) {
digitalWrite(led[i], HIGH);
delay(500);
digitalWrite(led[i], LOW);
delay(500);
}
}
void anticlockwise() {
Serial.println("Anticlockwise: " + String(count));
for (int i = 2; i >= 0; i--) {
digitalWrite(led[i], HIGH);
delay(500);
digitalWrite(led[i], LOW);
delay(500);
}
}
void blink() {
Serial.println("Blink: " + String(count));
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
digitalWrite(led[i], HIGH);
}
delay(500);
for (int i = 2; i >= 0; i--) {
digitalWrite(led[i], LOW);
}
delay(500);
}
}
// Interrupt Service Routine (ISR)
void counter() {
// Use the ISR to update count
count++;
if (count > 4) {
count = 1;
}
delay(50); // Small delay to avoid bouncing issues
}