switch case statement problem

Hi All,

I'm need help with the switch case statement for below code. I'm trying to create a menu screen for my Aquarium controller.
There are 2 issues with the code:
Issue 1:
when I run the below code it is running in sequence means I have to touch on the button 1 than 2 than 3 than 4 and so on. What I want is that I should be able to click on any button without any sequence.

Issue 2:
Also it is running only once. If I touch a button than I'm not able to click it again.

please guide me in right direction.

byte dispScreen=0; // it is placed on the top before setup loop.

    if (myTouch.dataAvailable())  {
      
    myTouch.read();
    x=myTouch.getX();
    y=myTouch.getY();
    setx=myTouch.getX();
    sety=myTouch.getY();
    setx1=myTouch.getX();
    sety1=myTouch.getY();


    switch (dispScreen) {
    case 0:
		if ((x>=mainBut[0]) && (x<=mainBut[2]) && (y>=mainBut[1]) && (y<=mainBut[3]))       
            {
				clearScreen();
				mainScreen();
				waitForIt(mainBut[0], mainBut[1], mainBut[2], mainBut[3]);
				myGLCD.print("MAIN", 22, 222, 1);
				dispScreen = 1;
            }
			
    break;
    case 1:
		if ((x>=ledBut[0]) && (x<=ledBut[2]) && (y>=ledBut[1]) && (y<=ledBut[3]))       
            {
				clearScreen();
				LEDScreen();
				waitForIt(ledBut[0], ledBut[1], ledBut[2], ledBut[3]);
				myGLCD.print("LED", 75, 222, 1);
				dispScreen = 2;
            }
			
	break;
    case 2:	
		if ((x>=tempBut[0]) && (x<=tempBut[2]) && (y>=tempBut[1]) && (y<=tempBut[3]))       
            {
				clearScreen();
				tempDisplay();
				waitForIt(tempBut[0], tempBut[1], tempBut[2], tempBut[3]);
				myGLCD.print("TEMP", 122, 222, 1);
				dispScreen = 3;
            }
			
	break;			
	case 3:
		if ((x>=pHBut[0]) && (x<=pHBut[2]) && (y>=pHBut[1]) && (y<=pHBut[3]))       
            {
				clearScreen();
				backgroundScreen();
				waitForIt(165, 219, 205, 235);
				myGLCD.print("pH", 100, 100, 1);
                dispScreen = 4;
            }
			
	break;
	case 4:
		if ((x>=settBut[0]) && (x<=settBut[2]) && (y>=settBut[1]) && (y<=settBut[3]))       
            {
				clearScreen();		
				generalSettings();
				waitForIt(settBut[0], settBut[1], settBut[2], settBut[3]);
				myGLCD.print("SETTINGS", 230, 222, 1);
				dispScreen = 5;
			}
						
	break;
	case 5:
		if (dispScreen = 5){
			if ((setx>=dtSet[0]) && (setx<=dtSet[2]) && (sety>=dtSet[1]) && (sety<=dtSet[3]))       
			{
				clearScreen();				
				dateTimeSettings();
				dispScreen = 6;
			}
			if ((setx>=mlSet[0]) && (setx<=mlSet[2]) && (sety>=mlSet[1]) && (sety<=mlSet[3]))       
			{
				clearScreen();
				moonlight();
				dispScreen = 7;
			}
			if ((setx>=ledSet[0]) && (setx<=ledSet[2]) && (sety>=ledSet[1]) && (sety<=ledSet[3]))       
			{
				clearScreen();	
				backgroundScreen();			
				myGLCD.print("LED Settings", 100, 100, 1);
				dispScreen = 8;
			}
			if ((setx>=tempSet[0]) && (setx<=tempSet[2]) && (sety>=tempSet[1]) && (sety<=tempSet[3]))       
			{
				clearScreen();
				backgroundScreen();				
				myGLCD.print("TEMP SETTING", 100, 100, 1);
				dispScreen = 9;
			}
			if ((setx>=alaSet[0]) && (setx<=alaSet[2]) && (sety>=alaSet[1]) && (sety<=alaSet[3]))       
			{
				clearScreen();
				backgroundScreen();				
				myGLCD.print("ALARM SETTING", 100, 100, 1);
				dispScreen = 10;
			}
	break;
	case 6:
		if (dispScreen = 6){
			if ((setx1>=savBut[0]) && (setx1<=savBut[2]) && (sety1>=savBut[1]) && (sety1<=savBut[3]))       
			{
				clearScreen();				
				generalSettings();
			}

			if ((setx1>=canBut[0]) && (setx1<=canBut[2]) && (sety1>=canBut[1]) && (sety1<=canBut[3]))       
			{
				clearScreen();				
				generalSettings();
			}
		}
				
	break;
	case 8:
		if (dispScreen = 7){
			if ((setx1>=savBut[0]) && (setx1<=savBut[2]) && (sety1>=savBut[1]) && (sety1<=savBut[3]))       
			{
				clearScreen();				
				generalSettings();
			}
			if ((setx1>=canBut[0]) && (setx1<=canBut[2]) && (sety1>=canBut[1]) && (sety1<=canBut[3]))       
			{
				clearScreen();				
				generalSettings();
			}
		}
				
	break;						
 
  }
  delay(100);
}
}

