I figured I's make a Simon Says just because why not. But It isn't working and I don't know why. Here is a schematic: https://123d.circuits.io/circuits/1290892-simon-sucks
Do note that in the schematic, there are four switches. That is because the sim would pause when reading them I guess to keep from being an infinite loop.
Here is my code:
/*const byte ledRed = 2;
const byte ledGrn = 3;
const byte ledBlu = 4;
const byte ledRng = 5;*/
const byte buttonRed = 6;
const byte buttonGrn = 7;
const byte buttonBlu = 8;
const byte buttonRng = 9;
byte pattern[35]; //pattern
byte button[35]; //correct button order
byte location; //this is for the pattern playback
byte counter; //this is for our button presses
unsigned long then;
unsigned long wait;
void setup() {
for (byte i = 2; i < 6; i++) {
pinMode(i, OUTPUT); digitalWrite(i,0);
}
for (byte i = 6; i < 10; i++) {
pinMode(i, INPUT_PULLUP);
}
clearPattern();
}
void loop() {
boolean lose = 0;
//make sure there's something there
if (pattern[0] == 0) {
generate();
}
//cycle through the pattern
while (pattern[counter] != 0) {
digitalWrite(pattern[counter], 1);
then = millis();
while (millis() - then < 250) {
//do nothing)
}
digitalWrite(pattern[counter], 0);
counter++;
}
//buttonary input
counter = 0;
while (button[counter] != 0 && lose == 0) { //we are going to do the following code once for each step in the pattern. it will also stop if the wrong button is pressed
while (digitalRead(buttonRed) & digitalRead(buttonGrn) & digitalRead(buttonBlu) & digitalRead(buttonRng) == 1) {
//do nothing until a button is pressed
}
while (millis() - wait < 1) {
//debouncing
}
if (digitalRead(buttonRed) | digitalRead(buttonGrn) | digitalRead(buttonBlu) | digitalRead(buttonRng) == 0) { //makes sure a button was pressed
if (digitalRead(button[counter]) != 0) { //if it was not the correct button
lose = 1; //will stop the search
clearPattern(); //clear current pattern
}
else { //but if it was the correct one
counter++;
generate();
}
while(digitalRead(buttonRed) | digitalRead(buttonGrn) | digitalRead(buttonBlu) | digitalRead(buttonRng) == 0){
//wait
}
}
}
}
void clearPattern() {
memset(pattern, 0, sizeof(pattern));
memset(button, 0, sizeof(button));
counter = 0;
}
void generate() {
byte number1;
byte number2;
number1 = random(2, 5);
number2 = number1+4;
pattern[counter] = number1;
button[counter] = number2;
}
The 123D Emulator was doing a weird thing that the random number part of the code always 'generates' the same number, which migrates into the green LED lighting up first. I found that pressing the correct button counts as being wrong. Thinking that it was just the emulator being stupid, I made it for real... it behaves exactly the same way. So going back to the emulator, one of the arrays (pattern[])goes from being full of 0's to being completely blank.
I just have no idea what's going on. If I use the sim's debugger, I can pause the code at specific lines (82, 83, 84, 90, 94) to read the variable's values.
Mid-Post Edit
Both arrays act dumb. Actually, it's like the whole generate() function is wrong.