touch screen buttons

I have to create a weather station for my hnc electronics course using arduino
I have a tft touchscreen and want to create buttons, I have a start screen and I want a menu button at the bottom. most of what I have done so far is copy and paste. I can get the start page working. this just shows the title, my name and sensor names. at the bottom I have a rectangle with menu written in it. I have tried to make this a button by using parts of the examples that come with the libraries. but I cant get the button to work.

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

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];



UTFT        myGLCD(ITDB32S,38,39,40,41);  
UTouch      myTouch(6,5,4,3,2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);




void start()
{
   
//set colour for the title, display title and position on x axis(center) and position on y axis(0, 16)
    myGLCD.setColor(0, 0, 255);                          
    myGLCD.print("WEATHER STATION", CENTER, 0);
    myGLCD.print("PROJECT", CENTER, 16);  
//set colour to red, name and sensor names with positions 
    myGLCD.setColor(255, 0, 50);                        
    myGLCD.print("By Simon Bradley", CENTER, 32);    
    myGLCD.print("  -DHT22", CENTER, 48);
    myGLCD.print("  -BMP108", CENTER, 64);
    myGLCD.print("  -LDR", CENTER, 80);  
 //set colour to green, more names and positions  
    myGLCD.setColor(0, 255, 0);                          
    myGLCD.print("ARDUINO PROJECT", CENTER, 112);
    myGLCD.print("Engineering Science", CENTER, 144); 
//set colour to blue create menu button  
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (118, 170, 199, 199);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (118, 170, 199,199 );
    myGLCD.print("Menu", CENTER, 176);
    
    
    

}

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myButtons.setTextFont(BigFont);
  start();

}

void loop()
{
  int menu;
   menu = myButtons.addButton( 118,  170, 199,  199, "menu", 0);
  myButtons.drawButtons();
  
  while(1) 
  {
    if (myTouch.dataAvailable() == true)
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==menu)
        myGLCD.clrScr();
        myGLCD.setColor(255,255,255);
        myGLCD.print("MENU    ", CENTER, 0);
    }
  }
}

this is what I have so far. im new to this and understand some things, enough to create a start page. when I try to compile this it comes up with an error saying “pressed button not declared”. I don’t understand what the “true” coment means in this - if (myTouch.dataAvailable() == true)
im thinking that because of this - myTouch.dataAvailable()
I should use a myTouch.readData() or something like that?

any help would be great, I intend to document my project to help others so your help will go further!

happy new year

simon

@simon,
Check out "code tags" for keeping your embedded code neat... It us the little "#" in the edit menu... Just edit, hilight the code, click #.

Congrats on getting the home page complete. Rather than trying to work with too much code, is there not a simple example of the button function? If so, load the example, save as another filename, and play with the copy. Put some text around the button... Move the button... Add a second. Get familiar with the way the library works. Then leave this open and open another GUI window with your current code. The problem should be clearer then.

Ray

Hi, maybe I can help.

myTouch.dataAvailable() This is a function from your UTouch library and what this function does is it returns the state of the screen, whether if or not it was touched. So if you touch the screen, it sets a flag to TRUE letting the user know that the screen has been touched somewhere. If not touch, it simply returns false.

Now what the creator wrote, “if (myTouch.dataAvailable() == true)” what this is saying is basically what I just said, the IF statement is looking for the returned value the “condition” to be TRUE. He could just as well as “if (myTouch.dataAvailable() )” and it would mean the exact same thing.

Now your error stated “pressed button not declared” this is saying that the variable pressed_button was not declaired, so a simple int pressed_button; declaired either globally or in the void loop function, should fix that.

not there yet, I have upgraded my script a bit and some stuff works.
the start screen appears for a brief second, then the other buttons appear with the word “menu” at the top
then the buttons disappear and the titles of each sensor appear for a second then disappear.

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

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];



UTFT        myGLCD(ITDB32S,38,39,40,41);  
UTouch      myTouch(6,5,4,3,2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);


int pressed_button, butmenu, butdht, butbmp, butldr;

