My students and I am having a strange problem with this code. We are using the 5x6 ortho neokey keypad with the Talkie library. This keyboard is being used as a Braille Teaching device. My students added Braille keycaps and they want to have 2 modes. The teach Mode is where the speaker will say the name of the letter out loud after a button is pressed. The game Mode is where a letter is randomly pressed and it says "Good Work" if the correct button is pressed and "Try Again" if not pressed within 10 seconds.
The problem is that in teach mode, when you press any button in the 4th row, it says all the letters in that row, one after the other. When I try the sample code in the Adafruit Keypad library, it behaves perfectly. Can you see something wrong in this code? I am at a loss as to what is going on.
Could this be the Talkie library not playing nice with the keypad code? It seems to behave erratically and unpredictably.
Thanks.
#include "Adafruit_Keypad.h"
#include "Talkie.h"
#include "Vocab_US_Large.h"
#include "Vocab_Special.h"
#include "Vocab_US_TI99.h"
#include "Vocab_US_Clock.h"
#include "Vocab_Soundbites.h"
#include "Vocab_US_Acorn.h"
#define ROWS 5 // rows
#define COLS 6 // columns
long startTime = 0;
boolean nameIt = true;
//define the symbols on the buttons of the keypads
char keys[ROWS][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', 'Z', ' ', ' ', ' ', ' ' }
};
uint8_t rowPins[ROWS] = { 8, 9, 10, 11, 12 }; //connect to the row pinouts of the keypad
uint8_t colPins[COLS] = { 7, 6, 5, 4, 13, 2 }; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Adafruit_Keypad customKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Talkie voice;
char letterKey;
int switchPin = A0;
int letterpicked;
void setup() {
Serial.begin(9600);
//while (!Serial);
Serial.println("Ortho 5x6 keypad demo");
customKeypad.begin();
randomSeed(analogRead(5));
letterpicked = random(0, 27);
}
void loop() {
if (analogRead(switchPin) > 500) {
Serial.println("teach mode");
teachMode();
} else {
Serial.println("game mode");
gameMode();
}
}
void teachMode() {
customKeypad.tick();
while (customKeypad.available()) {
keypadEvent e = customKeypad.read();
Serial.print((char)e.bit.KEY);
if (e.bit.EVENT == KEY_JUST_PRESSED) {
Serial.println(" pressed");
uint8_t row = e.bit.ROW;
uint8_t col = e.bit.COL;
Serial.print("Row: ");
Serial.print(row);
Serial.print(" col: ");
Serial.print(col);
Serial.print(" -> ");
uint16_t keynum;
Serial.println(keynum);
sayLetter((char)e.bit.KEY);
}
//else if (e.bit.EVENT == KEY_JUST_RELEASED) {
// Serial.println(" released");
// }
}
delay(100);
}
void sayLetter(char letter) {
switch (letter) {
case 'A':
voice.say(sp2_A);
break;
case 'B':
voice.say(sp2_B);
break;
case 'C':
voice.say(sp2_C);
break;
case 'D':
voice.say(sp2_D);
break;
case 'E':
voice.say(sp2_E);
break;
case 'F':
voice.say(sp2_F);
break;
case 'G':
voice.say(sp2_G);
break;
case 'H':
voice.say(sp2_H);
break;
case 'I':
voice.say(sp2_I);
break;
case 'J':
voice.say(sp2_J);
break;
case 'K':
voice.say(sp2_K);
break;
case 'L':
voice.say(sp2_L);
break;
case 'M':
voice.say(sp2_M);
break;
case 'N':
voice.say(sp2_N);
break;
case 'O':
voice.say(sp2_O);
break;
case 'P':
voice.say(sp2_P);
break;
case 'Q':
voice.say(sp2_Q);
break;
case 'R':
voice.say(sp2_R);
break;
case 'S':
voice.say(sp2_S);
break;
case 'T':
voice.say(sp2_T);
break;
case 'U':
voice.say(sp2_U);
break;
case 'V':
voice.say(sp2_V);
break;
case 'W':
voice.say(sp2_W);
break;
case 'X':
voice.say(sp2_X);
break;
case 'Y':
voice.say(sp2_Y);
break;
case 'Z':
voice.say(sp2_Z);
break;
}
delay(1000);
}
void gameMode() {
Serial.print(letterpicked);
Serial.println(" ");
if (millis() - startTime < 10000) {
while (nameIt) {
switch (letterpicked) {
case 0:
voice.say(sp2_A);
letterKey = 'A';
Serial.println("letter chosen = A");
break;
case 1:
voice.say(sp2_B);
letterKey = 'B';
Serial.println("letter chosen = B");
break;
case 2:
voice.say(sp2_C);
letterKey = 'C';
Serial.println("letter chosen = C");
break;
case 3:
voice.say(sp2_D);
letterKey = 'D';
Serial.println("letter chosen = D");
break;
case 4:
voice.say(sp2_E);
letterKey = 'E';
Serial.println("letter chosen = E");
break;
case 5:
voice.say(sp2_F);
letterKey = 'F';
Serial.println("letter chosen = F");
break;
case 6:
voice.say(sp2_G);
letterKey = 'G';
Serial.println("letter chosen = G");
break;
case 7:
voice.say(sp2_H);
letterKey = 'H';
Serial.println("letter chosen = H");
break;
case 8:
voice.say(sp2_I);
letterKey = 'I';
Serial.println("letter chosen = I");
break;
case 9:
voice.say(sp2_J);
letterKey = 'J';
Serial.println("letter chosen = J");
break;
case 10:
voice.say(sp2_K);
letterKey = 'K';
Serial.println("letter chosen = K");
break;
case 11:
voice.say(sp2_L);
letterKey = 'L';
Serial.println("letter chosen = L");
break;
case 12:
voice.say(sp2_M);
letterKey = 'M';
Serial.println("letter chosen = M");
break;
case 13:
voice.say(sp2_N);
letterKey = 'N';
Serial.println("letter chosen = N");
break;
case 14:
voice.say(sp2_O);
letterKey = 'O';
Serial.println("letter chosen = O");
break;
case 15:
voice.say(sp2_P);
letterKey = 'P';
Serial.println("letter chosen = P");
break;
case 16:
voice.say(sp2_Q);
letterKey = 'Q';
Serial.println("letter chosen = Q");
break;
case 17:
voice.say(sp2_R);
letterKey = 'R';
Serial.println("letter chosen = R");
break;
case 18:
voice.say(sp2_S);
letterKey = 'S';
Serial.println("letter chosen = S");
break;
case 19:
voice.say(sp2_T);
letterKey = 'T';
Serial.println("letter chosen = T");
break;
case 20:
voice.say(sp2_U);
letterKey = 'U';
Serial.println("letter chosen = U");
break;
case 21:
voice.say(sp2_V);
letterKey = 'V';
Serial.println("letter chosen = V");
break;
case 22:
voice.say(sp2_W);
letterKey = 'W';
Serial.println("letter chosen = W");
break;
case 23:
voice.say(sp2_X);
letterKey = 'X';
Serial.println("letter chosen = X");
break;
case 24:
voice.say(sp2_Y);
letterKey = 'Y';
Serial.println("letter chosen = Y");
break;
case 25:
voice.say(sp2_Z);
letterKey = 'Z';
Serial.println("letter chosen = Z");
break;
}
nameIt = false;
}
} else {
voice.say(spt_TRY_AGAIN);
letterpicked = random(0, 27);
startTime = millis();
nameIt = true;
}
getKeyStroke();
delay(1000);
}
void getKeyStroke() {
customKeypad.tick();
while ((customKeypad.available()) && (digitalRead(switchPin <= 500))) {
Serial.print("letter typed= ");
keypadEvent e = customKeypad.read();
Serial.println((char)e.bit.KEY);
if ((letterKey) == (char)e.bit.KEY) {
voice.say(spt_GOOD_WORK);
Serial.println("good work");
letterpicked = random(0, 27);
nameIt = true;
}
}
delay(1000);
}