Developing a sketch based on film "The Martian" to output ASCII characters from 2 4 BIT key presses. I am using the usual membrane keypad. Program works intermittently After the "delay (50)" (only delay in sketch) the program in about 4 of 5 cases jumps back to the beginning of the loop. This causes first key press to have to be repeated several times. Can anyone help?
Once problem is solved will add display and will only print the characters in sequence.
#include "Keypad.h"
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'F','0','E', 'D'} // # = E & * = F for 4 Bit Hex encoding
};
byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,8,9};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int key_val;
int key2_val;
int x;
int y;
void setup() {
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();// Read the key
if (key != NO_KEY){
key_val = key;
Serial.print(" 1st pressed = ");
Serial.print(key);
Serial.print(", key HEX val = ");
Serial.println(key_val, HEX);
if (key_val >= 48 && key_val <= 57){
x = key_val -48;}
if (key_val >= 65 && key_val <= 70){
x = key_val -55;}
x = x << 4; //moves 1st half words to 4MSBits
Serial.print("1st four bits = ");
Serial.println(x, HEX);
delay (50);
}
char key2 = keypad.getKey();// Read the key
if (key2 != NO_KEY){
key2_val = key2;
Serial.print("key ");
Serial.print(key2);
Serial.print(" 2nd pressed");
Serial.print(" ----- ");
Serial.print("key HEX val = ");
Serial.println(key2_val, HEX);
if (key2_val >= 48 && key2_val <= 57){
y = key2_val -48;} // converts keys 0-9 to four bits
if (key2_val >= 65 && key_val <= 70){
y = key2_val -55;} // converts keysA-F to four bits
Serial.print("2nd four bits = 0");
Serial.println(y, HEX);
x = x ^ y; //combiles 4MSBits & 4LSBits to 8 BIT word
Serial.print("X-OR for 8 BIT HEX = ");
Serial.print(x, HEX);
Serial.print(" ----- ");
Serial.print("ASCII character = ");
Serial.println((char) x);
Serial.println("__________________________________");
}
}