void start()
{
  myButtons.deleteAllButtons(); 
     //set colour for the title, display title and position on x axis(center) and position on y axis(0, 16)
   
    myGLCD.setColor(0, 0, 255);                          
    myGLCD.print("WEATHER STATION", CENTER, 0);
    myGLCD.print("PROJECT", CENTER, 16);  
//set colour to red, name and sensor names with positions 
    myGLCD.setColor(255, 0, 50);                        
    myGLCD.print("By Simon Bradley", CENTER, 32);    
    myGLCD.print("  -DHT22", CENTER, 48);
    myGLCD.print("  -BMP108", CENTER, 64);
    myGLCD.print("  -LDR", CENTER, 80);  
 //set colour to green, more names and positions  
    myGLCD.setColor(0, 255, 0);                          
    myGLCD.print("ARDUINO PROJECT", CENTER, 112);
    myGLCD.print("Engineering Science", CENTER, 144); 
     butmenu = myButtons.addButton( 10, 180, 300, 30, "Menu");
  myButtons.drawButtons();
  
  while (1)
{
  if (myTouch.dataAvailable() == true)
    
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butmenu)
    {
      menu();
     myButtons.deleteButton(butmenu);
    }
    }
}
}
    




void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myButtons.setTextFont(BigFont);
  

}

void loop()
{
     
  boolean default_colors = true;
  
 start();

 

}       
   

   
         
          

       
   void bmp() 
       {
          
          
           myGLCD.clrScr(); 
          myGLCD.print("BMP", CENTER, 0);
          delay(1000);
         
       } 
          
   void ldr()     
      {
         
         
           myGLCD.clrScr();
          myGLCD.print("LDR", CENTER, 0);
          delay(1000);
           
      }
    

  
  
      void menubutton()
   {
  
      if (pressed_button==butmenu)
      myGLCD.clrScr();
    myButtons.deleteButton(butmenu);
     menu();
   }

void menu()
{
    
    myGLCD.clrScr();
      myButtons.deleteButton(butmenu);
          myGLCD.print("Menu", CENTER, 0);
          butdht = myButtons.addButton( 10, 20, 300, 30, "DHT22");
          butbmp = myButtons.addButton( 10, 90, 300, 30, "BMP");
          butldr = myButtons.addButton( 10, 160, 300, 30, "LDR");
          myButtons.drawButtons();
          
            delay(3000);       
          myGLCD.clrScr();
          start();

  if (myTouch.dataAvailable() == true)
    myButtons.enableButton(butdht, true);
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butdht)
    {
      dhtmenu();
     myButtons.deleteButton(butdht);
    }
    }

}



void dhtmenu()

{
 
          myGLCD.clrScr();
          myGLCD.print("DHT22", CENTER, 0);
          delay(3000);
          menu();
          
}

Seen this:
http://forum.arduino.cc/index.php?topic=151958.msg1141476#msg1141476

im using the ubutton library

the utouch seemed a little more complicated.

I can get the start screen to stay on, the menu button to swith to the menu screen with buttons and a delay of three seconds before returning to the start screen.
how would I interrupt the menu screen with one of the three buttons. by interrupt,(guessing here) I mean press one of the three buttons to take me elsewhere. I have three sensors and I want to show the information from each on a separate page. I intend to use the nrf24lo1 modules to send that information through the air.

thanks for the help so far, this is gonna be a long one so get comfy!!!

all the best

simon

Is this the current library you are using? Are all libraries up-to-date?
http://henningkarlsen.com/electronics/library.php?id=55

Does this example function on your display/touch-screen?
I do not have this hardware, so there is no way I can test 4U.
Ray

Example: UTouch_ButtonTest.ino

// UTouch_ButtonTest (C)2010-2012 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a quick demo of how create and use buttons.
//
// This program requires the UTFT library.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin 
// numbers in the setup.
//

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

// Declare which fonts we will be using
extern uint8_t BigFont[];

// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT        myGLCD(ITDB24D,19,18,17,16);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(15,10,14,9,8);

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);

int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

/*************************
**   Custom functions   **
*************************/

void drawButtons()
{
// Draw the upper row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.printNumI(x+1, 27+(x*60), 27);
  }
// Draw the center row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
    if (x<4)
      myGLCD.printNumI(x+6, 27+(x*60), 87);
  }
  myGLCD.print("0", 267, 87);
