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.
- 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…)
Circuit Design
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);
}
}