regards
BW

This is not all of your code.
Post all of it to have all questions answered.

Sorry for not posting full code.

#include <UTFT.h>
#include <UTouch.h>

extern uint8_t SmallFont[];
#define LARGE true
#define SMALL false

UTFT	myGLCD(ITDB32S,38,39,40,41);
UTouch	myTouch(6,5,4,3,2);
int i;
int x, y, setx, sety, setx1, sety1;
byte dispScreen=0;
unsigned long prevMillis05 = 0;
unsigned long prevMillis1 = 0;
unsigned long prevMillis5 = 0;
unsigned long prevMillis60 = 0;
unsigned long prevMillis600 = 0;

const int savBut[]= {40, 187, 130, 209};
const int canBut[]= {185, 187, 280, 209};

const int mainBut[] = {15, 219, 55, 235};
const int ledBut[] = {65, 219, 105, 235};
const int tempBut[] = {115, 219, 155, 235};
const int pHBut[] = {165, 219, 205, 235};	
const int settBut[] = {215, 219, 300, 235};
const int dtSet[] = {25, 35, 295, 60};	
const int mlSet[] = {25, 70, 295, 95};
const int ledSet[] = {25, 105, 295, 130};
const int tempSet[] = {25, 140, 295, 165};	
const int alaSet[] = {25, 175, 295, 200};

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch();
  Serial.begin(9600);
}

void loop()
{
	mainScreen();
while (true)
  {
    if (myTouch.dataAvailable())  {
      processMyTouch();
    }
  }  
}

void backgroundScreen() {
//HEADER
myGLCD.setColor(255, 0, 0);
myGLCD.setBackColor(255, 0, 0);
myGLCD.fillRect(1, 1, 319, 239);
myGLCD.setColor(255, 255, 255);
myGLCD.print("DATE", 5, 5, 1);
myGLCD.print("CONTROLLER V1", CENTER, 5, 1);
myGLCD.print("TIME", RIGHT, 5, 1);
//BODY
myGLCD.setColor(153, 50, 204);
myGLCD.setBackColor(153, 50, 204);
myGLCD.fillRect(1, 25, 319, 221);
myGLCD.setColor(255, 255, 255);
//FOOTER
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(0, 0, 255);
myGLCD.fillRoundRect (15, 219, 55, 235);
myGLCD.fillRoundRect (65, 219, 105, 235);
myGLCD.fillRoundRect (115, 219, 155, 235);
myGLCD.fillRoundRect (165, 219, 205, 235);
myGLCD.fillRoundRect (215, 219, 300, 235);
myGLCD.setColor(255, 255, 255);
myGLCD.print("MAIN", 22, 222, 1);
myGLCD.print("LED", 75, 222, 1);
myGLCD.print("TEMP", 122, 222, 1);
myGLCD.print("pH", 178, 222, 1);
myGLCD.print("SETTINGS", 230, 222, 1);
myGLCD.setColor(153, 50, 204);
myGLCD.setBackColor(153, 50, 204);
myGLCD.fillRect(15, 219, 300, 221);
myGLCD.setColor(255, 255, 255);
}

void hlightUpButton(int x, int y)
{
  myGLCD.setColor(0, 0, 125);
  for (int i=0; i<15; i++)
     myGLCD.drawLine(x+4+(i/1.5), y+17-i, x+25-(i/1.5), y+17-i);
  while (myTouch.dataAvailable()) {
    myTouch.read(); 
  }
  myGLCD.setColor(0, 255, 255);
  for (int i=0; i<15; i++)
     myGLCD.drawLine(x+4+(i/1.5), y+17-i, x+25-(i/1.5), y+17-i);
}

void hlightDownButton(int x, int y)
{
  myGLCD.setColor(0, 0, 125);
  for (int i=0; i<15; i++)
     myGLCD.drawLine(x+4+(i/1.5), y+7+i, x+25-(i/1.5), y+7+i);
  while (myTouch.dataAvailable()) {
    myTouch.read(); 
  }
  myGLCD.setColor(0, 255, 255);
  for (int i=0; i<15; i++)
     myGLCD.drawLine(x+4+(i/1.5), y+7+i, x+25-(i/1.5), y+7+i);
}

void MyDelay(int x, int y)
{
  myGLCD.print("0", x, y);
  long  now = millis();
  long  end = now + 500;
  while(millis() < end)
  {
    //myGLCD.printNumI(i, x, y);
  } 
}

void waitForIt(int x2, int y2, int x3, int y3)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.drawRoundRect (x2, y2, x3, y3);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(153, 50, 204);
  myGLCD.setBackColor(153, 50, 204);
  myGLCD.fillRoundRect (x2, y2, x3, y3);
  myGLCD.drawRoundRect (x2, y2, x3, y3);
  myGLCD.setColor(255, 255, 255);
}

void printButton(char* text, int x1, int y1, int x2, int y2, byte cr, byte cg, byte cb, byte br, byte bg, byte bb)
{
  int stl = strlen(text);
  int fx, fy;
  fx = x1+(((x2 - x1)-(stl*8))/2);
  fy = y1+(((y2 - y1-1)-8)/2);
  myGLCD.setColor(cr, cg, cb);
  myGLCD.setBackColor(cr, cg, cb);
  myGLCD.fillRoundRect(x1, y1, x2, y2);
  myGLCD.setColor(br, bg, bb);
  myGLCD.print(text, fx, fy);
 }

void generalSettings(){
myGLCD.clrScr();
backgroundScreen();
myGLCD.setColor(0, 0, 255);
myGLCD.setBackColor(0, 0, 255);
myGLCD.fillRoundRect (25, 35, 295, 60);
myGLCD.fillRoundRect (25, 70, 295, 95);
myGLCD.fillRoundRect (25, 105, 295, 130);
myGLCD.fillRoundRect (25, 140, 295, 165);
myGLCD.fillRoundRect (25, 175, 295, 200);
myGLCD.setColor(255, 255, 255);
myGLCD.print("DATE & TIME SETTINGS", CENTER, 40, 1);
myGLCD.print("MOONLIGHT SETTINGS", CENTER, 75, 1);
myGLCD.print("LED SETTINGS", CENTER, 110, 1);
myGLCD.print("TEMPERATURE SETTINGS", CENTER, 145, 1);
myGLCD.print("ALARM SETTINGS", CENTER, 180, 1);
}

void mainScreen(){
myGLCD.clrScr();
backgroundScreen();
myGLCD.print("MAIN SCREEN", 100, 100, 1);
}

void dateTimeSettings(){
myGLCD.clrScr();
backgroundScreen();
myGLCD.print("DATE & TIME SETTINGS", 100, 100, 1);
printButton("SAVE", 40, 187, 130, 209, 0, 0, 255, 255, 255, 255);
printButton("cancel", 185, 187, 280, 209, 0, 0, 255, 255, 255, 255);
}

void LEDScreen(){
myGLCD.clrScr();
backgroundScreen();
myGLCD.print("LED SCREEN", 100, 100, 1);
}
	
