...

Assignment

For my first assignment, I tried to establish a connection between two LED lights and a switch to the Arduino controller. The objective was to design a functional circuit where pressing the switch once would illuminate the first LED, a second press would activate the second LED while keeping the first one lit, and a third press of the switch would turn off both LEDs simultaneously. And it worked! First, I started by creating a counter variable that counted the times I pressed the switch and used the modulo that only counted up to 3, so that every time I clicked the switch for the fourth time, the cycle repeated. At the beginning, I had some difficulties because I didn’t know how to create a condition for the system to react only when the button’s value changed. I did some searching on the Physical Computing website (https://learn.newmedia.dog/tutorials/arduino-and-electronics/arduino/digital-io-input-changed/) and found a solution among the tutorials: I needed a variable to check the button’s value. After several trial and error attempts, I realized that it was necessary to add a condition that the button’s value should not be zero, as the command would still trigger even when I released the button.

Images

Some images of the breadboard circuit:

Detail image

Detail image

Code

Here’s the code I wrote:

int buttonState = 0;
int firstlight = 9;
int secondlight = 6;
int buttonChange = 0;
int buttonCounter = 0;


void setup(){  
  // Arduino setup code here to run once  
  pinMode(firstlight, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(secondlight, OUTPUT);
}  

void loop(){  
  // Arduino loops repeats as long as there is power  
  buttonState = digitalRead(2);

  if(buttonState != buttonChange && buttonState != 0)
  {
    buttonCounter = (buttonCounter + 1) % 3;
  }
  
  if(buttonCounter == 0)
  {
    digitalWrite(firstlight, LOW);
    digitalWrite(secondlight, LOW);
  }
  else if(buttonCounter == 1)
  {
    digitalWrite(firstlight, HIGH);
    digitalWrite(secondlight, LOW);
  }
  else if(buttonCounter == 2)
  {
    digitalWrite(firstlight, HIGH);
    digitalWrite(secondlight, HIGH);
  }
  buttonChange = buttonState;
  delay(10);

}

Video

This is the final result!