...

Assignment

Circuit

  1. Connect two LEDs to your Arduino using a breadboard
  2. Connect one switch to your Arduino using a breadboard

Code

  • Read a momentary switch being pressed
  • When the program starts, both LEDs are off
  • When the switch is pressed once, the first LED turns on
  • When the switch is pressed the second time, the second LED turns on (the first one should also still be on)
  • When the switch is pressed the third time, both LEDs turn off
  • Repeat this same cycle of LEDs turning on and off in sequence (off, one LED, two LEDs, off…)

Images

Detail image

Code

int i = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(2,INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(2) == 0){
    while (digitalRead(2)==0){
    delay(10);
    }
    i++;
  }
  if(i%2 == 1){
    digitalWrite(9,1);
    }
  if(i%2 == 0){
    digitalWrite(10,1);
  }
  if(i%3 == 0){
  digitalWrite(9,0);
  digitalWrite(10,0);
  i = 0;
  }  
}

Video

Just update the filename to be correct. Here is the result: