I've got the count printing on serial monitor, but when I enter a count for example 99 it prints 9999. Tried for a couple of other counts as well. It prints double the digits.
for (byte i = 0; i < x; i++)
{
lcd.print(digits[i]);
unsigned long count = atol(digits);
Serial.print(count);
a = false;
}
Why are you calling atol() and Serial.print() in a loop?
PaulS:
Why are you calling atol() and Serial.print() in a loop?
Are they to be called separately or somewhere else?
Are they to be called separately or somewhere else?
What is that loop doing? It is printing one character at a time to the LCD and calling atol() and calling Serial.print() to print count.
What DO you want the loop doing? Isn't it obvious that you want it doing only the first thing? Move the other code outside of the loop.
Since you (will) have count as an unsigned long, and since the LCD can print() an unsigned long, it doesn't make sense to print the digits one at a time to the LCD. So, get rid of the loop, call atol() once, and Serial.print() and LCD.print() count once.
PaulS:
Since you (will) have count as an unsigned long, and since the LCD can print() an unsigned long, it doesn't make sense to print the digits one at a time to the LCD. So, get rid of the loop, call atol() once, and Serial.print() and LCD.print() count once.
I'm printing digits one at a time so that user knows what count he is entering. Wouldn't displaying the count only after pressing the enter key be inappropriate?
Now I'm able to print digits one at a time on the lcd and also print the unsigned long count on serial monitor.
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 12, 11}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
bool a = true;
void setup()
{
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.setCursor(3, 0);
lcd.print(" PRIYA");
lcd.setCursor(3, 1);
lcd.print("ELECTRONICS");
delay(100);
for (int positionCounter = 0; positionCounter < 40; positionCounter++)
{
lcd.scrollDisplayLeft();
delay(120);
}
delay(800);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Count:");
}
//unsigned long total;
void loop()
{
const byte entryMaxSize = 5;
static char digits[entryMaxSize + 1];
static byte x; // index
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:");
x = 0; // reset the counter
a = true;
break;
case 'D':
if (a)
{
if (x == 0)
{
invalidCount();
}
else
{
lcd.clear();
lcd.print("Count Set:");
for (byte i = 0; i < x; i++)
{
lcd.print(digits[i]);
a = false;
}
unsigned long count = atol(digits);
Serial.print(count);
}
}
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.
{
digits[x++] = key;
digits[x] = '\0';
lcd.print( key ) ; // new print statement <<<<<<<<<<<<<<<<
//total = ((count[ 0 ] - '0') * 10000UL + (count[ 1 ] - '0') * 1000 + (count[ 2 ] - '0') * 100 + (count[3] - '0') * 10 + (count[4] - '0'));
}
}
else
{
countWarning();
}
}
break;
}
}
}
void invalidCount()
{
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;
}
void countWarning()
{
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;
}
I'm printing digits one at a time so that user knows what count he is entering. Wouldn't displaying the count only after pressing the enter key be inappropriate?
The block of code I was referring to only happened when the 'D' key was pressed. Printing each character entered, as it is entered, IS appropriate. Printing them all again, one at a time, when the 'D' key is pressed, isn't.
PaulS:
The block of code I was referring to only happened when the 'D' key was pressed. Printing each character entered, as it is entered, IS appropriate. Printing them all again, one at a time, when the 'D' key is pressed, isn't.
So what I've implemented now is correct? Shall I proceed to storing the unsigned long 'count' into the Arduino EEPROM?
So what I've implemented now is correct?
If it has been thoroughly tested against a specification, yes.
Shall I proceed to storing the unsigned long 'count' into the Arduino EEPROM?
You don't need to ask permission.
AWOL:
If it has been thoroughly tested against a specification, yes.
What does do you mean by this?
AWOL:
You don't need to ask permission.
Not asking for permission just making sure I've implemented correctly and can proceed with the next part.
AWOL:
If it has been thoroughly tested against a specification, yes.
GautamD:
What does do you mean by this?
it does what you want it to do...
![]()
Yes it is. Thanks Lowell. ![]()