That works, partially, but automatically scrolls through pages of information. I want to be able to press the UP button and indefinitely show some continuously updating information, then hit the DOWN button and show some other indefinitely continuously updating information. And possibly other pages as well. Is that possible?
You have to work on code to complete your goal, I can't do all your job.
My two codes are enough examples to do what you are looking for.
I have done that; I have adapted example code multiple times to work in my project. I appreciate the help, however the code examples don't do what I'm looking for. The first one does not allow for code to run continuously and update the display continuously. The second one automatically cycles between multiple pages of information, but does not allow the pages to be selectable and stay on screen indefinitely. You are forgetting I am not a programmer and am learning as I go. As I mentioned, I am looking for a code example that allows for the UP or DOWN (or other buttons) buttons to be pressed and that will change to a different page and allow that code to continuously run and update the display continuously, until the page is changed.
You have to use the button logic from the first example and the time logic from second example to complete your goal.
Play with examples to learn how it works.
Another example:
// Include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <PinChangeInterrupt.h>
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// Encoder output to Arduino Interrupt pin. Tracks the pulse count.
#define ENC_A 18
#define ENC_B 19
// Keep track of the number of pulses
volatile long pulse_count_a = 0;
volatile long pulse_count_b = 0;
volatile long pulse_count_z = 0;
#define UPDATE_INTERVAL 1 // Time is seconds
int menuIndex = 0;
bool stop = false;
unsigned long prevMillis = 0;
void pulse_a();
void pulse_b();
void pulse_z();
void setup()
{
// Put your setup code here, to run once:
// Open the serial port at 9600 bps
Serial.begin(9600);
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
// Print to the LCD.
lcd.setCursor(0, 0);
lcd.print("A:");
lcd.setCursor(0, 1);
lcd.print("B:");
lcd.setCursor(11, 0);
lcd.print("Z:");
// Set pin states of the encoder
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
// Every time the pin goes high, this is a pulse
attachInterrupt(digitalPinToInterrupt(ENC_A), pulse_a, RISING);
attachInterrupt(digitalPinToInterrupt(ENC_B), pulse_b, RISING);
attachPinChangeInterrupt(23, pulse_z, RISING);
}
uint8_t i = 0;
void loop()
{
uint8_t buttons = lcd.readButtons();
if (buttons)
{
if (buttons & BUTTON_SELECT)
{
stop = !stop;
}
}
if(stop == true)
{
prevMillis = millis();
}
switch (menuIndex)
{
case 0:
{
if((millis() - prevMillis) > (UPDATE_INTERVAL * 1000UL))
{
lcd.setCursor(2,0);
lcd.print(pulse_count_a); // Write pulse counts to display
lcd.setCursor(2,1);
lcd.print(pulse_count_b);
lcd.setCursor(13,0);
lcd.print(pulse_count_z);
menuIndex++;
prevMillis = millis();
}
break;
}
case 1:
{
if((millis() - prevMillis) > (UPDATE_INTERVAL * 1000UL))
{
lcd.setCursor(2,0);
lcd.print("Menu 1");
menuIndex++;
prevMillis = millis();
}
break;
}
case 2:
{
if((millis() - prevMillis) > (UPDATE_INTERVAL * 1000UL))
{
lcd.setCursor(2,0);
lcd.print("Menu 2");
menuIndex++;
prevMillis = millis();
}
break;
}
case 3:
{
if((millis() - prevMillis) > (UPDATE_INTERVAL * 1000UL))
{
lcd.setCursor(2,0);
lcd.print("Menu 3");
menuIndex = 0;
prevMillis = millis();
}
break;
}
default:
{
break;
}
}
}
// Increment the number of A pulses by 1
void pulse_a()
{
pulse_count_a++;
}
// Increment the number of B pulses by 1
void pulse_b()
{
pulse_count_b++;
}
// Increment the number of Z pulses by 1
void pulse_z()
{
pulse_count_z++;
}
Press the select button to stop in the current menu.
This code example is about as close as I've been able to get....
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
const int numPages = 4;
int currentPage = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
// Use the button readings to change the current page
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
currentPage = (currentPage + 1) % numPages;
}
if (buttons & BUTTON_DOWN) {
currentPage = (currentPage - 1 + numPages) % numPages;
}
// Show the information for the current page
switch (currentPage) {
case 0:
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Page 1");
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
break;
case 1:
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Page 2");
break;
case 2:
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Page 3");
break;
case 3:
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Page 4");
break;
}
// Update the information on the current page
// lcd.setCursor(0, 1);
// lcd.print(millis()/1000);
}
I commented out some lcd.clear() commands as they were causing the screen to clear over and over. But how to handle clearing the screen, just once, when the user changes pages, so that the info from the previous page is cleared off the screen? I also commented out a section at the bottom, as that shows the same information on each page. I'm trying to get where I can have different info on each page that will update continuously until the user changes pages.
You have to keep this condition, otherwise you will get a blinking LCD.
All I had to do was add lcd.clear() commands when the buttons are pressed. The following works, however I get random pages when I press up and down. According to Adafruit, who makes the LCD shield, the buttons are debounced by the library.
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
const int numPages = 4;
int currentPage = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
// Use the button readings to change the current page
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
currentPage = (currentPage + 1) % numPages;
lcd.clear();
}
if (buttons & BUTTON_DOWN) {
currentPage = (currentPage - 1 + numPages) % numPages;
lcd.clear();
}
// Show the information for the current page
switch (currentPage) {
case 0:
// Page 1 code:
lcd.setCursor(0, 0);
lcd.print("Page 1");
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
break;
case 1:
// Page 2 code:
lcd.setCursor(0, 0);
lcd.print("Page 2");
lcd.setCursor(0, 1);
lcd.print("New Data Here");
break;
case 2:
// Page 3 code:
lcd.setCursor(0, 0);
lcd.print("Page 3");
break;
case 3:
// Page 4 code:
lcd.setCursor(0, 0);
lcd.print("Page 4");
break;
}
}
What it does?
What's the value for currentPage after each press?
I guess it just adds or subtracts one from the currentPage. Which should take you to the next page, but it seems to be random or possibly jumping over pages on each key press.
Just wanted to post this here, in case someone in the future is looking for a simple menu/page system that works well with the Adafruit RGB LCD Shield. This is a fully working example, where you can drop in your code on the various pages. The data on each page will run continuously, until you change pages. Use UP and DOWN button to change pages.
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
const int numPages = 4;
int currentPage = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
// Use the button readings to change the current page
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
while (lcd.readButtons() != 0){} // wait for button release
currentPage = (currentPage + 1) % numPages;
lcd.clear();
}
if (buttons & BUTTON_DOWN) {
while (lcd.readButtons() != 0){} // wait for button release
currentPage = (currentPage - 1 + numPages) % numPages;
lcd.clear();
}
// Show the information for the current page
switch (currentPage) {
case 0:
// Page 1:
lcd.setCursor(0, 0);
lcd.print("Page 1");
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
break;
case 1:
// Page 2:
lcd.setCursor(0, 0);
lcd.print("Page 2");
lcd.setCursor(0, 1);
lcd.print("New Data Here");
break;
case 2:
// Page 3:
lcd.setCursor(0, 0);
lcd.print("Page Three");
lcd.setCursor(0, 1);
lcd.print("More Stuff");
break;
case 3:
// Page 4:
lcd.setCursor(0, 0);
lcd.print("Fourth Page");
lcd.setCursor(0, 1);
lcd.print("1234567890");
break;
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.