// Draw the lower row of buttons
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (10, 130, 150, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 130, 150, 180);
  myGLCD.print("Clear", 40, 147);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (160, 130, 300, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (160, 130, 300, 180);
  myGLCD.print("Enter", 190, 147);
  myGLCD.setBackColor (0, 0, 0);
}

void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}

// Draw a red frame while a button is touched
void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

/*************************
**  Required functions  **
*************************/

void setup()
{
// Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  drawButtons();  
}

void loop()
{
  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
      if ((y>=10) && (y<=60))  // Upper row
      {
        if ((x>=10) && (x<=60))  // Button: 1
        {
          waitForIt(10, 10, 60, 60);
          updateStr('1');
        }
        if ((x>=70) && (x<=120))  // Button: 2
        {
          waitForIt(70, 10, 120, 60);
          updateStr('2');
        }
        if ((x>=130) && (x<=180))  // Button: 3
        {
          waitForIt(130, 10, 180, 60);
          updateStr('3');
        }
        if ((x>=190) && (x<=240))  // Button: 4
        {
          waitForIt(190, 10, 240, 60);
          updateStr('4');
        }
        if ((x>=250) && (x<=300))  // Button: 5
        {
          waitForIt(250, 10, 300, 60);
          updateStr('5');
        }
      }

      if ((y>=70) && (y<=120))  // Center row
      {
        if ((x>=10) && (x<=60))  // Button: 6
        {
          waitForIt(10, 70, 60, 120);
          updateStr('6');
        }
        if ((x>=70) && (x<=120))  // Button: 7
        {
          waitForIt(70, 70, 120, 120);
          updateStr('7');
        }
        if ((x>=130) && (x<=180))  // Button: 8
        {
          waitForIt(130, 70, 180, 120);
          updateStr('8');
        }
        if ((x>=190) && (x<=240))  // Button: 9
        {
          waitForIt(190, 70, 240, 120);
          updateStr('9');
        }
        if ((x>=250) && (x<=300))  // Button: 0
        {
          waitForIt(250, 70, 300, 120);
          updateStr('0');
        }
      }

      if ((y>=130) && (y<=180))  // Upper row
      {
        if ((x>=10) && (x<=150))  // Button: Clear
        {
          waitForIt(10, 130, 150, 180);
          stCurrent[0]='\0';
          stCurrentLen=0;
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(0, 224, 319, 239);
        }
        if ((x>=160) && (x<=300))  // Button: Enter
        {
          waitForIt(160, 130, 300, 180);
          if (stCurrentLen>0)
          {
            for (x=0; x<stCurrentLen+1; x++)
            {
              stLast[x]=stCurrent[x];
            }
            stCurrent[0]='\0';
            stCurrentLen=0;
            myGLCD.setColor(0, 0, 0);
            myGLCD.fillRect(0, 208, 319, 239);
            myGLCD.setColor(0, 255, 0);
            myGLCD.print(stLast, LEFT, 208);
          }
          else
          {
            myGLCD.setColor(255, 0, 0);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            delay(500);
            myGLCD.print("BUFFER EMPTY", CENTER, 192);
            delay(500);
            myGLCD.print("            ", CENTER, 192);
            myGLCD.setColor(0, 255, 0);
          }
        }
      }
    }
  }
}

UTFT_Buttons.pdf (145 KB)

What you can do is make your separate screens into functions, and when a button is pressed, it will repaint the screen (draw the items you need) and show your data. You may want to enclose them in while loops just so all your other data does not slow down your readings.

Here is a “small” example, really, compared to my full controller, these are just parts.

My loop function.

void loop()
{ 
  myTouch.read();
  tx = myTouch.TP_X;
  ty = myTouch.TP_Y;
  XC = constrain(map(tx, 205, 3900,0,239),0,239);
  YC = constrain(map(ty, 300, 3850,0,319),0,319);
  if(debugDisplay){
    data(YC,XC);
  }

  X = constrain(map(analogRead(A14),410,345,0,10),0,10); //415 - 275 415,275
  Y = constrain(map(analogRead(A15),415,275,0,20),0,20);//410 - 340  410,340
  Z = constrain(map(analogRead(A13),310,410,0,20),0,20);//310 - 415  310,420

  if(myTouch.TouchButton(220,220,318,238,XC,YC)) //limits screen
  { 
    ConState = false;
    ChangePars(); 
  }
  else if(myTouch.TouchButton(1,220,98,238,XC,YC))//settings screen
  {
    settings = true;
    myGLCD.ResetRadioButton(1);
    Settings();
  }
  else
  {
    SendOut(X,Y,Z);
  }
}

