Hi guys,
I have been working on a project with an Arduino Mega 2560, 4x4 keypad, and LCD display. In the program I would like the user to be able to enter a 5-7 digit number using the keypad, press "E" and have the value returned to the main void loop and displayed on the LCD.
//Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//4x4 Keypad setup
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
byte rowPins[ROWS] = {26, 27, 28, 29};
byte colPins[COLS] = {22, 23, 24, 25};
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char hexaKeys[ROWS][COLS] =
{
{'1','2','3','M'},
{'4','5','6','C'},
{'7','8','9','P'},
{'<','0','E','S'}
};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//LCD initiate
LiquidCrystal_I2C lcd(0x27,16,2);
//internal program variables
int Display = 11;
int prevDisplay = 0;
int gettingNum = false;
unsigned long timeOut = 1000;
long int timeIn = 1000;
void setup(){
Serial.begin(9600); //this creates the Serial Monitor
Wire.begin(); //this creates a Wire object
lcd.begin(16,2);
lcd.backlight();
}
//Enter number then press enter
int GetNumber(){
//only works for 4 or less digits
Serial.println("In GetNumber Func");
if (gettingNum == true){
long num = 0;
char key = customKeypad.getKey();
lcd.setCursor(8, 1);
while(key != 'E'){
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':
lcd.print(key);
num = num * 10 + (key - '0');
break;
case 'C':
num = 0;
lcd.clear();
break;
}
key = customKeypad.getKey();
}
//Assigns correct num but returns jumbled num
Serial.println(num);
Serial.println(num/2);
return num;
}
}
void loop(){
//Set Up Menu
char customKey = customKeypad.getKey();
switch(Display){
case 11:{
prevDisplay = 10;
lcd.setCursor(0, 0);
lcd.print("Enter Millis");
//Get millis from buttons, turn to int, set as time out
//May need to set inputNumber to 4 and send to GetNumber();
gettingNum = true;
timeOut = GetNumber();
Serial.println(timeOut);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Extension Time:");
lcd.setCursor(0, 1);
lcd.print(timeOut);
gettingNum = false;
delay(800);
Display = 20;
prevDisplay = 10;
break;
}
}
}
When I run my code, it works great up to 4 digits of user input. For a number larger than 4 digits, the GetNumber() function is interpreting the user input and assigning it into the num variable correctly, but when it is returned to the main void loop, assigned to variable timeOut(), and printed to the LCD or serial print, it becomes a random 10 digit number. I have been trying to use unsigned long, but its still not working.
Thank you for the help!