I have been trying to create a memory game (kind of like simon) on my arduino, using a 3x3 LED grid and a corresponding grid of switches. My problem is with my code. I have been able to create code to generate a random pattern of 5 LEDs for the player to copy, I have been able to create code to translate the player's inputs into LED flashes on the grid, and I'm (pretty sure) I have created code to remember the generated pattern and the player's inputs and compare them to check for a success or failure.
BUT
The program always only does one function or the other.
The sequence of events should be:
- program generates 5 random LED flashes, and remembers them
- waits for player input
- player presses 5 buttons, i.e lights 5 LEDs in an attempt to copy the pattern
- program checks generated pattern against user input
- program indicates if user is correct or not, (i.e. via a flash of all LEDs or something like that, or simply playing a new pattern. the specific behavior is not important)
The problem is step 2 never seems to happen; my code either just generates patterns, or if I try to rearrange things, just allows input.
I have never programmed before, and this is pretty much my first arduino program, so I'm quite the novice. Any help of any sort is greatly welcome!
(I also apologize for the ugliness of my code. Like I said, I'm new to this, and am not very good with formatting)
Code:
#include <Keypad.h> //use Keypad library
const byte rows = 3; //three rows
const byte cols = 3; //three columns
char keys[rows][cols] =
{
{'1','2','3' }, //touchpad grid
{'4','5','6' },
{'7','8','9' }
};
byte rowPins[rows] = {
2, 3, 4}; //the row pin of the touchpad
byte colPins[cols] = {
5, 6, 7}; // column pin of the touchpad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
int ledCol1 = 8; //identify pins
int ledCol2 = 9;
int ledCol3 = 10;
int ledRow1 = 11;
int ledRow2 = 12;
int ledRow3 = 13;
int memoryRow[5];
int memoryCol[5];
int pressedRow[5];
int pressedCol[5];
void setup()
{
Serial.begin(9600);
pinMode(ledCol1, OUTPUT); //define pin states,
pinMode(ledCol2, OUTPUT);
pinMode(ledCol3, OUTPUT);
pinMode(ledRow1, OUTPUT);
pinMode(ledRow2, OUTPUT);
pinMode(ledRow3, OUTPUT);
digitalWrite (ledCol1, LOW);
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, LOW);
memoryFunc();
for(int t=0; t<5; t++)
{
Serial.println(memoryCol[t]);
//Serial.println(memoryRow[t]);
}
for(int x=0; x<5; x++)
{
playerFunc(x);
}
}
///////////memory
void memoryFunc()
{
for(int i=0; i<5; i++)
{
memoryRow[i]=random(11, 14);
memoryCol[i]=random(8, 11);
lightMemory(i);
delay(500);
}
digitalWrite (ledCol1, LOW); //All Off
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
}
///////////mem inf
int lightMemory(int j)
{
if(memoryRow[j]==11 && memoryCol[j]==8)
{
digitalWrite (ledCol1, HIGH); //light LED 1
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==12 && memoryCol[j]==8)
{
digitalWrite (ledCol1, LOW); //light LED 2
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==13 && memoryCol[j]==8)
{
digitalWrite (ledCol1, LOW); //light LED 3
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==11 && memoryCol[j]==9)
{
digitalWrite (ledCol1, HIGH); //light LED 4
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==12 && memoryCol[j]==9)
{
digitalWrite (ledCol1, LOW); //light LED 5
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==13 && memoryCol[j]==9)
{
digitalWrite (ledCol1, LOW); //light LED 6
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
}
else if(memoryRow[j]==11 && memoryCol[j]==10)
{
digitalWrite (ledCol1, HIGH); //light LED 7
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
}
else if(memoryRow[j]==12 && memoryCol[j]==10)
{
digitalWrite (ledCol1, LOW); //light LED 8
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
}
else if(memoryRow[j]==13 && memoryCol[j]==10)
{
digitalWrite (ledCol1, LOW); //light LED 9
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
}
}
///////////input
void playerFunc(int z)
{
char key = keypad.getKey(); //keypad detection
Serial.println(key);
if (key == '1')
{
pressedRow[z]=11;
pressedCol[z]=8;
digitalWrite (ledCol1, HIGH); //light LED 1
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '2')
{
pressedRow[z]=12;
pressedCol[z]=8;
digitalWrite (ledCol1, LOW); //light LED 2
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '3')
{
pressedRow[z]=13;
pressedCol[z]=8;
digitalWrite (ledCol1, LOW); //light LED 3
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '4')
{
pressedRow[z]=11;
pressedCol[z]=9;
digitalWrite (ledCol1, HIGH); //light LED 4
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '5')
{
pressedRow[z]=12;
pressedCol[z]=9;
digitalWrite (ledCol1, LOW); //light LED 5
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '6')
{
pressedRow[z]=13;
pressedCol[z]=9;
digitalWrite (ledCol1, LOW); //light LED 6
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, HIGH);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '7')
{
pressedRow[z]=11;
pressedCol[z]=10;
digitalWrite (ledCol1, HIGH); //light LED 7
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
delay (220); //wait 220
int off1 = off(); //send to off
}
else if (key == '8')
{
pressedRow[z]=12;
pressedCol[z]=10;
digitalWrite (ledCol1, LOW); //light LED 8
digitalWrite (ledCol2, HIGH);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
delay (220); //wait 220
int off1 = off(); //send to off function
}
else if (key == '9')
{
pressedRow[z]=13;
pressedCol[z]=10;
digitalWrite (ledCol1, LOW); //light LED 9
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, HIGH);
digitalWrite (ledRow1, HIGH);
digitalWrite (ledRow2, HIGH);
digitalWrite (ledRow3, LOW);
delay (220); //wait 220
int off1 = off(); //send to off
}
}
int off() //function turns LEDs off
{
digitalWrite (ledCol1, LOW); //light NONE
digitalWrite (ledCol2, LOW);
digitalWrite (ledCol3, LOW);
digitalWrite (ledRow1, LOW);
digitalWrite (ledRow2, LOW);
digitalWrite (ledRow3, LOW);
}
void loop()
{
}
Thanks!