23 September 2019

It Doesn't Work

This project was a grand (mis)adventure. First, I attempted to create what is known as a "Lights Out" puzzle, where all lights are on in a grid and you need to turn them all off. Pushing a button/light turns that one off and causes a state change to those adjacent to it. So, if they are on they'll turn off and if they are already off they'll infuriatingly turn on. I thought, "Hey, I've always wanted to know how to do this, I'll give it a try! It will probably be a little challenging, but it's basically just coding state changes so I kinda know the objective!" Here's a basic sketch of what I had in mind:

This is basically how the game works - the rules are easy but success is difficult and usually involves a lot of guesswork........

After several attempts at wiring and fitting a 3x3 grid of LEDs and corresponding buttons on the board, I realized I had made a major oversight: how was I going to individually program 18 components with the 13 pins of an Arduino?? I didn't know how to get the Arduino to recognize individual LEDs on a string attached to one data pin, which was rather essential. Also, it was kind of a mess at this point anyway.

Feeling only a moderate amount of discouragement, I decided to try to make a different game: Simon Says. Series of colors lights up, push the corresponding buttons in order. I thought, "This will be so much easier! I can just use a few LEDs, create a sequence, then track when the correct buttons are pushed!" If you're wondering how I'm going to do that last part, you probably know what's going to happen later.

Here's a simple sketch of my simple idea that definitely wouldn't be too complicated to make:

Simple mockup of LED and button sequences for victory


Ultimately I ended up adding a "start" button that triggers the sequence and game. I did not add a "victory" light for... reasons (you'll see). I placed everything on the breadboard like so:

Arrangement on breadboard

Schematic

I wanted to keep the board clean looking and leave space for people to push the buttons and clearly see the lights (aaaand limit the mess I had made at this point with the accumulating pile of difficult-to-read resistors) so I used "INPUT_PULLUP" instead of putting resistors between the buttons and pins.

For the code, I set up a variable called "score" and used a series of "if/else" statements to try to track correct button pushes. If incorrect, all the lights would flash and the game would reset. The problem is, for some reason I have yet to understand, "score" was not registering, so any button push triggered the reset state. I Googled and experimented and tested and isolated code to make sure it did what I wanted, all to no avail.

In the end, my frustration led me to the final version of the game: you can try playing Simon Says, but the buttons don't do what you want. Enjoy!


Here's the code I ended up using, including the remnants of the original "score" code I couldn't get to work.


const int blueLED = 2;
const int redLED = 3;
const int greenLED = 4;
const int blueButton = 8;
const int redButton = 9;
const int greenButton = 10;
const int start = 12;
/* Variable to record "correct" button pushes
int score = 0; */
void setup() {
// put your setup code here, to run once:
pinMode(blueLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueButton, INPUT_PULLUP);
pinMode(redButton, INPUT_PULLUP);
pinMode(greenButton, INPUT_PULLUP);
pinMode(start, INPUT_PULLUP);
}
void loop() {
while (digitalRead(start) == HIGH) {
// do nothing
}
digitalWrite(blueLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(redLED, HIGH);
delay(200);
digitalWrite(redLED, LOW);
delay(200);
while (digitalRead(start) == HIGH) {
if ((digitalRead(greenButton) == LOW) && (digitalRead(redButton) == LOW)) {
digitalWrite(blueLED, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(redLED,LOW);
}
else if ((digitalRead(redButton) == LOW) && (digitalRead(greenButton) == HIGH)){
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
digitalWrite(blueLED, LOW);
}
else {
digitalWrite(blueLED, LOW);
}
if (digitalRead(blueButton) == LOW) {
digitalWrite(greenLED,HIGH);
}
else {
digitalWrite(greenLED, LOW);
}
//testing the same as the first "else if" as "if"
if ((digitalRead(greenButton) == LOW) && (digitalRead(redButton) == HIGH)) {
digitalWrite(redLED, HIGH);
}
else {
digitalWrite(redLED, LOW);
}
if ((digitalRead(blueButton) == LOW) && (digitalRead(redButton) == LOW) && (digitalRead(greenButton) == LOW)) {
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
return;
}
}
}
// original code I was trying to make work so it would progress with correct input and reset with incorrect
//if (digitalRead(blueButton) == LOW) {
//digitalWrite(blueLED, HIGH);
//score++;
//}
//else {
//digitalWrite(blueLED, LOW);
//}
//if (digitalRead(redButton) == LOW) {
//digitalWrite(redLED, HIGH);
//score++;
//}
//else if (digitalRead(redButton) == LOW && score != 3) {
//digitalWrite(blueLED, HIGH);
//digitalWrite(redLED, HIGH);
//digitalWrite(greenLED, HIGH);
//delay(200);
//digitalWrite(blueLED, LOW);
//digitalWrite(redLED, LOW);
//digitalWrite(greenLED, LOW);
//return;
//}
//else {
//digitalWrite(redLED, LOW);
// }
//if (digitalRead(greenButton) == LOW) {
//digitalWrite(greenLED, HIGH);
//score++;
//}
//else if (digitalRead(greenButton) == LOW) {
//digitalWrite(greenLED, HIGH);
//score++;
//}
//else if (digitalRead(greenButton) == LOW) {
//digitalWrite(blueLED, HIGH);
//digitalWrite(redLED, HIGH);
//digitalWrite(greenLED, HIGH);
//delay(200);
//digitalWrite(blueLED, LOW);
//digitalWrite(redLED, LOW);
//digitalWrite(greenLED, LOW);
//return;
//}
//else {
//digitalWrite(greenLED, LOW);
//}
view raw simonsays.ino hosted with ❤ by GitHub

No comments:

Post a Comment

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