Week 7: Adruino Buttons and LEDs

My first idea for this assignment was to create a sort of pattern game where the lights show a pattern, and you are required to mimic that pattern to unlock the fourth (blue light).  Unfortunately, I wasn’t entirely sure how to accomplish that, so decided to experiment with other ideas. Here is what my code does:

Red button: turns on each led one at a time, consecutively, then all off.

Blue button: turns all LEDs on at the same time, for 3 seconds then turns them off.

Green button: usually just turns green like a regular switch, but when pushed after the red button, will turn on both green and red

Yellow button: does a pattern where two lights are turned on at once.

This assignment was particularly interesting because when I faced an issue, I was unsure whether I should debug from the code or from the physical board. At some points, even though I was sure that everything in the circuit was working fine, my green button never turned off and I began considering a fault in the circuit. (ultimately it was my code). I also faced a lot of problems getting the initial circuit to work, because of the number of wires and resistors, and small breadboard. Eventually what worked for me was colour coordinating the wires to the colors (i used white for power, instead of red just this time so that I could colour coordinate).

int greenButton = 2;
int greenLed = 3;
int yellowButton = 4;
int yellowLed = 5;
int redButton = 6;
int redLed = 7;
int blueButton = 8;
int blueLed = 9;
int redState = LOW;
int redButtonState = LOW;

void setup() {
  // set pin modes
  pinMode(redLed, OUTPUT);
  pinMode(redButton, INPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(greenButton, INPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(yellowButton, INPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(blueButton, INPUT);

void loop() {
  //green button that lights green regularly, but if pushed after the red button, the red lights up too
  int greenState = digitalRead(greenButton);
  digitalWrite(greenLed, greenState);
  if (greenState == HIGH && redButtonState == HIGH){
    digitalWrite(redLed, greenState);
    redButtonState = LOW;

  int redState = digitalRead(redButton);
  digitalWrite(redLed, redState);

  //yellow makes a pattern appear- two leds at a time
  int yellowState = digitalRead(yellowButton);
  digitalWrite(yellowLed, yellowState);
  if (yellowState == HIGH){
    digitalWrite(blueLed, yellowState);
    digitalWrite(yellowLed, yellowState);
    digitalWrite(greenLed, yellowState);
    digitalWrite(redLed, yellowState);
  // turns all the leds at once, for 3 seconds
  int blueState = digitalRead(blueButton);
  digitalWrite(blueLed, blueState);
  if (blueState == HIGH ) {
    digitalWrite(redLed, blueState);
    digitalWrite(greenLed, blueState);
    digitalWrite(yellowLed, blueState);

  // turns each led one at a time with a one second delay
  if (redState == HIGH) {
    digitalWrite(blueLed, redState);
    digitalWrite(greenLed, redState);
    digitalWrite(yellowLed, redState);
    digitalWrite(redLed, redState);
    redButtonState = HIGH;


Leave a Reply