Week 9 – LED Lights with Switches

Concept

For this assignment, I used two LEDs controlled by two switches. The idea is that when one switch is pressed, one of the lights turns on and the other one turns off. When the other button is pressed, the light that is on turns off, and the one controlled by that switch is turned on. When both the buttons are pressed at the same time, both lights blink.

Code

void setup() {
  pinMode(8, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

digitalWrite(13, LOW);
digitalWrite(8, LOW);

}

void loop() {

  int switchPosition = digitalRead(A2);
  int switchTwoPosition = digitalRead(A3);

  if (switchPosition == HIGH && switchTwoPosition == HIGH)
  {
    digitalWrite(13, HIGH); 
    digitalWrite(8, HIGH);
    delay(100);
    digitalWrite(13, LOW); 
    digitalWrite(8, LOW);
    delay(100);

  }
  else
  {
    if (switchPosition == HIGH) {
    digitalWrite(13, HIGH); 
    digitalWrite(8, LOW);

  }

  if (switchTwoPosition==HIGH)
  {  
    digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
    digitalWrite(13, LOW);
  }

  }
 
}

Video

Future Improvements

Later, I can add more layers to the lights when each button is separately pressed, for example, change the brightness of the lights or make them fade.

Leave a Reply