I would like to do a menu system, using a LCD-shield with five buttons (RGB LCD-shield, 16x2). The select-button should show different messages if I have pressed one, two or three times. My problem is that the first message shows for every press, but the second and third message is shown afterwards. Can someone help me to get rid of the first message?
Thanks in advance!
/*********************
Example code for the Adafruit RGB Character LCD Shield and Library
This code displays text on the shield, and also reads the buttons on the keypad.
When a button is pressed, the backlight changes color.
**********************/
// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.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();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
unsigned long time, desiredTemp, desiredTime, n;
byte numbr[4]; //hold 4 bytes
byte indx; // will hold an index to the array
int enteredTemp; //holds the final value entered
int enteredTime; //holds the final value entered
//to give the buttons more functions
byte buttonPresses = 0; // how many times the button has been pressed
byte lastPressCount = 0; // to keep track of last press count
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//meddelande vid start
lcd.print("Temp = up/down");
lcd.setCursor(0, 1);
lcd.print("Time = left/right");
desiredTemp=62;
desiredTime=60;
lcd.setBacklight(BLUE); //choose colour
}
uint8_t i=0;
void loop() {
//give functions to the buttons
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_LEFT) {
desiredTime--;
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
}
if (buttons & BUTTON_UP) {
desiredTemp++;
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
}
if (buttons & BUTTON_DOWN) {
desiredTemp--;
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
}
if (buttons & BUTTON_RIGHT) {
desiredTime++;
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
}
if (buttons & BUTTON_SELECT) {
buttonPresses++;
if (buttonPresses == 4)
{buttonPresses = 0;} // rollover every third press
if (buttonPresses == 1 & buttonPresses != 2 & buttonPresses != 3 & buttonPresses != 0); {
if (lastPressCount not_eq buttonPresses) {
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
//to scroll text
delay(2000); //time until scroll starts
//lcd.setCursor(16,1);
for(int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
delay(10); //velocity of scroll
}
lcd.setCursor(16,0); //where new text prints
lcd.print("Press SELECT");
lcd.setCursor(16,1);
lcd.print("to start brewing");
//scroll back
delay(2000); //time until scroll starts
for(int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayRight();
delay(1); //velocity of scroll
}
lcd.print("Temp: ");
lcd.setCursor(6, 0);
lcd.print(desiredTemp);
lcd.setCursor(9,0);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.setCursor(6,1);
lcd.print(desiredTime);
lcd.setCursor(9,1);
lcd.print("min");
}
}
if (buttonPresses == 2) {
if (lastPressCount not_eq buttonPresses) {
lcd.clear();
lcd.setCursor(0,0);
delay(250);
lcd.print("Heating");
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.setCursor(6,1);
lcd.print(Temperature); //prints temperature
}
}
if (buttonPresses == 3) {
if (lastPressCount not_eq buttonPresses) {
lcd.clear();
lcd.setCursor(0,0);
delay(250);
lcd.print("Mashing");
}
}
lastPressCount = buttonPresses; // track last press count
}
}
}