#include <Wire.h> //
#include <SparkFunSX1509.h> //
#include <FastLED.h>
int wrong =0;
int extra =0;
int count =0;
int i, H=0, T=0 , Z=0 ,key;
char LEDpins[5][5]= {
{'a','b','c','d','e'},
{'f','g','h','i','j'},
{'k','l','m','n','o'},
{'p','q','r','s','t'},
{'u','v','w','x','y'},
};
int randomNumber[5]; //a variable to hold a random number
const byte SX1509_ADDRESS = 0x3E; // SX1509 I2C address (00)
SX1509 io; // Create an SX1509 object
#define KEY_ROWS 5
#define KEY_COLS 5
// Handy array we'll use to map row/column pairs to
// character values:
char keyMap[KEY_ROWS][KEY_COLS] = {
{'a','b','c','d','e'},
{'f','g','h','i','j'},
{'k','l','m','n','o'},
{'p','q','r','s','t'},
{'u','v','w','x','y'},
};
// ARDUINO pin 2 connected to SX1509 interrupt
#define INTERRUPT_PIN 2
#define LED_PIN 7
#define NUM_LEDS 25
CRGB leds[NUM_LEDS];
const int buzzer = 7; //buzzer to arduino pin 7
void setup() {
randomSeed(analogRead(A0)); //generate a random seed
Serial.begin(9600); //start serial communication
// What is all these line below for?
/////////////////////////////////////////////////////////////////////////
for (int a=0; a<5; a++)
{
pinMode(LEDpins[a],OUTPUT); //set LED as output
}
for (int b=0; b<5; b++)
{
pinMode(LEDpins[b],OUTPUT); //set LED as output
}
uint8_t x[25];
for (size_t i = 0; i < 25; i++) x[i] = i+1;
for (size_t i = 0; i < 25; i++)
{
size_t j = random(i, 25);
// swap x[i] and x[j]
auto t = x[i];
x[i] = x[j];
x[j] = t;
}
////////////////////////////////////////////////////////////////////////
if (!io.begin(SX1509_ADDRESS))
{
Serial.println("Failed to communicate.");
while (1);
}
// defining scan time, debounce time, and sleep time:
// Sleep time range: 128 ms - 8192 ms (powers of 2) 0=OFF
unsigned int sleepTime = 0;
// Scan time range: 1-128 ms, powers of 2
byte scanTime = 32 ; // Scan time per row, in ms
// Debounce time range: 0.5 - 64 ms (powers of 2)
byte debounceTime = 64; // Debounce time
io.keypad(KEY_ROWS, KEY_COLS, sleepTime, scanTime, debounceTime);
// Set the ARDUINO pin as an input, to monitor the interrupt
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
Serial.println("Row | Col | Key");6;
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 7 as an output
} //End of void setup
void loop() {
// 1. Generate random lights on the LED
FastLED.clear();
for (i=0; i<5; i++)
{
randomNumber[i] = (random(0,24));
leds[randomNumber[i]] = CRGB(10, 10, 10);
delay(50);
}
FastLED.show();
// KP: Print out the random numbers you generated to check
for (i=0; i<5; i++)
{
Serial.println(randomNumber[i]);
}
// 2. Clear the lights and start the games
delay(6000);
FastLED.clear(); //
FastLED.show();
wrong=0;
while( count<5 && wrong==0)
{
// 3. Read
// If the interrupt pin goes active-low, a keypad button
// is begin pressed:
while(digitalRead(INTERRUPT_PIN));
count++;
// Use readKeypad() to get a binary representation for
// which row and column are pressed
unsigned int keyData = io.readKeypad();
// Use the getRow, and getCol helper functions to find
// which row and column keyData says are active.
byte row = io.getRow(keyData);
byte col = io.getCol(keyData);
char key = keyMap[row][col];
Serial.print(String(row) + " | " + String(col) + " | ");
Serial.println(key);
// 4. Check and score
key=key-97;
Serial.print("I pressed: ");
Serial.println(key,DEC);
for (int i=0; i<5; i++) // KP: You are looping 5 times and checking if there is a match between key and the elements in the array. [FIXED]
{
Serial.print("Random Number: ");
Serial.println(randomNumber[i]);
if(key==randomNumber[i]) //1
{
Serial.print(T);
Serial.println("Match");
}
else {
if (key==randomNumber[i++]) //2
{
Serial.print(T);
Serial.println("Match");
}
}
else {
if(key==randomNumber[i+2]) //3
{
Serial.print(T);
Serial.println("Match");
}
}
else {
if(key==randomNumber[i+3]) //4
{
Serial.print(T);
Serial.println("Match");
}
}
else {
if(key==randomNumber[i+4]) //5
{
Serial.print(T);
Serial.println("Match");
}
else // else wo a previous ‘if’ {
T=0;
Serial.println("Try Again");
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
wrong = 1;
}
[/quote]
So basically for our project we want to create a toy where there would be 5 random LED that will light up randomly in a 5x5 led strip.User have to key in all 5 position of the LED correct on the keypad for the SCORE to increment by 1. So i have to use arduino to generate the 5 numbers which will sync to the LED and the exact position of the number corresponding to the respective LED will light up. When the LED lights up there will be a 5x5 keypad connected to the arduino, where user will have to click the position of the LED on the keypad. The problem now is my arduino only checks the input from the keypad twice then it will exit to generate another 5 sets of random number. I want to ask for your help to edit this code because even though i key in the same values on the keypad as generated on the serial monitor output. My program will not check through the 5 sets of random number before deciding whether my user input all the values on the keypad correct or not. It will always just check till the 2 nd set of number and exit the program to generate a new set of random numbers.
- List item