Limits screen:

void ChangePars()
{
  if(ConState == false)
  {
    for(int line = 0; line <= 239; line++)
    {
      int R = map(line, 0, 239, Color[0][0], Color[1][0]);
      int G = map(line, 0, 239, Color[0][1], Color[1][1]);
      int B = map(line, 0, 239, Color[0][2], Color[1][2]);

      myGLCD.setColor(R, G, B);//text color White  
      myGLCD.drawLine(0, line, 319, line);
    }
    BoxPars();
    PrintPars();

    tx = myTouch.TP_X;
    ty = myTouch.TP_Y;
    while(ConState == false)
    { 
      myGLCD.setColor(0,255,0); //green
      myTouch.read();
      tx = myTouch.TP_X;
      ty = myTouch.TP_Y;
      int XC= constrain (map(tx, 205, 3900,0,239),0,239);
      int YC= constrain (map(ty, 300, 3850,0,319),0,319);
      if(debugDisplay){
        data(YC,XC);
      }

      myGLCD.setBackColor(0, 0, 0);
      myGLCD.setColor(255,255,255);
      //ParCheck();

      if(myTouch.TouchButton(30,70,90,95,XC,YC)) {
        myGLCD.printNumI(XparPos >= 10 ? XparPos = 10 : XparPos++,52,108);  //X accelerometer data positive limit
      }
      else if(myTouch.TouchButton(30,130,90,155,XC,YC)) {
        myGLCD.printNumI(XparPos <= 0 ? XparPos = 0 : XparPos--,52,108); //X accelerometer data positive limit
        myGLCD.print(" ", 57, 108);
      }
      else if(myTouch.TouchButton(130,20,190,45,XC,YC)) {
        myGLCD.printNumI(YparPos >= 20 ? YparPos = 20 : YparPos++,152,53);//Y accelerometer data positive limit
      }
      else if(myTouch.TouchButton(130,70,190,95,XC,YC)) {
        myGLCD.printNumI(YparPos <=10 ? YparPos = 10 : YparPos--,152,53);//Y accelerometer data positive limit
      }
      else if(myTouch.TouchButton(230,20,290,45,XC,YC)) {
        myGLCD.printNumI(ZparPos >= 20 ? ZparPos = 20 : ZparPos++,252,53);//Z accelerometer data positive limit
      }
      else if(myTouch.TouchButton(230,70,290,95,XC,YC)) {
        myGLCD.printNumI(ZparPos <= 10 ? ZparPos = 10 : ZparPos--,252,53);//Z accelerometer data positive limit
      } 
      else if(myTouch.TouchButton(130,130,190,155,XC,YC)) {
        myGLCD.printNumI(YparNeg >= 10 ? YparNeg = 10 : YparNeg++,152,163); //Y accelerometer data negative limit
      else if(myTouch.TouchButton(130,180,190,205,XC,YC)) {
        myGLCD.printNumI(YparNeg <= 0 ? YparNeg = 0 : YparNeg--,152,163);// Y accelerometer data negative limit
        myGLCD.print(" ", 157, 163);
      }
      else if(myTouch.TouchButton(230,130,290,155,XC,YC)) {
        myGLCD.printNumI(ZparNeg >= 10 ? ZparNeg = 10 : ZparNeg++,252,163);//Z accelerometer data negative limit
      }
      else if(myTouch.TouchButton(230,180,290,205,XC,YC)) {
        myGLCD.printNumI(ZparNeg <= 0 ? ZparNeg = 0 : ZparNeg--,252,163);// Z accelerometer data negative limit
        myGLCD.print(" ", 257, 163);
      }
      else if(myTouch.TouchButton(0, 220, 50, 238,XC,YC)) { // Load from eeprom
        XparPos = EEPROM.read(0);
        YparPos = EEPROM.read(1);
        YparNeg = EEPROM.read(2);
        ZparPos = EEPROM.read(3);
        ZparNeg = EEPROM.read(4);
        delay(10);
        ChangePars();
      }
      else if(myTouch.TouchButton(70, 220, 120, 238,XC,YC)) {// Save to eeprom
        EEPROM.write(0, (byte)XparPos);
        EEPROM.write(1, (byte)YparPos);
        EEPROM.write(2, (byte)YparNeg);
        EEPROM.write(3, (byte)ZparPos);
        EEPROM.write(4, (byte)ZparNeg);
        myGLCD.setBackColor(0, 0, 230);
        myGLCD.setColor(0,0,0);
        myGLCD.print("Saved",30,200,0);
        delay(1500);
        myGLCD.print("     ",30,200,0);
      }

      else {
        myGLCD.printNumI(XparPos,52,108);
        myGLCD.printNumI(YparPos,152,53);
        myGLCD.printNumI(ZparPos,252,53);
        myGLCD.printNumI(YparNeg,152,163);
        myGLCD.printNumI(ZparNeg,252,163);

        if(myTouch.TouchButton(220, 220, 318, 238,XC,YC)){//controller, I.E. the main screen
          ConState = true;
          controller(); 
        }
      }
    }
  }
}

Settings screen exceeds the 9500 word limit
Video of my controller HERE Skip to 3:40

your display for your car is very impressive!!!

I have looked at your sketch and cant understand it. I am still learning this stuff and im trying to keep it simple.
I want to stay with the ubuttons library as the utouch seems a little more involved.

thanks for your help

all the best
simon

hello

Im having trouble getting more of my buttons to work. I think it may be something to do with the while statement or if statement. I have tried copying more of this statement and just changing the button names but nothing works. any help

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

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];



