Hello,
I am having difficulty developing a program that shows a selection of options on a LCD screen and can be chosen through buttons.
My problem is that on the LCD screen the information keeps scrolling through without stopping. If I push a button it goes to a different screen and prints the correct information but continues to repeat/scroll.
My hardware is as follows:
Rugged Arduino: Ruggeduino-SE "Special Edition" — Rugged CircuitsRugged Arduino
Serial Enabled 20x4 LCD - Black on Green 5V: Serial Enabled 20x4 LCD - Black on Green 5V - LCD-09568 - SparkFun Electronics
I have reduced the code of the switch statement some since essentially all the cases are the same just different options/button pushes.
I have looked at menu selection programs for arduino and they all seem to use switch statements.
My wiring
LCD Rugged Arduino
RX TX
GND GND
VDD +5v
#include <stdlib.h>
#include "mcp_can.h"
#include <SPI.h>
#include "can_ext.h"
int curr_screen = 0;
int prev_screen = 0;
int temp_gear_up = 3;
int temp_gear_down = 3;
int desired_shift_gear_up;
int desired_shift_gear_down;
bool auto_shift_type;
char sString[80];
const String Blank = " ";
//buttons
const int Back = 9;
const int Enter = A0;
const int Reset = 2;
const int Button1 = 3;
const int Button2 = 4;
const int Button3 = 5;
const int Button4 = 6;
const int Button5 = 12;
//switch statements
const int main_menu = 6;
const int auto_menu = 7;
unsigned char stmp[8] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37};
// ------------------------------------------------------------------------
// SYSTEM: Setup routine runs on power-up or reset
// ------------------------------------------------------------------------
// LCD Display Software
void DisplayXY(uint8_t line, uint8_t column)
{
char A = 0;
A = 0xFE;
Serial.print(A);
A = 0x45;
Serial.print(A);
switch(line)
{
case 1:
A = 0x0;
break;
case 2:
A = 0x40;
break;
case 3:
A = 0x14;
break;
case 4:
A = 0x54;
break;
default:
break;
}
A = A + column - 1;
Serial.print(A);
delay(2);
}
// Convert String HEX to Integer
int strhex2int(char *s)
{
int x = 0;
for(;;) {
char c = *s;
if (c >= '0' && c <= '9') {
x *= 16;
x += c - '0';
}
else if (c >= 'A' && c <= 'F') {
x *= 16;
x += (c - 'A') + 10;
}
else break;
s++;
}
return x;
}
void setup() {
digitalWrite(Enter ,INPUT_PULLUP);
pinMode(Back ,INPUT_PULLUP);
pinMode(Button1 ,INPUT_PULLUP);
pinMode(Button2 ,INPUT_PULLUP);
pinMode(Button3 ,INPUT_PULLUP);
pinMode(Button4 ,INPUT_PULLUP);
pinMode(Button5 ,INPUT_PULLUP);
pinMode(Reset ,INPUT_PULLUP);
Serial.begin(9600);
}// end setup
// ------------------------------------------------------------------------
// Main Loop - Arduino Entry Point
// ------------------------------------------------------------------------
void loop()
{
Statedetermination();
}// end loop
void Statedetermination()
{
trans_current_gear = 8;
ia_lock_state = 1;
axle_lock_state = 0;
axle_state = 620;
vehicle_speed_flag = 1;
//Serial.print("Current_Screen: "); Serial.print(curr_screen); Serial.print("\n\r");
switch (curr_screen)
{
case main_menu:
{
DisplayXY(1,1);
Serial.print("B1: AUTOMATIC ");
DisplayXY(2,1);
Serial.print("B2: MANUAL ");
DisplayXY(3,1);
Serial.print("B3: DIAGNOSTICS ");
DisplayXY(4,1);
Serial.print("Back For Prev Menu ");
if (digitalRead(Back) == LOW || digitalRead(Reset) == LOW)
{
curr_screen = main_menu;
prev_screen = 0;
}
else if (digitalRead(Button1) == LOW)
{
curr_screen = auto_menu;
prev_screen = main_menu;
}
else if (digitalRead(Button2) == LOW)
{
curr_screen = manual_home;
}
else if (digitalRead(Button3) == LOW)
{
curr_screen = diagnostics_home;
}
delay(200);
break;
}
case auto_menu:
{
while(curr_screen == auto_menu)
{
DisplayXY(1,1);
Serial.print("SELECT STRATEGY ");
DisplayXY(2,1);
Serial.print("B1: SINGLE GEAR ");
DisplayXY(3,1);
Serial.print("B2: SPLIT GEAR ");
DisplayXY(4,1);
Serial.print("Back For Prev Menu ");
if (digitalRead(Back) == LOW)
{
curr_screen = prev_screen;
}
else if (digitalRead(Button1) == LOW)
{
curr_screen = auto_menu_single;
auto_shift_type = 0;
prev_screen = auto_menu;
}
else if (digitalRead(Button2) == LOW)
{
curr_screen = auto_menu_split_up;
auto_shift_type = 1;
prev_screen = auto_menu;
}
else if (digitalRead(Reset) == LOW)
{
curr_screen = main_menu;
prev_screen = 0;
}
}
delay(200);
break;
}
default:
{
DisplayXY(1,1);
Serial.print("B1: AUTOMATIC ");
DisplayXY(2,1);
Serial.print("B2: MANUAL ");
DisplayXY(3,1);
Serial.print("B3: DIAGNOSTICS ");
DisplayXY(4,1);
Serial.print("Back For Prev Menu ");
if (digitalRead(Back) == LOW || digitalRead(Reset) == LOW)
{
curr_screen = main_menu;
prev_screen = 0;
}
else if (digitalRead(Button1) == LOW)
{
curr_screen = auto_menu;
prev_screen = main_menu;
}
else if (digitalRead(Button2) == LOW)
{
curr_screen = manual_home;
}
else if (digitalRead(Button3) == LOW)
{
curr_screen = diagnostics_home;
}
delay(200);
break;
}
}
}
}