// Source: chapter_02.SpaceshipInterface.ino
// Description: This program controls a series of LEDs using one switch
// Programmed by: Douglas Ferreira / January 28, 2024
int switchState = 0;
void setup() {
// Initialize all the LED pins as outputs and the switch pin as an input
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
digitalWrite(3, LOW); // Ensure green LED is off
digitalWrite(4, LOW); // Ensure red LED is off
digitalWrite(5, LOW); // Ensure other red LED is off
}
void loop() {
// Read the state of the switch
switchState = digitalRead(2);
if (switchState == LOW) {
// The button is not pressed
digitalWrite(3, HIGH); // Turn on green LED
digitalWrite(4, LOW); // Turn off red LED
digitalWrite(5, LOW); // Turn off other red LED
} else {
// The button is pressed
digitalWrite(3, LOW); // Turn off green LED
digitalWrite(4, LOW); // Ensure this red LED is off before toggling
digitalWrite(5, HIGH); // Turn on other red LED
delay(250); // Wait for a quarter second
// Toggle the LEDs
digitalWrite(4, HIGH); // Turn on red LED
digitalWrite(5, LOW); // Turn off other red LED
delay(250); // Wait for a quarter second
}
}