Would you help me with the corrections to the code? I'm trying but I'm not able to get the keys to print it the way I want.
Not me. I've tried, from the beginning, to get you to build functions that handle the various task. You seem welded to the idea that everything needs to happen in loop(), and unwilling to concede that that is NOT the case.
It is far simpler to debug 20 to 25 lines of code that deal with getting input and displaying it on the LCD in a known position (hint: you know where to store the data in the array, so use that index value to "guess" where to display the character on the LCD) than it is to wade through hundreds of lines of code in loop().
Would you help me with the corrections to the code?
It would be more instructive and you would learn more if you made the changes yourself with help as to what you need to do, but you do not seem to listen.
lcd.clear(); //do not clear the entire LCD. Position the cursor and print 5 spaces over the number
char displayText[18] = ""; //totally unnecessary
snprintf(displayText, sizeof(displayText), "Set Count:%5d", key - '0'); //and this
lcd.setCursor(0, 0); //position the cursor where you want to print the full number
lcd.print(displayText); //print the digits string not the 18 character mostly empty string
PaulS:
Not me. I've tried, from the beginning, to get you to build functions that handle the various task. You seem welded to the idea that everything needs to happen in loop(), and unwilling to concede that that is NOT the case.
It is far simpler to debug 20 to 25 lines of code that deal with getting input and displaying it on the LCD in a known position (hint: you know where to store the data in the array, so use that index value to "guess" where to display the character on the LCD) than it is to wade through hundreds of lines of code in loop().
I'm not welded to making everything work through the loop. I'm just trying to get the basic stuff going. Building functions outside the loop is probably the next step. For what should I build functions outside the loop so that debugging becomes easier?
UKHeliBob:
It would be more instructive and you would learn more if you made the changes yourself with help as to what you need to do, but you do not seem to listen.
lcd.clear(); //do not clear the entire LCD. Position the cursor and print 5 spaces over the number
char displayText[18] = ""; //totally unnecessary
snprintf(displayText, sizeof(displayText), "Set Count:%5d", key - '0'); //and this
lcd.setCursor(0, 0); //position the cursor where you want to print the full number
lcd.print(displayText); //print the digits string not the 18 character mostly empty string
I've removed the lcd.clear() part from my code, I've not posted it yet because I'm not able to print the keys where I want.
Building functions outside the loop is probably the next step.
It is NOT the next step. When you have code working as part of loop(), move it out of loop() into a function. The function you are working on, whether it is loop() or some other function, should be as short as possible.
In any case, you need to abandon this project for a while. Play around with writing to the LCD completely separately from this project. When you COMPLETELY understand what setCursor(), clear(), etc. do, then you will have no problem using the LCD in this project, and putting the text EXACTLY where you want it.
Play around with strings. Clearly, you do not COMPLETELY understand arrays and putting text in arrays at specific positions and what NULL terminated means and printing arrays vs. printing characters from arrays.
When you do, you'll understand that what you are trying to do is very simple.
There IS a correlation between where the put a letter into the array and where to print the letter on the LCD. When you see that, putting the letter into the array and putting the letter on the LCD will be a piece of cake.
I mean what part from my code should I try to build outside the loop?
You want the user to enter a number, that you call count. So, create a function:
unsigned long count = getCount();
You want to save that value in EEPROM. So, create a function:
saveInEEPROM(count, address);
You want to show the user the value stored in EEPROM when the Arduino starts up, so you need to read that value. Create a function:
unsigned long storedCount = readFromEEPROM(address);
Then, when you have a problem, it is easy for you to discuss which function you need help writing/debugging/calling. It is easy to look at a few lines of code that purport to save an unsigned long in EEPROM by looking at the function saveInEEPROM() vs. having the find the 10 lines of code in the 12000 line loop() function that are supposed to do that.
Back again with the corrections that PaulS suggested. For now tried to build functions outside the loop. Following is the code. If any improvements/ modifications please suggest. Thanks.
#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;
const byte entryMaxSize = 5;
static char digits[entryMaxSize + 1];
static byte x; // index
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:");
}
void loop()
{
char key = keypad.getKey();
if (key != NO_KEY) // Check for a valid key.
{
switch (key)
{
case 'A': initializeCounter(); break;
case 'D': setCount(); break;
default: getCount(key);
}
}
}
void initializeCounter()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Count:");
x = 0; // reset the counter
a = true;
}
void getCount(char key)
{
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 <<<<<<<<<<<<<<<<
}
}
else
{
countWarning();
}
}
}
void setCount()
{
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);
}
}
}
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;
}
Minor improvements would include giving the variables a and x meaningful names and using the smallest applicable data types for variables such as LCD_COLS and LCD_ROWS which are less than 255. The keys, rowPins and colPins arrays could be made const
Made following changes to print from right to left.
unsigned long tempcount; // temporary count created by key entry
void getCount(const byte state, char key)
{
if (state == PRESSED)
{
if (getNumber)
{
// if not 5 characters yet
if (index < 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[index++] = key;
digits[index] = '\0';
tempcount = atol(digits);
char displayText[17] = "";
snprintf(displayText, sizeof(displayText), "Set Count: %5lu", tempcount);
lcd.setCursor(0, 0);
lcd.print(displayText);
}
}
else
{
countWarning();
}
}
}
}