Turn ALL the lights on

I’ve created a mini game. The challenge here is to figure out which 2 of the 4 switches you have to press to turn all 4 lights one. It’s a simple guessing game, with not a lot of possibilities, but things could get a little more interesting by increasing the number of switches and introducing logical hints to the players.

The correct two switches are coded in, and here is the source code to the program being used for this game (if arrays were introduced here, it would have made the code more efficient and short):

void setup() {
  //green light
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  //red light
  pinMode(4, INPUT);
  pinMode(5, OUTPUT);
  //blue light
  pinMode(6, INPUT);
  pinMode(7, OUTPUT);
  //yellow light
  pinMode(8, INPUT);
  pinMode(9, OUTPUT);
}

void loop() {
  if(digitalRead(2) == HIGH && digitalRead(4) == HIGH){
    digitalWrite(7, HIGH);
  }

  if(digitalRead (4) == HIGH && digitalRead(6) == HIGH){
    digitalWrite(3, HIGH);
    digitalWrite(9, HIGH);
  }
  if(digitalRead (6) == HIGH && digitalRead(8) == HIGH){
    digitalWrite(3, HIGH);
  }
  if(digitalRead (2) == HIGH && digitalRead(8) == HIGH){
    digitalWrite(5, HIGH);
  }
  if (digitalRead(2) == HIGH){
    digitalWrite(3, HIGH);
  }
  if (digitalRead(4) == HIGH){
    digitalWrite(5, HIGH);
  }
  if (digitalRead(6) == HIGH){
    digitalWrite(7, HIGH);
  }
  if (digitalRead(8) == HIGH){
    digitalWrite(9, HIGH);
  }
  if (digitalRead(2) == LOW){
    digitalWrite(3, LOW);
  }
  if (digitalRead(4) == LOW){
    digitalWrite(5, LOW);
  }
  if (digitalRead(6) == LOW){
    digitalWrite(7, LOW);
  }
  if (digitalRead(8) == LOW){
    digitalWrite(9, LOW);
  }
}

And here is a diagram of how the connections are made:

logical_switch

 

If you press the two middle switches, you can turn all the lights on:

20150914_004040

 

Leave a Reply