#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 11, 2, 3, 4, 5, 6, 7, 8, 9);
char bOut[7];
char num[4] = "000";
char format[4] = "000";
char previous[4] = "111";
char hexOut[6] = "";
char hexNum[3];
unsigned long debounceTime;
int keyPos = 0;
int hexPos = 0;
byte outByte;
bool hexRead = false;
bool binReset[10];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '0', 'F', 'E', 'D' }
};
uint8_t colPins[COLS] = { 35, 36, 37, 38 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 31, 32, 33, 34 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
pinMode(39, OUTPUT); //LCD anode
digitalWrite(39, HIGH);
lcd.begin(16,2);
lcd.setCursor(2, 0);
for (int i = 0; i <=8; i++) {
pinMode(i + 20, INPUT_PULLUP);
}
for (int i = 0; i <=8; i++) {
pinMode(i + 40, OUTPUT);
}
printByte();
lcd.setCursor(12,1);
lcd.print("0");
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
GetNumber(key);
}
refresh();
binaryCounter();
}
void printByte() {
for (int i = 0; i < 8; i++) {
lcd.setCursor(7-i,0);
lcd.print(bitRead(outByte, i));
}
lcd.setCursor(9,1);
lcd.print(hexOut);
if (!hexRead){
lcd.setCursor(keyPos, 1);
}
for (int i = 0; i < 8; i++) {
digitalWrite(47 - i, bitRead(outByte, i));
}
}
int GetNumber(char key)
{
if (!hexRead){
switch (key)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
num[keyPos] = key;
if (atoi(num) >= 255) {
strcpy(num, "255");
}
lcd.cursor();
lcd.setCursor(keyPos,1);
lcd.print(num[keyPos]);
keyPos++;
key = NO_KEY;
break;
case 'A':
case 'B': case 'C': case 'D': case 'E': case 'F':
hexRead = true;
lcd.blink();
lcd.setCursor(hexPos + 11,1);
return;
break;
}
}
if (hexRead){
switch(key){
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': case 'A':
case 'B': case 'C': case 'D': case 'E': case 'F':
hexNum[hexPos] = key;
if (StrToHex(hexNum) >= 255) {
strcpy(hexNum, "FF");
}
lcd.blink();
lcd.setCursor(hexPos + 11,1);
outByte = StrToHex(hexNum);
sprintf(num, "%03d", outByte);
lcd.print(hexNum[hexPos]);
++hexPos;
key = NO_KEY;
break;
}
}
}
void binaryCounter() {
for (int i = 0; i <=8; i++){
int j = (9-i);
if (!digitalRead(j + 20) && binReset[j]){
bitWrite(outByte, i, !bitRead(outByte, i));
sprintf(num, "%03d", outByte);
binReset[j] = false;
refresh();
}
else if (digitalRead(j + 20) && millis() - debounceTime >= 20){
binReset[j] = true;
debounceTime = millis();
}
}
}
void refresh() {
if (keyPos >= 3) {
keyPos = 0;
lcd.noCursor();
}
if (hexPos > 1) {
hexPos = 0;
hexRead = false;
return;
}
if (atoi(num) != atoi(previous)) {
lcd.setCursor(0, 1);
lcd.print(num);
lcd.setCursor(keyPos,1);
strcpy(previous, num);
outByte = byte(atoi(num));
sprintf(hexOut, "0x%02%X", outByte);
printByte();
}
if (!hexRead) {
lcd.noBlink();
}
}
int StrToHex(char str[])
{
return (int) strtol(str, 0, 16);
}