void loop()
{
const byte entryMaxSize = 5;
static char count[entryMaxSize];
unsigned long total;
// index
static byte x = 0;
char key = keypad.getKey();
if (key != NO_KEY) // Check for a valid key.
{
switch (key)
{
case 'A':
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Count:");
// reset the counter
x = 0;
a = true;
break;
case 'D':
if (a) {
if (x == 0)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Invalid Count!!");
lcd.setCursor(0, 1);
lcd.print("Press A"); // suggesting the user to enter the count again
a = false;
}
else {
lcd.clear();
lcd.print("Count Set:");
for (byte i = 0; i < x; i++) {
lcd.print(count[i]);
a = false;
}
}
}
break;
default:
if (a)
{
// if not 5 characters yet
if (x < entryMaxSize)
{
// add key to userinput array and increment counter
if ( key >= '0' && key <= '9' ) {
// key is of type char and has a value between 0 and 9 so do something with it.
count[x++] = key;
lcd.print( key ) ; // new print statement <<<<<<<<<<<<<<<<
total = ((count[ 5 ] - '0') + (count[ 4 ] - '0') * 10 + (count[ 3 ] - '0') * 100 + (count[4] - '0') * 1000 + (count[5] - '0') * 10000);
}
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("5 Digits Only!!"); // warning for the user if more than permitted digits are entered
lcd.setCursor(0, 1);
lcd.print("Press A"); // suggesting the user to enter the count again
a = false;
}
}
break;
}
}
}
C:\Users\Umesh\Documents\Arduino\sketch_oct06a_experimental\sketch_oct06a_experimental.ino:51:17: warning: variable 'total' set but not used [-Wunused-but-set-variable]
The compiler appears to be telling you that you have gone to a lot of effort to set "total" in the loop() but not used it anywhere. Either turn it into a global variable (put the definition outside the loop) or use it say with Serial.println( total ) ;
atoi(), as has been pointed out to you, would be an even better choice. One reason is that it handles the situation better if characters other than 0-9 are present.
Here: (count[5] - '0') * 10000) it would be good to say (count[5] - '0') * 10000UL) instead. The UL ensures that the number is not treated as an int which could be too small for the result.
Except that I now see there is an error in that count should go from 0 to 4, not 1 to 5 so you should change the code accordingly.
It is like if you pressed the '9' key 5 times on a pocket calculator.
The first press give the number of 10,000's, the second press gives the number of 1,000's etc. etc.
The complete number, that is, 99,999 is an integer which can be used in the following calculation.
would result in val containing 35007.
If count[3] contained a 'E', then val would contain 350.
With the code you have been trying, val would contain some meaningless garbage if any element in the array was not a digit.
@PaulS I want to implement it the way you are suggesting. I want to obtain a count which can be any number between 0 to 99,999 and then storing it in the Arduino EEPROM for comparison. What changes I shall make to my current code since you say that my current code is not helping me achieve what I want. Thanks.
What changes I shall make to my current code since you say that my current code is not helping me achieve what I want.
You want to get from point A (what the code does now) to point B (the code does what you want it to do). I have no ideal where point A is (you have not said what you current code looks like OR what it actually does) or where point B is (I do not understand what you are trying to actually store in EEPROM).