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);
  Serial.begin(9600);
  pinMode(greenLed, OUTPUT);
  pinMode(greenButton, INPUT);
  Serial.begin(9600);
  pinMode(yellowLed, OUTPUT);
  pinMode(yellowButton, INPUT);
  Serial.begin(9600);
  pinMode(blueLed, OUTPUT);
  pinMode(blueButton, INPUT);
  Serial.begin(9600);
}


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

  int redState = digitalRead(redButton);
  Serial.println(redLed);
  digitalWrite(redLed, redState);

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

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


}

Leave a Reply