UTFT        myGLCD(ITDB32S,38,39,40,41);  
UTouch      myTouch(6,5,4,3,2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);


int pressed_button, butmenu, butdht, butbmp, butldr;

void start()
{
  myButtons.deleteAllButtons(); 
     //set colour for the title, display title and position on x axis(center) and position on y axis(0, 16)
   
    myGLCD.setColor(0, 0, 255);                          
    myGLCD.print("WEATHER STATION", CENTER, 0);
    myGLCD.print("PROJECT", CENTER, 16);  
//set colour to red, name and sensor names with positions 
    myGLCD.setColor(255, 0, 50);                        
    myGLCD.print("By Simon Bradley", CENTER, 32);    
    myGLCD.print("  -DHT22", CENTER, 48);
    myGLCD.print("  -BMP108", CENTER, 64);
    myGLCD.print("  -LDR", CENTER, 80);  
 //set colour to green, more names and positions  
    myGLCD.setColor(0, 255, 0);                          
    myGLCD.print("ARDUINO PROJECT", CENTER, 112);
    myGLCD.print("Engineering Science", CENTER, 144); 
     butmenu = myButtons.addButton( 10, 180, 300, 30, "Menu");
  myButtons.drawButtons();
  
  while (true)
{
  if (myTouch.dataAvailable() == true)
    
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butmenu)
    {
      menu();
     myButtons.deleteButton(butmenu);
    }
    }
}
}
    




void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myButtons.setTextFont(BigFont);
  

}

void loop()
{
     
  boolean default_colors = true;
  
 start();

 

}       
   

   
         
          

       
   void bmpmenu() 
       {
          
          
           myGLCD.clrScr(); 
          myGLCD.print("BMP", CENTER, 0);
          delay(3000);
          menu();
         
       } 
          
   void ldr()     
      {
         
         
           myGLCD.clrScr();
          myGLCD.print("LDR", CENTER, 0);
          delay(1000);
           
      }
    

  
  
      void menubutton()
   {
  
      if (pressed_button==butmenu)
      myGLCD.clrScr();
    myButtons.deleteButton(butmenu);
     menu();
   }

void menu()
{
    
    myGLCD.clrScr();
      myButtons.deleteButton(butmenu);
          myGLCD.print("Menu", CENTER, 0);
          butdht = myButtons.addButton( 10, 20, 300, 30, "DHT22");
          butbmp = myButtons.addButton( 10, 90, 300, 30, "BMP");
          butldr = myButtons.addButton( 10, 160, 300, 30, "LDR");
          myButtons.drawButtons();
          
          while (true)
{
  if (myTouch.dataAvailable() == true)
    
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butdht)
    {
      dhtmenu();
     myButtons.deleteButton(butdht);
    }
    }
   
}
   
            delay(3000);       
          myGLCD.clrScr();
          start();

}



