Did I say 50 seconds? Haha, I meant 50 milliseconds. And 13 is set to HIGH. I soldered it to pin 13 not knowing how it would function, so I just leave 13 set to HIGH. I wasn't going to post all of my code to avoid revealing my mess to the world, but here it is. A lot may redundant/sloppy; I've been completely focused on button behavior.
#include <RGBLEDs.h> //My library for controlling the 4 LEDs
#include <Bounce.h>
#include <TrueRandom.h>
int level = 0;
int guess; //Button press
bool correct = true; //Tracks progress
int leds[] = {2,3,4,5}; //Contains LED pin locations
int ledColors[] = {9,10,11,12}; //LED color values
RGBLEDs matrix(11,10,9,leds); //Creates a RGB LED controller
//Pushbuttons
Bounce button1 = Bounce(6,50);
Bounce button2 = Bounce(7,50);
Bounce button3 = Bounce(8,50);
Bounce button4 = Bounce(12,50);
Bounce buttons[] = {button1,button2,button3,button4}; //Bounce objects
int buttonIndexes[] = {6,7,8,12}; //Button pins
int pattern[10]; //Contains the light sequence
/**Sets pin modes and initializes sequence arrray */
void setup()
{
Serial.begin(9600);
//Button inputs
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(12,INPUT);
pinMode(13,OUTPUT); //Common button pin
digitalWrite(13,HIGH);
for (int i=0; i < 10; i++)
pattern[i] = TrueRandom.random(0,4); //Creates random sequence
for (int i=0; i<2; i++) //Opening sequence
{
matrix.displayColors(500,9,10,11,12);
matrix.displayColors(70,9,10,11,12);
matrix.displayColors(70,10,11,12,9);
matrix.displayColors(70,11,12,9,10);
matrix.displayColors(70,12,9,10,11);
}
delay(500);
}
/*Plays sequence, awaits input, and repeats */
void loop()
{
displaySequence(level);
if (acceptInput(level))
level++;
else
correct = true;
}
/*Sequentially lights up each LED in the sequence */
void displaySequence(int currentLevel)
{
for (int i = 0; i <= currentLevel; i++)
{
delay(100);
matrix.lightButton(pattern[i]);
}
}
/*Accepts user input with the pushbuttons */
bool acceptInput(int currentLevel)
{
//Recursively calls the method as per the level
if (currentLevel > 0)
acceptInput(currentLevel-1);
if (correct)
{
guess = -1;
//Waits until a button is pressed
while (guess < 0)
{
for (int i=0; i<4; i++)
if (buttons[i].update() && buttons[i].read()==HIGH)
guess = i;
}
Serial.println(guess);
//If the guess is wrong, the game ends
if (guess != pattern[currentLevel])
return gameOver();
else
{
matrix.lightButton(guess);
return true;
}
delay(100);
}
}
/*Plays a sequence signifying game over */
bool gameOver()
{
for (int i=0; i<1; i++)
{
matrix.displayColors(500,9,10,11,12);
matrix.displayColors(70,9,10,11,12);
matrix.displayColors(70,10,11,12,9);
matrix.displayColors(70,11,12,9,10);
matrix.displayColors(70,12,9,10,11);
}
level = 0;
correct = false;
return false;
}
UPDATE: Okay, I wired the common button pin to VCC since the onboard resistor might have been interfering. It works great, but you have to hold down the button for a second or so for it to register.