...

Assignment

This week, we are assigned to create a circuit and Arduino code that does the following:

Objectives

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

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

Circuit Design

Detail image

My Code

int button; 
int n=0;

void setup(){  

  pinMode (10, OUTPUT);
  pinMode (9, OUTPUT);
  pinMode (2, INPUT);
  Serial.begin(9600);

}  

void loop(){  

  button = digitalRead(2);

 if (button == HIGH){
  delay(50);
  n++;

  if (n == 1){
   digitalWrite(10, HIGH);
   digitalWrite(9, LOW);

  }else if (n == 2){
   digitalWrite(10, HIGH);
  digitalWrite(9,HIGH);

  }else if (n>2) {
   digitalWrite(10, LOW);
   digitalWrite(9, LOW);
   n=0;
 }

Serial.print("Press Count:");
Serial.println(n);

delay(200);
 }
}

Press and Press