void dhtmenu()

{
 
          myGLCD.clrScr();
          myGLCD.print("DHT22", CENTER, 0);
          delay(3000);
          menu();
          
}

Can you clean up the code, format it, so everything is neat.

void ChangePars()
{
if(ConState == false)
.
.
.
while(ConState == false)
.
.
.
if(myTouch.TouchButton(220, 220, 318, 238,XC,YC)){//controller, I.E. the main screen
ConState = true;
controller();
}

You need a way to breakout of the while loop when your done with that section of code.

the above isn't mine

please could you look at this and tell me what im doing wrong. neither button works now. before I only had the butdht and it worked. this time I added the other buttons and none of them work.
I don't know what im doing wrong

          while (true)
{
  if (myTouch.dataAvailable() == true)
    
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butdht)
    {
      dhtmenu();
     myButtons.deleteButton(butdht);
    }
    if (pressed_button==butbmp)
    {
      bmpmenu();
    }
    if (pressed_button==butldr);
    {
   ldrmenu();
    }   
}
   
            delay(3000);       
          myGLCD.clrScr();
          start();

}

You still need to fix your codes, the format "look" is terrible. Clean it up first.

i have tried to clean up the sketch that i have.
i still dont know how to get the buttons working. would i have to make a separate function for each button?
if anyone could POINT me in the right direction i would appreciate it. im still a beginner at this, it would be helpful if someone could suggest something (that isnt sarcasm).
thanks
simon

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

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];



UTFT        myGLCD(ITDB32S,38,39,40,41);  
UTouch      myTouch(6,5,4,3,2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);


int pressed_button, butmenu, butdht, butbmp, butldr;

void start()
{
  myButtons.deleteAllButtons(); 
     //set colour for the title, display title and position on x axis(center) and position on y axis(0, 16)
   
    myGLCD.setColor(0, 0, 255);                          
    myGLCD.print("WEATHER STATION", CENTER, 0);
    myGLCD.print("PROJECT", CENTER, 16);  
//set colour to red, name and sensor names with positions 
    myGLCD.setColor(255, 0, 50);                        
    myGLCD.print("By Simon Bradley", CENTER, 32);    
    myGLCD.print("  -DHT22", CENTER, 48);
    myGLCD.print("  -BMP108", CENTER, 64);
    myGLCD.print("  -LDR", CENTER, 80);  
 //set colour to green, more names and positions  
    myGLCD.setColor(0, 255, 0);                          
    myGLCD.print("ARDUINO PROJECT", CENTER, 112);
    myGLCD.print("Engineering Science", CENTER, 144); 
    butmenu = myButtons.addButton( 10, 180, 300, 30, "Menu");
    myButtons.drawButtons();
  
    while (true)
      {
          if (myTouch.dataAvailable() == true)
    
      {
          pressed_button = myButtons.checkButtons();
              if (pressed_button==butmenu)
                  {
                      menu();
                      myButtons.deleteButton(butmenu);
                  }
      }
    }
  }
    




void setup()
  {
    myGLCD.InitLCD();
    myGLCD.clrScr();
    myGLCD.setFont(BigFont);
    myTouch.InitTouch();
    myTouch.setPrecision(PREC_MEDIUM);
    myButtons.setTextFont(BigFont);
  

  }

void loop()
  {
     
      boolean default_colors = true;
      
      start();

  }       
   

void bmp() 
       {
          
          
           myGLCD.clrScr(); 
           myGLCD.print("BMP", CENTER, 0);
           delay(1000);
         
       } 
          
void ldr()     
      {
         
         
           myGLCD.clrScr();
           myGLCD.print("LDR", CENTER, 0);
           delay(1000);
           
      }
    

void menubutton()
     {
  
        if (pressed_button==butmenu)
        myGLCD.clrScr();
        myButtons.deleteButton(butmenu);
        menu();
     }

