23 September 2019

Multi Switch: Turn on the lights



For this multi meter I decided to create an interactive logic game. The main goal is to find a way to have all the lights turned on at the same time. Each button has a set of conditionals that turn some of the LEDs on or off. The 3 LEDs receive their power from the Arduino, which also receives the input from the buttons when they are pressed. Through exploration or logic, the user can find a way to solve the puzzle while pressing the buttons in a specific order. This game can be expanded with many more lights, making it even harder to solve each time.





const int yellowL = 8;
const int greenL = 9;
const int blueL = 10;
const int b1 = 5;
const int b2 = 6;
const int b3 = 7;
void setup() {
// put your setup code here, to run once:
pinMode(yellowL, OUTPUT);
pinMode(greenL, OUTPUT);
pinMode(blueL, OUTPUT);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
Serial.begin(9600);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
}
void loop() {
// read the button state - 0 is pushed 1 is off
int b1State = digitalRead(b1);
int b2State = digitalRead(b2);
int b3State = digitalRead(b3);
int yellowState = digitalRead(yellowL);
int greenState = digitalRead(greenL);
int blueState = digitalRead(blueL);
//Serial.println(b1State);
//Serial.println(b2State);
//Serial.println(b3State);
//What buttons will do
if(b1State == 0) {
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, LOW);
}
if(b2State == 0) {
digitalWrite(blueL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(yellowL, LOW);
}
if(b3State == 0) {
digitalWrite(greenL, HIGH);
}
//if blue light is on and b2 is pushed, blue light will turn off
if(blueState == 1 && b2State == 0) {
digitalWrite(blueL, LOW);
}
// Hit all 3 buttons at the same time to reset to all lights off
if(b1State == 0 && b2State == 0 && b3State == 0){
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
}
//When all lights are lit send Success message
if(yellowState == 1 && greenState == 1 && blueState == 1) {
Serial.println("Success!");
delay(300);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
delay(100);
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(blueL, HIGH);
delay(100);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
delay(100);
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(blueL, HIGH);
delay(100);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
delay(100);
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(blueL, HIGH);
delay(100);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
delay(100);
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(blueL, HIGH);
delay(100);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
delay(100);
digitalWrite(yellowL, HIGH);
digitalWrite(greenL, HIGH);
digitalWrite(blueL, HIGH);
delay(100);
digitalWrite(yellowL, LOW);
digitalWrite(greenL, LOW);
digitalWrite(blueL, LOW);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.