Here is what I have now
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char userInput; // input from computer
char userInput2; // input from keypad
char currChar; //character to be stored in character array
boolean newChar = false;
boolean newChar2 = false;
boolean terminate = false; // if true then it ends the program
const byte numChars = 3; // size of character array
char receivedChars[numChars]; // stores characters if '*' or 'E' is pressed
int count = 0;
static byte i = 0; //keeps index of receivedChars array
int Ecount = 0; //keeps track of number of E entered
int Acount = 0; //keeps track of number of * entered
void setup() {
Serial.begin(9600);
Serial1.begin(9600); // communication between
// mega and uno
}
void loop() {
getOneChar();
displayChar();
//checkCharVal();
if( terminate == true )
{
exit(0); //is supposed to terminate program when consition met
}
}
void getOneChar()
{
//char customKey = customKeypad.getKey();
//userInput = Serial.read(); // reads user Input
char endMarker = '*';
char endMarker2 = 'E';
if(Serial.available()) //receives data from computer
{
currChar = Serial.read(); // reads user Input from computer
userInput = currChar; //stores the character in userInput var
if(userInput == 'E')
{
//checkCharVal();
++Ecount;
Serial.println("Ecount: ");
Serial.println(Ecount); // gives value of Ecount
if( Ecount == 3) //checks if val of Ecount = 3
{
Serial.println("PROGRAM TERMINATED");
//terminate = true;
}
}
else
{
Ecount = 0;
Serial.println("Ecount set back to value of: ");
Serial.println(Ecount);
}
/*if(userInput == '*')
{
//currChar = userInput;
//checkCharVal();
Serial.println("Acount: ");
Serial.println(Acount);
Acount++;
}
else
{
Acount = 0;
Serial.println("Acount set back to value of: ");
Serial.println(Acount);
}*/
newChar = true;
}
char customKey = customKeypad.getKey();
if (customKey)
{
userInput2 = customKey;
if(customKey == '*') // checks value of customKey
{
//currChar = userInput;
//checkCharVal();
++Acount;
Serial.println("Acount: ");
Serial.println(Acount); //prints val of Acount
if( Acount == 3) //checks of Acount is equal to 3
{
Serial.println("PROGRAM TERMINATED");
//terminate = true;
}
}
else
{
Acount = 0; // resets Acount to 0 if condition not met
Serial.println("Acount set back to value of: ");
Serial.println(Acount); // gives current val of Acount
}
newChar2 = true;
}
}
void displayChar()
{
if(newChar == true) // deals with char from computer
{
Serial1.write(userInput); // displays char from computer on LCD
Serial.println("Character Received from computer: ");
Serial.println(userInput); // displays char from computer on console
newChar = false;
}
if(newChar2 == true) // deals with char from keypad
{
Serial1.print(userInput2); // displays char from keypad on LCD
Serial.println("Character Received from keypad: ");
Serial.println(userInput2); // displays char from keyoad on console
newChar2 = false;
}
}
Test for input coming from Keypad:
Acount:
1
Character Received from keypad:
*
Acount set back to value of:
0
Character Received from keypad:
C
Acount:
1
Character Received from keypad:
*
Acount:
2
Character Received from keypad:
*
Acount set back to value of:
0
Character Received from keypad:
9
Acount:
1
Character Received from keypad:
*
Acount:
2
Character Received from keypad:
*
Acount:
3
PROGRAM TERMINATED
Character Received from keypad:
*
Things are going in the right direction, the program terminates before the third consecutive '' is printed. For testing purposes if exit(0) is called I never see the program output the last ''. However, this is fine as long as it acknowledge 3 '*' were input consecutively. So it seems fine..
Input coming from computer:
Ecount:
1
Character Received from computer:
E
Ecount set back to value of:
0
Character Received from computer:
Ecount:
1
Character Received from computer:
E
Ecount set back to value of:
0
Character Received from computer:
Ecount:
1
Character Received from computer:
E
Ecount set back to value of:
0
Character Received from computer:
Ecount set back to value of:
0
Character Received from computer:
Q
Ecount set back to value of:
0
Character Received from computer:
Ecount:
1
Character Received from computer:
E
Ecount set back to value of:
0
Character Received from computer:
This seems to be a different story. Ecount is stuck at value of "1" regardless of how many consecutive 'E's that I enter. However, it does reset when I enter a character that is not 'E'. Unfortunately it seems like it runs the line "Character Received from computer: " again without user input and outputs a blank line.
It seems the lines of code are not executing in the order I want. the comparison of whether Acount is == 3 trumps my print statements and ends the program. For Ecount the if statement moves forward without even receiving a user input and fails to properly increment ECount.