void menu()
    {
    
        myGLCD.clrScr();
        myButtons.deleteButton(butmenu);
        myGLCD.print("Menu", CENTER, 0);
        butdht = myButtons.addButton( 10, 20, 300, 30, "DHT22");
        butbmp = myButtons.addButton( 10, 90, 300, 30, "BMP");
        butldr = myButtons.addButton( 10, 160, 300, 30, "LDR");
        myButtons.drawButtons();
          
     while (true)
        {
          if (myTouch.dataAvailable() == true)
    
            {
                pressed_button = myButtons.checkButtons();
                if (pressed_button==butdht)
                  {
                    dhtmenu();
                    myButtons.deleteButton(butdht);
                  }
            }
        }
   
            delay(3000);       
            myGLCD.clrScr();
            start();

      }



void dhtmenu()

{
 
          myGLCD.clrScr();
          myGLCD.print("DHT22", CENTER, 0);
          delay(3000);
          menu();
          
}

This: while (true) {code} will be a forever-loop unless you

  1. change to a conditional inside the while parenthesis. I.e. "while (someBoolean==true) or simply just while(someBoolean)
  2. Add a test to send you out of the loop using "break;". e.g. " if(conditionsForLeaving Loop==true) break;"

/H

hello

thanks for the reply, please could you explain a little more. i dont know what "someboolean" would do. im guessing its true/false. isnt that the same as while (true)?

add a test? im not sure about this. i cant remember reading about it.
" if(conditionsForLeaving Loop==true) break;" is this just an example?

all i want to do is have three buttons that would take you to their display screens, so you would have three buttons laid out like this, DHT22
BMP108
LDR
when i press the DHT22, it goes to that display screen but no matter what i do i cant get the others to work. i have tried writing different functions and other things but i cant get it working. one that i did try was

if(button_pressed==butdht)
{
dhtmenu();
}
if(button_pressed==butbmp)
{
bmpmenu();
}
if(button_pressed==butldr)
{
ldrmenu();
}

when i put this in the dht button and display screen stopped working, it seemed to freeze at the sensor menu. i tried some other things that caused the start display to freeze and the menu button stopped working.

OK sorry for being too short…

“someBoolean” means an identified boolean variable that you specify the value of(true or false).
check the example HAzardsMind posted earlier:

void ChangePars()
{
  if(ConState == false)
.
.
.
  while(ConState == false)
.
.
.
if(myTouch.TouchButton(220, 220, 318, 238,XC,YC)){//controller, I.E. the main screen
          ConState = true;  
          controller(); 
        }

In that code there has first been a declaration of the variable ConState as Boolean, and second the value of ConState nedds to be set to true or flase.

When the
"while (ConState==true) " statement executes the value of ConState decides wether the block following it should execute aswell. If the statement in the parenthesis returns ‘true’ the block (the code between the following{} pair) is executed and then the “while (ConState==true)” statement is repeated again and if it has not changed within the block the code is executed again. The code will then get stuck. But in the example above there is the “test” in the form of an if-statement that potentially changes the value of ConState if the right criteria are set so there is an exit from the while-loop.

But there is a more serious problem with your code and that is that you tend to call functions within functions rather than letting them exit by themselves.

Example: You call start() from menu() and menu() from start().

while this works for a while you must realise that each call manifests new variables in the memory which will then eventually be full or the stack will overflow.

To get the program to jump back from a function call even if has not reached the end of the code you can use return instead of calling the function again.

Example:

void fa(int i)
{
    if (i == 2)
         return;
    std::cout << i << '\n';
}

Ill try to explain this further by the following example:

void a()
{ 
while (true)
{ 
b();
println("a");
}
}
void b()
{ 
while (true)
{ 
println("b");
a();
}
}

The output would be a neverending series of b’s until the memory is full.
However if we write

void a()
{ 
while (true)
{ 
b();
println("a");
}
}
void b()
{ 
while (true)
{ 
println("b");
return;
a();
}
}

We would get a series of b’s AND a’s since the code would return to a() just after the call to b(). In the previous example a new a() would be initialized and executed but now we return to the the one we already got and the running instance of b is destroyed

I

To get you buttons to work, you first need to know if you are able to get feedback from the touch screen.
Next you will need to calibrate your touch screen and set the boundaries. Once you got all that and you can see where you are touching the screen with X and Y values and it is accurate to the screen, you can then use this.

TouchButton(int x1, int y1, int x2, int y2, int xc, int yc) // the first four values are the coordinates of where your button is on the screen, then the second two values are the raw touch coordinates.
{
int tmp;	
  if(xc >= x1 && xc<= x2 && yc >= y1 && yc <= y2) return true;
  return false;
}

hello

im still having the same problem, I have tried rewriting the sketch and ordering it a little better but the same problem persists. I think it may have something to do with the calibration of the screen. the buttons light up when pressed and the dht button is always working. sometimes the ldr (bottom) button is working, it will work once, return to sensormenu() then only light up when pressed. the bmp (middle) button hasn’t worked at all yet, only light when pressed.

im not sure what it is, maybe the screen not calibrated but otherwise im stumped.

here is the code, I appreciate all the help I have received so far it really has helped! (I tried adjusting the precision on the touch display from low, medium, hi and extreme. I also swapped display both had the same problem. I think adjusting the calibration on the .h file may help. does anyone know how to do that? I have the values)

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

// Declare which fonts we will be using
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];



