I have some basic programming experience but I am a novice at both C and Arduino.
I messed around with the example LCD code to try to move a character around with the onboard keypad. I also wanted the actual move to occur only once the button is released, like you would expect from a keypad.
The code works, but I have a feeling all the empty WHILE loops inside the main loop() function I used are not the best practice.. :)
How did I do? Is there a more convenient/elegant solution I missed and can learn from?
Thanks, and sorry for the messy code!
void loop()
{
lcd.clear();
lcd.setCursor(posCol,posRow); //initialized beforehand
lcd.print("O"); //random char to move
lcdOld = btnNONE;
while (read_LCD_buttons() == btnNONE) //do nothing while nothing is pressed
{
}
lcdOld = read_LCD_buttons();
while (read_LCD_buttons() == lcdOld) //do nothing while button is down
{
}
switch (lcdOld) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
posCol++;
break;
}
case btnLEFT:
{
posCol--;
break;
}
case btnUP:
{
if (posRow == 1)
{
posRow = 0;
break;
}
else
{
break;
}
}
case btnDOWN:
{
if (posRow == 0)
{
posRow = 1;
break;
}
else
{
break;
}
}
case btnSELECT:
{
break;
}
case btnNONE:
{
break;
}
}
}
read_LCD_buttons()
already takes care of that)