The LED puzzle I am made is a sort of game where 3 different LED lights randomly turn on at different times and speed. There will come a point when all 3 lights are on at the same time, and this is when a player should press the button. If a player has pressed the button at this exact time of alignment, then all the lights will blink 7 times, signifying the win of the game.
I wanted to make this a game of speed and simplicity. I am personally very happy with what I have produced because it looks neat to me. One thing a player would have to be aware of is the pressing of the button because sometimes the button does not come in full contact with the board and so it leads to misses. Would love to learn to incorporate music to this game later in the future.
INITIAL CODE (didn’t work):
int ledPin = 2; int ledPin2 = 4; int buttonPin2 = 5; int ledPin3 = 6; int buttonState2 = 0; void setup() { // put your setup code here, to run once: pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin2, INPUT); pinMode(ledPin3, OUTPUT); } void loop() { // put your main code here, to run repeatedly: int buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH) { Serial.println("button"); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin, HIGH); } else { // turn LED off: while (buttonState2 != HIGH){ int n = 2*random(1,4); int m = 2*random(1,4); int p = 2*random(1,4); //Serial.begin(9600); //Serial.println(n); digitalWrite(n, HIGH); delay(random(1,500)); digitalWrite(m, HIGH); delay(random(1,500)); digitalWrite(p, HIGH); delay(random(1,500)); digitalWrite(n, LOW); delay(random(1,500)); digitalWrite(m, LOW); delay(random(1,500)); digitalWrite(p, LOW); delay(random(1,500)); if (buttonState2 == HIGH) { Serial.println("BUTTON"); break; } } digitalWrite(ledPin2, LOW); digitalWrite(ledPin, LOW); } delay(100); }
You can actually tell from the video below that the lights do turn on, but the buttons do not carry out any functionalities… Not good!
FINAL CODE:
I wanted to make my code as simple and straightforward as possible. Here is what it looks like.
int ledPin1 = 2; int ledPin2 = 4; int ledPin3 = 6; int buttonPin2 = 5; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(buttonPin2, INPUT); } void loop() { led1(); led2(); led3(); button(); } void led1() { if (digitalRead (ledPin1) == LOW) { delay(random(1, 300)); digitalWrite (ledPin1, random(0, 2)); } else { delay(random(1, 300)); digitalWrite (ledPin1, random(0, 2)); } } void led2() { if (digitalRead (ledPin2) == LOW) { delay(random(1, 300)); digitalWrite (ledPin2, random(0, 2)); } else { delay(random(1, 300)); digitalWrite (ledPin2, random(0, 2)); } } void led3() { if (digitalRead (ledPin3) == LOW) { delay(random(1, 300)); digitalWrite (ledPin3, random(0, 2)); } else { delay(random(1, 300)); digitalWrite (ledPin3, random(0, 2)); } } void button() { int buttonState2 = digitalRead(buttonPin2); if (buttonState2 == HIGH && digitalRead(ledPin1) == HIGH && digitalRead(ledPin2) == HIGH && digitalRead(ledPin3) == HIGH) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); delay(2000); int count = 0; while (count < 7){ digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); delay(20); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, LOW); delay(1000); count += 1; } } }
How does someone know when they get it? Is there any indication to the user? Using delay (like we talked about in class on monday), will interfere with getting accurate button press reads too. We’ll look at how to mitigate that in class today.
Thanks for the comment! Yes, once the user successfully presses the button when all 3 LED lights turn on, all LEDs will light up one time and start blinking for seven consecutive times. So it is a pretty clear indication to the user whether or not he got it right. I think the video also contains the part where this happens 🙂