I have the Arduino uno with rgb lcd shield. there is a very weird behavior with the buttons in one specific function.
The function is called yes/no. It displays a message on the screen (working) the user can select answer yes/no with the buttons up/down/left/right and approve the answer by pressing select button.
The function is as follows:
bool yesno(String message)
{
//Serial.println("asking yesno question " + message);
bool answer = false;
bool answerSelected = false;
setColor('r');
setText(message+'?', "no");
while (!answerSelected)
{
uint8_t buttons = lcd.readButtons();
if (buttons){
if (buttons &(BUTTON_UP || BUTTON_DOWN || BUTTON_RIGHT || BUTTON_LEFT)) {
if (answer) {
answer = false;
setColor('r');
setText(message+'?', "no");
Serial.println(answer);
}
else {
answer = true;
setColor('g');
setText(message+'?', "yes");
Serial.println(answer);
}
}
else if (buttons & BUTTON_SELECT) {
setColor('w');
answerSelected = true;
Serial.println("selected ");
Serial.println(answer);
return answer;
}
}
delay(50);
}
}
for some reason, when pressing left/right/up/down, nothing happens. When pressing select, instead it executes function up/down/left/right
using if (buttons &&(BUTTON_UP || BUTTON_DOWN || BUTTON_RIGHT || BUTTON_LEFT))
instead, the buttons left/right/up/down work as intended but the button select still acts as the other buttons
similar code for a menu works as intended:
void InitializeMenu()
{
// initialize
Serial.println("entering menu");
setColor('w');
while (!exitMenu)
{
if (menuItem != selectedItem)
{
Serial.println("switching menu to: "+menuItems[menuItem]);
setText("Menue", menuItems[menuItem]);
selectedItem = menuItem;
}
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
menuItem--;
Serial.println("menu up");
Serial.println(menuItem);
Serial.println(menuSize);
if (menuItem < 0)
{
menuItem = menuSize;
Serial.println("start of menu going to end");
}
}
if (buttons & BUTTON_DOWN) {
menuItem++;
Serial.println("menu down");
Serial.println(menuItem);
Serial.println(menuSize);
if (menuItem > menuSize) {
menuItem = 0;
Serial.println("end of menu going to start");
}
}
if (buttons & BUTTON_SELECT) {
Serial.println("enter");
if (menuItem == menuSize) exitMenu = true;
}
delay(50);
}
}
I want to keep the code as short and simple as possible due to very limited space on the arduino uno.
doesn't do what you expect it to do.
||
is logicalor
, and the entire expression evaluates totrue
. To get a bitmask, change it to bitwiseor
(|
):