Hello,
I have my Arduino project with an I2C 20x4 LCD display connected, and i want make multiple "pages" on it, and change page with push buttons. I fear this is a simple question, but i am still getting confused...
I have various values that the Arduino reads, and i am displaying them on the LCD. However, i want to display some values on the LCD, say analog read A3 and digital read pin 7, and then if i push button A, the LCD changes to displaying some different values, such as a calculated voltage value, as an example.
Then, if i push button B, the LCD will go back to displaying the original values: Basically like a simple menu system, with a button to "scroll" the menu one way, and another button to go the other way.
Of course, i want the system to be able to continue doing other things no matter what menu the LCD is on, such as turn pin 8 to HIGH when analog read of A3 is higher than a set point, for example.
Sorry if that is a confusing question, but would appreciate some help with the code..
All you have to do is to have a mode variable which defines what to draw on the screen when you want it to refresh.
So when it comes to drawing the screen you make a function that looks at this variable and draws what it needs to draw depending on what your mode variable is.
A mode variable is just like any other variable. It contains a number which says what sort of screen you want to draw.
Your push buttons will simply change this mode variable and then call the redraw function.
i'm guessing you have a bunch of lcd function calls to update the display rather than a sub-function that is passed a few strings and you're reluctant to duplicate the block of code to display different things
or is this more about processing buttons or even cycling thru different displays every few seconds?
Yes because you function to redraw will include getting the numbers and displaying them.
The screen will flicker each time the numbers get refreshed but there is a way round that. Remember when developing anything you take it one step at a time and get that working before you refine it.
Thanks all, very helpful advice, i have learnt a lot.
I have now basically got what i wanted working, with this code:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//Pin Name Variables
const int setpointPin = A3;
const int ledPin = 7;
const int menuRightPin = 2;
const int menuLeftPin = 3;
//Other Variables
int setpointAnalog;
float setpointVolts;
int lcdCurrentPg;
void setup() {
pinMode(setpointPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(menuRightPin, INPUT_PULLUP);
pinMode(menuLeftPin, INPUT_PULLUP);
lcdCurrentPg = 1;
lcd.backlight();
lcd.init();
lcd.clear();
}
void loop() {
setpointAnalog = analogRead(setpointPin);
setpointVolts = 5.0 / 1024 * setpointAnalog;
if (digitalRead(menuRightPin) == LOW) {
lcdCurrentPg = 2;
}
if (digitalRead(menuLeftPin) == LOW) {
lcdCurrentPg = 1;
}
//Draw stuff to the LCD:
if (lcdCurrentPg == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Analog Value:");
lcd.setCursor(13, 0);
lcd.print(setpointAnalog);
lcd.setCursor(0, 1);
lcd.print("Voltage:");
lcd.setCursor(8, 1);
lcd.print(setpointVolts);
lcd.setCursor(12, 1);
lcd.print("V");
delay(500);
} else {
lcd.clear();
lcd.print("Stuff On Other Page");
delay(500);
}
//Everything else goes here:
if (setpointVolts > 2.5) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
I do get that this might be messy code/an over complicated way of doing it, but at least it works, and i understand everything there.
Now i just need to stop the screen flickering when the values update (i had noticed that even before making this menu system), Grumpy_Mike, perhaps you could point me in the right direction? Is it something to do with updating the whole display all the time? Also, i could try and get rid of the nasty delays in my code, because they cause it to all run a bit slow, because the code keeps stopping to wait for the delay.
Obviously this only really works the way it is with 2 pages on the LCD, but i can modify it to work with more pages if needed.
Yes it is everything to do with that. If you do a clear screen when you just want to update the values the screen will go off and then be painted again. All you need to do is to have two parts to the drawing, The first is to update the whole thing along with the values. The next will be just to display the values. Make a note (store in a variable) the last values you wrote and only update the screen when they change. You can update the variables by drawing a black rectangle over the place the old variable was stored and the writing the new one. The flickering will vanish.