void moonlight(){
myGLCD.clrScr();
backgroundScreen();
myGLCD.print("MOON LIGHT SETTINGS", 100, 100, 1);
printButton("SAVE", 40, 187, 130, 209, 0, 0, 255, 255, 255, 255);
printButton("cancel", 185, 187, 280, 209, 0, 0, 255, 255, 255, 255);
}


void tempDisplay(){
       myGLCD.clrScr();
       backgroundScreen();
       myGLCD.print("TEMP DISPLAY", 100, 100, 1);
}
void clearScreen()
{
 myGLCD.setColor(0, 0, 0);
 myGLCD.fillRect(1, 1, 318, 239); 
}
void processMyTouch()
{
   myTouch.read();
   x=myTouch.getX();
   y=myTouch.getY();
   setx=myTouch.getX();
   sety=myTouch.getY();
   setx1=myTouch.getX();
   sety1=myTouch.getY();
   prevMillis600 = millis();
   switch (dispScreen) {
   case 0: 
if ((x>=mainBut[0]) && (x<=mainBut[2]) && (y>=mainBut[1]) && (y<=mainBut[3])) 
           {
	clearScreen();
	mainScreen();
	waitForIt(mainBut[0], mainBut[1], mainBut[2], mainBut[3]);
	myGLCD.print("MAIN", 22, 222, 1);
           dispScreen = 1;
           }
   break;
   case 1:
if ((x>=ledBut[0]) && (x<=ledBut[2]) && (y>=ledBut[1]) && (y<=ledBut[3])) 
           {
           clearScreen();
	LEDScreen();
	waitForIt(ledBut[0], ledBut[1], ledBut[2], ledBut[3]);
	myGLCD.print("LED", 75, 222, 1);
           dispScreen = 2;
           }
break;
   case 2:	
if ((x>=tempBut[0]) && (x<=tempBut[2]) && (y>=tempBut[1]) && (y<=tempBut[3])) 
           {
           clearScreen();
           tempDisplay();
	waitForIt(tempBut[0], tempBut[1], tempBut[2], tempBut[3]);
	myGLCD.print("TEMP", 122, 222, 1);
           dispScreen = 3;
           }
break;			
case 3:
if ((x>=pHBut[0]) && (x<=pHBut[2]) && (y>=pHBut[1]) && (y<=pHBut[3])) 
           {
	clearScreen();
	backgroundScreen();
	waitForIt(165, 219, 205, 235);
	myGLCD.print("pH", 100, 100, 1);
           dispScreen = 4;
           }
break;
case 4:
if ((x>=settBut[0]) && (x<=settBut[2]) && (y>=settBut[1]) && (y<=settBut[3])) 
           {
    clearScreen();
           generalSettings();
	waitForIt(settBut[0], settBut[1], settBut[2], settBut[3]);
	myGLCD.print("SETTINGS", 230, 222, 1);
           dispScreen = 5;
	}
break;
case 5:
if (dispScreen = 5){
if ((setx>=dtSet[0]) && (setx<=dtSet[2]) && (sety>=dtSet[1]) && (sety<=dtSet[3])) 
{
           clearScreen();
           dateTimeSettings();
           dispScreen = 6;
}
if ((setx>=mlSet[0]) && (setx<=mlSet[2]) && (sety>=mlSet[1]) && (sety<=mlSet[3])) 
{
clearScreen();
           moonlight();
           dispScreen = 7;
}
if ((setx>=ledSet[0]) && (setx<=ledSet[2]) && (sety>=ledSet[1]) && (sety<=ledSet[3])) 
{
           clearScreen();
           backgroundScreen();
           myGLCD.print("LED Settings", 100, 100, 1);
           dispScreen = 8;
}
if ((setx>=tempSet[0]) && (setx<=tempSet[2]) && (sety>=tempSet[1]) && (sety<=tempSet[3])) 
{
           clearScreen();
           backgroundScreen();
           myGLCD.print("TEMP SETTING", 100, 100, 1);
           dispScreen = 9;
}
if ((setx>=alaSet[0]) && (setx<=alaSet[2]) && (sety>=alaSet[1]) && (sety<=alaSet[3])) 
{
            clearScreen();
            backgroundScreen();				
            myGLCD.print("ALARM SETTING", 100, 100, 1);
            dispScreen = 10;
}
break;
case 6:
if (dispScreen = 6){
	if ((setx1>=savBut[0]) && (setx1<=savBut[2]) && (sety1>=savBut[1]) && (sety1<=savBut[3])) 
	{
             clearScreen();				
             generalSettings();
	}

	if ((setx1>=canBut[0]) && (setx1<=canBut[2]) && (sety1>=canBut[1]) && (sety1<=canBut[3])) 
	{
           clearScreen();				
           generalSettings();
	}
}
break;
case 7:
if (dispScreen = 7){
	if ((setx1>=savBut[0]) && (setx1<=savBut[2]) && (sety1>=savBut[1]) && (sety1<=savBut[3])) 
	{
           clearScreen();				
           generalSettings();
	}
	if ((setx1>=canBut[0]) && (setx1<=canBut[2]) && (sety1>=canBut[1]) && (sety1<=canBut[3])) 
	{
            clearScreen();				
            generalSettings();
	}
}
break;						
 }
  //delay(100);
}   						
}