UTFT        myGLCD(ITDB32S,38,39,40,41);  
UTouch      myTouch(6,5,4,3,2);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);


int pressed_button, butmenu, butdht, butbmp, butldr;

  




void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  myButtons.setTextFont(BigFont);
  

}

void loop()
{
     
  boolean default_colors = true;
  
   myButtons.deleteAllButtons(); 
     //set colour for the title, display title and position on x axis(center) and position on y axis(0, 16)
   
    myGLCD.setColor(0, 0, 255);                          
    myGLCD.print("WEATHER STATION", CENTER, 0);
    myGLCD.print("PROJECT", CENTER, 16);  
//set colour to red, name and sensor names with positions 
    myGLCD.setColor(255, 0, 50);                        
    myGLCD.print("By Simon Bradley", CENTER, 32);    
    myGLCD.print("  -DHT22", CENTER, 48);
    myGLCD.print("  -BMP108", CENTER, 64);
    myGLCD.print("  -LDR", CENTER, 80);  
 //set colour to green, more names and positions  
    myGLCD.setColor(0, 255, 0);                          
    myGLCD.print("ARDUINO PROJECT", CENTER, 112);
    myGLCD.print("Engineering Science", CENTER, 144); 
    butmenu = myButtons.addButton( 10, 180, 300, 30, "Menu");
    myButtons.drawButtons();
      while (true)
{
  if (myTouch.dataAvailable() == true)
    
    {
      pressed_button = myButtons.checkButtons();
      if (pressed_button==butmenu)
        {
          sensormenu();
         
        }
 
    }
}

 
}
 
 void sensormenu()   

    {
      myGLCD.clrScr();
      myButtons.deleteButton(butmenu);
          myGLCD.print("Menu", CENTER, 0);
          butdht = myButtons.addButton( 10, 20, 300, 30, "DHT22");
          butbmp = myButtons.addButton( 10, 90, 300, 30, "BMP");
          butldr = myButtons.addButton( 10, 160, 300, 30, "LDR");
          myButtons.drawButtons();
     
     while (true)
        {
            if (myTouch.dataAvailable() == true)
    
              {
                  pressed_button = myButtons.checkButtons();
                  if (pressed_button==butdht)
                      {    
                          dhtmenu();
                      }
                      
              
             
               if (pressed_button==butbmp)
                      {    
                          bmpmenu();
                      }
                    
                 if (pressed_button==butldr)
                     {
                          ldrmenu();
                     }
    
              }
          }
     }
          
     void bmpmenu() 
       {   
          myGLCD.clrScr(); 
          myGLCD.print("BMP", CENTER, 0);
          delay(3000);
          sensormenu();
       } 
          
   void ldrmenu()     
      {             
           myGLCD.clrScr();
           myGLCD.print("LDR", CENTER, 0);
           delay(3000);
           sensormenu();
      }

void dhtmenu()
    {
          myGLCD.clrScr();
          myGLCD.print("DHT22", CENTER, 0);
          delay(3000);
          sensormenu();
    }

The Utouch library has a way to let you see your raw touch data. GetY and GetX. These will let you see if your screen is calibrated or not. There should also be a calibration sketch, if not, then it should be on henning’s website.