I am using some code I found on the forum for entering decimal values. It was written by @GolamMostafa in Dec, 2019.
The entry code works fine, but I want the ability to either delete a keystroke or re-enter the value so a mistake can be corrected. I couldn't figure out how to delete a keystroke so I opted for re-entry of the entire value. I can get rejection, but I can't seem to get the entry system to restart.
My code is below without my various restart code tries.
Please be gentle – I am 78 and before playing with Arduino, my only coding experience was Visual Basic back in the early 1990s.
Many thanks,
Don
//LIBRARIES USED
#include <Keypad_I2C.h> // I2C Keypad library from https://github.com/joeyoung/arduino_myKeypads
#include <Keypad.h>
#include <LiquidCrystal_I2C.h> // I2C LCD Library from https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#include <Stepper.h>
#include <Wire.h>
/*** SET lcd ***/
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define lcd_addr 0x27 // I2C address of typical I2C LCD Backpack
#define myKeypad_addr 0x20 // I2C address of I2C Expander module
//LCD Pins to I2C LCD Backpack - These are default for HD44780 LCD's
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
// Define the myKeypad pins
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Keypad pins connected to the I2C-Expander pins P0-P7
byte rowPins[ROWS] = {0, 1, 2, 3}; // connect to the row pinouts of the myKeypad
byte colPins[COLS] = {4, 5, 6, 7}; // connect to the column pinouts of the myKeypad
// Create instance of the Keypad name myKeypad and using the PCF8574 chip
Keypad_I2C myKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS, myKeypad_addr, PCF8574 );
char myData[5] = {'_', '_', '.', '_', '\0'};
int arrayTracker = 3;
byte counter = 0;
float enteredValue;
char customKey;
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
// Initialize Keypad
myKeypad.begin(makeKeymap(keys) );
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
lcd.setCursor(0, 0); //cursor position
lcd.print("Enter Code:");
lcd.setCursor(0, 1);
lcd.print(myData);
}
void loop()
{
DecimalEntry();
}
/**** FUNCTIONS ****/
void DecimalEntry()
{
customKey = myKeypad.getKey(); //scan myKeypad
if (customKey != 0x00) //if scan code is non-zer0
{
myData[arrayTracker] = customKey; //store the ASCII code of the lable of key in array
ShowOnLCD();
counter++;
Serial.print("myData: "); Serial.println(myData);
Serial.print("arrayTracker: "); Serial.println(arrayTracker);
Serial.print("counter: "); Serial.println(counter);
if (counter == 3)
{
enteredValue = atof(myData);
HashToComplete();
AcceptReject();
}
}
}
// DISPLAY ENTRY VALUE
void ShowOnLCD()
{
lcd.setCursor(0, 1);
lcd.print(myData);
if (counter != 2)
{
myData[0] = myData[1];
myData[1] = myData[3];
}
}
// PRESS # TO ACCEPT
char HashToComplete()
{
lcd.setCursor(0, 3);
lcd.print("Press # to Complete");
Serial.println();
Serial.println("Press # to Complete");
customKey = myKeypad.getKey(); // wait for the "*" key to be pushed
while (customKey != '#')
{
customKey = myKeypad.getKey();
}
}
// ACCEPT OR REJECT
void AcceptReject()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You entered "); lcd.print(enteredValue, 1);
lcd.setCursor(0, 1);
lcd.print("A - Accept");
lcd.setCursor(0, 2);
lcd.print("B - ReEnter");
Serial.println();
Serial.print("You entered "); Serial.println(enteredValue, 1);
Serial.println("A - Accept");
Serial.println("B - ReEnter");
customKey = myKeypad.getKey();
while (customKey != 'Z')
{
customKey = myKeypad.getKey();
//customKey = myKeypad.getKey();
if (customKey == 'A') // accept entry and move on to next action
{
ChoiceA();
}
else if (customKey == 'B') // reject entry & re-enter values
{
ChoiceB();
}
}
}
void ChoiceA()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Okay, Move On ");
Serial.println("Okay, Move On ");
}
void ChoiceB()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reset Entry ");
Serial.println("Reset Entry ");
// At this juncture I want to return to the
// DecimalEntry function to re-enter the values.
// Calling the function does not work.
}