bhaskarwankhede:
Sorry for not posting full code.

void loop()

{
mainScreen();
while (true)



while WHAT is true??? If you don't specify it, the while loop will continue for ever and ever and ever and ever.....


void processMyTouch()
{
.
  switch (dispScreen) {
  case 0:
.
   break;
   case 1:
.
  break;
   case 2:
.
  break;
  case 3:
.
  break;
  case 4:
.
  break;
  case 5:
    if (dispScreen = 5){


Ignoring the syntax error, how could dispScreen be anything other than 5 here?


case 6:
    if (dispScreen = 6){


Ignoring the syntax error, how could dispScreen be anything other than 6 here?


case 7:
    if (dispScreen = 7){


Ignoring the syntax error, how could dispScreen be anything other than 7 here?

It looks like you're setting dispScreen to a new number at the end of each of those first few cases. I can't find where you're setting it to anything that is coming from the screen. So I wouldn't think it would matter which number you select, it is going to run through 0 and in case zero it gets set to 1, then the next time a reading is available it will run case 1 where it gets set to 2 and then case 2 and so on.

if REASSIGN: if (dispScreen = 6)
if COMPARE: if(dispScreen == 6)

Which did you want? Your code reassigns the value of dispScreen at each if statement.

JohnHoward:
if REASSIGN: if (dispScreen = 6)
if COMPARE: if(dispScreen == 6)

Which did you want? Your code reassigns the value of dispScreen at each if statement.

Hence my "Ignoring the syntax error". But those if statements are redundant. Switch case will only take the program to, say, case 6 if dispScreen is already == 6. There's no need to check dispScreen's value again.

Delta_G:
It looks like you're setting dispScreen to a new number at the end of each of those first few cases. I can't find where you're setting it to anything that is coming from the screen. So I wouldn't think it would matter which number you select, it is going to run through 0 and in case zero it gets set to 1, then the next time a reading is available it will run case 1 where it gets set to 2 and then case 2 and so on.

I think I know where the problem is. I will try to modify it.
I'm very new to coding & this is my first time coding. Most of the codes which I have written are modified codes available on internet.
Any pointer what need to be modify will really help.

Oh, ok. I was ignoring the } syntax mismatch. But you have

if (dispScreen = 5){

ending } right above the delay(100), so it encloses the cases 6 through 9.

Is that a syntax error to ignore or is that the problem?

JohnHoward:
Oh, ok. I was ignoring the } syntax mismatch.

I hadn't spotted that one. We appear to be talking at crossed purposes.

But you have

if (dispScreen = 5){

ending } right above the delay(100), so it encloses the cases 6 through 9.

Is that a syntax error to ignore or is that the problem?

The mismatch is another problem.

I guess it's time to sprinkle Serial.Print() statements throughout the case statement, so you can see where it's going when you punch a button.

JohnHoward:
I guess it's time to sprinkle Serial.Print() statements throughout the case statement, so you can see where it's going when you punch a button.

working on it.

I'm now able to use the menu screen as I want. Below is the modified code. Please let me know if you still see any issue with the code.

    switch (dispScreen) {
      case 0:
		backgroundScreen();
		dispScreen=1;
		Serial.println(dispScreen);
      break;
      case 1:
		if ((x>=mainBut[0]) && (x<=mainBut[2]) && (y>=mainBut[1]) && (y<=mainBut[3]))       
			{
				clearScreen();
				mainScreen();
				waitForIt(mainBut[0], mainBut[1], mainBut[2], mainBut[3]);
				myGLCD.print("MAIN", 22, 222, 1);
				dispScreen = 1;
			}
		if ((x>=ledBut[0]) && (x<=ledBut[2]) && (y>=ledBut[1]) && (y<=ledBut[3]))       
			{
				clearScreen();
				LEDScreen();
				waitForIt(ledBut[0], ledBut[1], ledBut[2], ledBut[3]);
				myGLCD.print("LED", 75, 222, 1);
				dispScreen = 1;
			}
		if ((x>=tempBut[0]) && (x<=tempBut[2]) && (y>=tempBut[1]) && (y<=tempBut[3]))       
			{
				clearScreen();
				tempDisplay();
				waitForIt(tempBut[0], tempBut[1], tempBut[2], tempBut[3]);
				myGLCD.print("TEMP", 122, 222, 1);
				dispScreen = 1;
			}
		if ((x>=pHBut[0]) && (x<=pHBut[2]) && (y>=pHBut[1]) && (y<=pHBut[3]))       
			{
				clearScreen();
				backgroundScreen();
				waitForIt(165, 219, 205, 235);
				myGLCD.print("pH", 100, 100, 1);
				dispScreen = 1;
			}
		if ((x>=settBut[0]) && (x<=settBut[2]) && (y>=settBut[1]) && (y<=settBut[3]))       
			{
				clearScreen();		
				generalSettings();
				waitForIt(settBut[0], settBut[1], settBut[2], settBut[3]);
				myGLCD.print("SETTINGS", 230, 222, 1);
				dispScreen = 2;
			}
Serial.println(dispScreen);						
	break;
	case 2:
		if ((setx>=dtSet[0]) && (setx<=dtSet[2]) && (sety>=dtSet[1]) && (sety<=dtSet[3]))       
		{
			clearScreen();				
			dateTimeSettings();
			dispScreen = 3;
		}
		if ((setx>=mlSet[0]) && (setx<=mlSet[2]) && (sety>=mlSet[1]) && (sety<=mlSet[3]))       
		{
			clearScreen();
			moonlightSettings();
			dispScreen = 3;
		}
		if ((setx>=ledSet[0]) && (setx<=ledSet[2]) && (sety>=ledSet[1]) && (sety<=ledSet[3]))       
		{
			clearScreen();	
			backgroundScreen();
			ledSettings();			
			dispScreen = 3;
		}
		if ((setx>=tempSet[0]) && (setx<=tempSet[2]) && (sety>=tempSet[1]) && (sety<=tempSet[3]))       
		{
			clearScreen();
			backgroundScreen();	
			tempSettings();			
			dispScreen = 3;
		}
		if ((setx>=alaSet[0]) && (setx<=alaSet[2]) && (sety>=alaSet[1]) && (sety<=alaSet[3]))       
		{
			clearScreen();
			backgroundScreen();
			pHSettings();				
			dispScreen = 3;
		}
		if ((setx>=bacButt[0]) && (setx<=bacButt[2]) && (sety>=bacButt[1]) && (sety<=bacButt[3]))       
		{
			clearScreen();
			backgroundScreen();				
			dispScreen = 1;
		}
Serial.println(dispScreen);
	break;
	case 3:
		if ((setx1>=savBut[0]) && (setx1<=savBut[2]) && (sety1>=savBut[1]) && (sety1<=savBut[3]))       
		{
			clearScreen();				
			generalSettings();
			dispScreen = 2;
		}

		if ((setx1>=canBut[0]) && (setx1<=canBut[2]) && (sety1>=canBut[1]) && (sety1<=canBut[3]))       
		{
			clearScreen();				
			generalSettings();
			dispScreen = 2;
		}
Serial.println(dispScreen);	
	break;

  }

}