Hello everyone
I've been coding for the past couple of days and I seem to be stuck and can't seem to see the light. Your assistance in pointing me in the correct direction will be greatly appreciated.
I've coded a menu using the case statement and loads of functions to make it look nice and pretty. The main menu screen works fine (I think it works fine since the loop is only looping back to this menu) but as soon as I try to go into the sub menu I run into trouble. I've setup a counter to count my enter and escape button presses to go in and out of the sub menu and this works but the problem is that if I use a if statement to test the counter and to go to the sub menu then the screen toggles between the main menu and the sub menu constantly. When I press the ESC button it works nicely and goes back to the main menu. I'm not sure how to get around this but I've tried changing the if statement to a while statement which works wonderfully but when I call the functions to test the button presses while using the while it is unresponsive.
The problem area is found in the void loop, if statement.
Your feedback will be greatly appreciated as I'm at the stage of pulling out some hair here :~
Code as follows:
#include <glcd.h> // include the library header
#include <fonts/allFonts.h> // include the Fonts
const int ESC_Button = 2;
const int UP_Button = 3;
const int DWN_Button = 12;
const int ENT_Button = 13;
const int ledPin = 13; // the number of the LED pin
int ESC_Button_State = 0;
int UP_Button_State = 0;
int DWN_Button_State = 0;
int ENT_Button_State = 0;
int UP_DOWN_Selector = 1;
int ENT_ESC_Selector = 1;
void setup()
{
GLCD.Init(NON_INVERTED); // Initialize the GLCD
GLCD.ClearScreen();
GLCD.Init();
pinMode ( ESC_Button, INPUT );
pinMode ( UP_Button, INPUT );
pinMode ( DWN_Button, INPUT );
pinMode ( ENT_Button, INPUT );
pinMode(ledPin, OUTPUT);
}
void loop()
{
ESC_Button_State = digitalRead(ESC_Button);
UP_Button_State = digitalRead(UP_Button);
DWN_Button_State = digitalRead(DWN_Button);
ENT_Button_State = digitalRead(ENT_Button);
Draw_Menu_Blocks();
Test_UP_DOWN_Counter();
Test_ENT_ESC_Counter_Value();
Main_Menu_Case_Switch();
/* if( ENT_ESC_Selector == 2 )
{
GLCD.ClearScreen();
}
*/
if( ENT_ESC_Selector == 0 ) // tried to use a while here as well.
{
UP_DOWN_Selector = 1;
Test_UP_DOWN_Counter();
Test_ENT_ESC_Counter_Value();
Sub_Menu1_Case_Switch();
}
}
/****************************************************************************************************************************************************************************
************************************** Menu Case Switch - Used to draw sub menu ********************************************************
****************************************************************************************************************************************************************************/
void Main_Menu_Case_Switch()
{
switch ( UP_DOWN_Selector )
{
case 1:
Menu_Line1_Highlighted();
Menu_Line2_Not_Highlighted();
Menu_Line3_Not_Highlighted();
Menu_Line4_Not_Highlighted();
break; // Break used to break main menu
case 2:
Menu_Line1_Not_Highlighted();
Menu_Line2_Highlighted();
Menu_Line3_Not_Highlighted();
Menu_Line4_Not_Highlighted();
break;
case 3:
Menu_Line1_Not_Highlighted();
Menu_Line2_Not_Highlighted();
Menu_Line3_Highlighted();
Menu_Line4_Not_Highlighted();
break;
case 4:
Menu_Line1_Not_Highlighted();
Menu_Line2_Not_Highlighted();
Menu_Line3_Not_Highlighted();
Menu_Line4_Highlighted();
break;
}
}
/****************************************************************************************************************************************************************************
************************************** Sub Menu1 Case Switch - Used to draw sub menu ********************************************************
****************************************************************************************************************************************************************************/
void Sub_Menu1_Case_Switch()
{
switch ( UP_DOWN_Selector )
{
case 1:
Sub_Menu1_Line1_Highlighted();
Sub_Menu1_Line2_Not_Highlighted();
Sub_Menu1_Line3_Not_Highlighted();
Sub_Menu1_Line4_Not_Highlighted();
break;
case 2:
Sub_Menu1_Line1_Not_Highlighted();
Sub_Menu1_Line2_Highlighted();
Sub_Menu1_Line3_Not_Highlighted();
Sub_Menu1_Line4_Not_Highlighted();
break;
case 3:
Sub_Menu1_Line1_Not_Highlighted();
Sub_Menu1_Line2_Not_Highlighted();
Sub_Menu1_Line3_Highlighted();
Sub_Menu1_Line4_Not_Highlighted();
break;
case 4:
Sub_Menu1_Line1_Not_Highlighted();
Sub_Menu1_Line2_Not_Highlighted();
Sub_Menu1_Line3_Not_Highlighted();
Sub_Menu1_Line4_Highlighted();
break;
}
}
/****************************************************************************************************************************************************************************
************************************** Code to draw up the menu ********************************************************
****************************************************************************************************************************************************************************/
void Draw_Menu_Blocks()
{
// Part above menu
GLCD.DrawRect( 0, 0, 127, 63); // draw an open rectangle that spans the extremties of the LCD
GLCD.FillRect( 47, 2, 2, 8); // Klein reg hoek om die Text beter te laat lyk, aan die linker kant
GLCD.DrawLine( 49, 2, 79, 2); // Lyn bo die text om dit beter te laat lyk
GLCD.DrawLine( 0, 12, 126, 12); // Lyn om 'n blokkie te maak
GLCD.SelectFont(System5x7, WHITE);
GLCD.CursorToXY(GLCD.Width/2 - 14, 3);
GLCD.print("MENU:");
GLCD.DrawLine( 0, 51, 126, 51); // Lyn om 'n blokkie te maak
//****************************************************************************************************************************************************************************
//Call menu line functions
//****************************************************************************************************************************************************************************
// Part below menu
GLCD.DrawLine( 31, 52, 31, 62); // Lines to draw four blocks at the bottom.
GLCD.DrawLine( 63, 52, 63, 62);
GLCD.DrawLine( 94, 52, 94, 62);
// Menu buttons at the bottom of the display.
GLCD.SelectFont(System5x7, BLACK);
GLCD.CursorToXY(8, 54); // Write ESC in the first block
GLCD.print("ESC");
GLCD.DrawLine( 47, 54, 47, 60); // Draw Up arrow in second block from left
GLCD.DrawLine( 46, 55, 48, 55);
GLCD.DrawLine( 45, 56, 49, 56);
GLCD.DrawLine( 78, 54, 78, 60); // Draw Down arrow in third block from left
GLCD.DrawLine( 77, 59, 79, 59);
GLCD.DrawLine( 76, 58, 80, 58);
GLCD.CursorToXY(103, 54); // Write ENT in thte last block
GLCD.print("ENT");
}