Hi forum,,
I want to
1-collect 12 digits using a keypad
2-and then saving those 12 digits into a variable (long long variable)
3-and then display this on lcd for confirmation
all of this using a function
and then save it for a use in another function.
So, i just set this long long variable (call it IC) as a global variable and used external function to print the variable on the lcd so that the value doesn't get destroyed.
My question is: Was that right ? Am i actually saving the IC to be used in another function like that?
Now the problem i am facing is: I tried to use a button to move from the first function to the other but apparently i'm stuck in the first one and the button is not doing the goal.
Here is my code:
#include <SoftwareSerial.h>
#include <String.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); //connecting the lcd
int buttonOne = 2; //connected to pin 2
long long IC = 0; // the number accumulator
uint8_t state ;
int buttonOnestate;
uint8_t smallstate;
const uint8_t toTakeID = 0; //state 1 (to collect the 12 digits into IC variable)
const uint8_t correct = 1;//state 2 (i will use the 12 digits here)
void setup() {
smallstate = toTakeID;
lcd.begin(20, 4);
lcd.setCursor(0, 0);
Serial.begin(9600);
pinMode(buttonOne, INPUT);//in real life must be INPUT_PULLUP and connect it to the arduino ground but i'm using potreus to simulate now
}
void loop() {
// state machine
if ( smallstate == toTakeID )
{
long keyvalue; // the key pressed at current moment
int isnum;
do
{
keyvalue = kpd.getKey(); // input the key
isnum = (keyvalue >= '0' && keyvalue <= '9'); // is it a digit?
if (isnum) // if it is a digit then do this
{
lcd.print(keyvalue - '0');
IC = (IC * 10) + keyvalue - '0'; // accumulate the input number
}
} while (isnum || !keyvalue); // until not a digit or while no key pressed
// Print the string from the buffer.
Serial.print("The value is:");
printLL(IC); //using this function because long long is too much for print and println
lcd.clear();
lcd.print("The value is: ");
LCDPrintLL(IC);//using this function because long long is too much for print and println
return IC; //i will need the IC value for later use
buttonOnestate = digitalRead(buttonOne);
if (buttonOnestate == HIGH) { //here i want to go to the next state by using the button
smallstate = correct;
}
}
if ( smallstate == correct ) //the next function
{
lcd.clear();
lcd.print("HI correct"); //just example, my application will involve using the IC here
}
}
//in another topic i was advised to use this function to print since
//it will distroy the value if it is used directly
void printLL(long long val) {
char digits[22];
int index = 21;
digits[index--] = '\0'; // Null terminator
// Take the rightmost digit, even if it's 0;
digits[index--] = (val % 10) + '0';
val /= 10;
// Take all the remaining digits
while (val != 0) {
digits[index--] = (val % 10) + '0';
val /= 10;
}
// Print the string from the buffer.
Serial.print(&digits[index + 1]);
}
//in another topic i was advised to use this function to print since
//it will distroy the value if it is used directly
void LCDPrintLL(long long val) {
char digits[22];
int index = 21;
digits[index--] = '\0'; // Null terminator
// Take the rightmost digit, even if it's 0;
digits[index--] = (val % 10) + '0';
val /= 10;
// Take all the remaining digits
while (val != 0) {
digits[index--] = (val % 10) + '0';
val /= 10;
}
// Print the string from the buffer.
lcd.print(&digits[index + 1]);
}