Making a button do something

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Making a button do something

Post by adafruit_support_rick »

Hard to say. Can you post the entire sketch?

Something that puzzles me is how you're able to compile the sketch with this: tft.setTextSize(2.5);
setTextSize in the Adafruit_GFX library only accepts unsigned 8-bit values. You should be getting a compiler error.

What libraries are you using?

kqpro
 
Posts: 24
Joined: Sun Feb 10, 2013 2:41 pm

Re: Making a button do something

Post by kqpro »

Here is the entire code driverblock, im not sure if you have an arduino mega and a TFT to test it. So i made the 2 buttons, ON and OFF. and I tested the coords to display coords in the serial monitor whenever I press within these 2 buttons, so I know I have pressed the buttons.

Now I want it to go to a different screen when I press them, as you can see, I want it to display, "please wait for the arm to be in position" But it is not displaying it when I press the button.

Code: Select all

// Paint example specifically for the TFTLCD Arduino shield.
// If using the breakout board, use the tftpaint.pde sketch instead!

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#ifndef USE_ADAFRUIT_SHIELD_PINOUT 
 #error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif

// These are the pins for the shield!
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0

// Assign human-readable names to some common 16-bit color values:
#define	BLACK   0x0000
#define	BLUE    0x001F
#define	RED     0xF800
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define name    0x4DBD33
#define backgroun1  0xe6f8e5


Adafruit_TFTLCD tft;

#define BOXSIZE   40
#define BANNED  4
int oldcolor, currentcolor;

void setup(void) {
  Serial.begin(9600);
  progmemPrintln(PSTR("Paint!"));

  tft.reset();

  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    return;
  }

  tft.begin(identifier);

  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(5, 10);
  tft.fillRect(0, 0, 260, 80, name);
  tft.setTextColor(0xe32b13);    tft.setTextSize(3.6);
  tft.println("W E L C O M E");
  tft.setCursor(130, 10); 
  tft.setTextSize(3.0);
  tft.println();
  tft.setTextColor(0x281855);
  tft.println();
  tft.setCursor(15, 56); tft.println("   S");
  tft.setTextColor(0x6E0EF9);
  tft.setTextColor(BLACK);
  tft.setCursor(88, 63); tft.setTextSize(2.8); tft.println("tanley");
  //circles
  tft.fillCircle(12, 92, 12, 0xb46455);
  tft.fillCircle(225, 92, 12, 0xb46455);
  tft.fillCircle(12, 305, 12, 0xb46455);
  tft.fillCircle(225, 305, 12, 0xb46455);
  //bottom text
  tft.setCursor(60, 310);
  tft.setTextColor(WHITE); tft.setTextSize(1.2);
  tft.println("@2013 Electro BANNED");
  
  // the 2 buttons
  tft.fillRect(0, 105, 110, 187.5, 0x0c775d);
  tft.fillRect(130, 105, 110, 187.5, 0x9e9b4e);
  // OFF text
  tft.setCursor(155, 240);
  tft.setTextColor(BLACK);
  tft.setTextSize(4); tft.println("OFF");
  //on symbol
  tft.fillCircle(53, 173, 35, BLACK); 
  tft.fillCircle(53, 173, 25, 0xe4D86EA);
  tft.fillRect(50,130, 8,40, BLACK);
  //off symbol
  tft.fillCircle(188, 173, 35, BLACK);
  tft.fillCircle(188, 173, 25, 0x9e9b4e);
  // ON TEXT
  tft.setTextColor(BLACK);
  tft.setCursor(30, 240);
  tft.setTextSize(4); tft.println("ON");
 
 // pinMode(13, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

void loop()
{
  Point p = ts.getPoint();
  
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    
    //Serial.print("X = "); Serial.print(p.x);
    //     Serial.print("\tY = "); Serial.print(p.y);
     //    Serial.print("\tPressure = "); Serial.println(p.z);
     
    //on button
    
    if ((p.x > 8) && (p.x < 110)) { 
      if ((p.y > 100) && (p.y < 289)) { 
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z); 
          tft.fillScreen(BLACK);
          tft.setTextColor(GREEN);    tft.setTextSize(2.5);
          tft.println("Please wait..");
          tft.println("for the arm to be ");
          tft.println("in position");

     }
    
     }
         
         
     //off button
     if ((p.x > 143) && (p.x < 245)) { 
      if ((p.y > 99) && (p.y < 290)) {    
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z); }
         
         
         
         
       }
         
        }
}


// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}


User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Making a button do something

Post by adafruit_support_rick »

The TFT and the touch are sharing a couple of pins. ts.getPoint sets them to input, but tft needs them to be outputs. You have to switch the direction after every call to getPoint.

This is from the tftpaint_shield example sketch:

Code: Select all

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);
I added it to your sketch (below), and it now works as expected. I also took the liberty of adding an on/off state variable. Since you can easily get multiple touch readings in your loop for a single touch of the screen, the state variable restricts the code to only redrawing the screen on the first hit.

Code: Select all

// Paint example specifically for the TFTLCD Arduino shield.
// If using the breakout board, use the tftpaint.pde sketch instead!

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#ifndef USE_ADAFRUIT_SHIELD_PINOUT 
#error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif

// These are the pins for the shield!
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0

// Assign human-readable names to some common 16-bit color values:
#define   BLACK   0x0000
#define   BLUE    0x001F
#define   RED     0xF800
#define   GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define name    0x4DBD33
#define backgroun1  0xe6f8e5

enum ButtonState_t {off, on};
ButtonState_t currentState = off;

Adafruit_TFTLCD tft;

#define BOXSIZE   40
#define BANNED  4
int oldcolor, currentcolor;

void setup(void) {
  Serial.begin(9600);
  progmemPrintln(PSTR("Paint!"));

  tft.reset();

  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    return;
  }

  tft.begin(identifier);

  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(5, 10);
  tft.fillRect(0, 0, 260, 80, name);
  tft.setTextColor(0xe32b13);    tft.setTextSize(3.6);
  tft.println("W E L C O M E");
  tft.setCursor(130, 10); 
  tft.setTextSize(3.0);
  tft.println();
  tft.setTextColor(0x281855);
  tft.println();
  tft.setCursor(15, 56); tft.println("   S");
  tft.setTextColor(0x6E0EF9);
  tft.setTextColor(BLACK);
  tft.setCursor(88, 63); tft.setTextSize(2.8); tft.println("tanley");
  //circles
  tft.fillCircle(12, 92, 12, 0xb46455);
  tft.fillCircle(225, 92, 12, 0xb46455);
  tft.fillCircle(12, 305, 12, 0xb46455);
  tft.fillCircle(225, 305, 12, 0xb46455);
  //bottom text
  tft.setCursor(60, 310);
  tft.setTextColor(WHITE); tft.setTextSize(1.2);
  tft.println("@2013 Electro BANNED");
  
  // the 2 buttons
  tft.fillRect(0, 105, 110, 187.5, 0x0c775d);
  tft.fillRect(130, 105, 110, 187.5, 0x9e9b4e);
  // OFF text
  tft.setCursor(155, 240);
  tft.setTextColor(BLACK);
  tft.setTextSize(4); tft.println("OFF");
  //on symbol
  tft.fillCircle(53, 173, 35, BLACK); 
  tft.fillCircle(53, 173, 25, 0xe4D86EA);
  tft.fillRect(50,130, 8,40, BLACK);
  //off symbol
  tft.fillCircle(188, 173, 35, BLACK);
  tft.fillCircle(188, 173, 25, 0x9e9b4e);
  // ON TEXT
  tft.setTextColor(BLACK);
  tft.setCursor(30, 240);
  tft.setTextSize(4); tft.println("ON");

// pinMode(13, OUTPUT);
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

void loop()
{
  Point p = ts.getPoint();
  
  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    
    //Serial.print("X = "); Serial.print(p.x);
    //     Serial.print("\tY = "); Serial.print(p.y);
     //    Serial.print("\tPressure = "); Serial.println(p.z);
     
    //on button
    
    if ((p.x > 8) && (p.x < 110)) { 
      if ((p.y > 100) && (p.y < 289)) { 
         Serial.println("On");
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z);
         if (off == currentState)
         {
            currentState = on;
            tft.fillScreen(BLACK);
            tft.setTextColor(GREEN);    tft.setTextSize(2.5);
            tft.println("Please wait..");
            tft.println("for the arm to be ");
            tft.println("in position");
         }
       }  
     }
         
     //off button
     if ((p.x > 143) && (p.x < 245)) { 
      if ((p.y > 99) && (p.y < 290)) {
         Serial.println("Off");
    
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z); }
         if (on == currentState)
         {
           currentState = off;
         }
       }         
     }
}


// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}

kqpro
 
Posts: 24
Joined: Sun Feb 10, 2013 2:41 pm

Re: Making a button do something

Post by kqpro »

Thank you very much !!! I will play around with it more now and see what I can do :)

kqpro
 
Posts: 24
Joined: Sun Feb 10, 2013 2:41 pm

Re: Making a button do something

Post by kqpro »

Hey driverblock, i made another screen, which is the screen it should go into after the "loading.." screen. I made these 2 screens into functions name second_screen, and loading_screen. So my plan is to: when i power on, it goes to the first screen ( i think i will make the first screen into a function called main_menu), the main menu, then if i press ON, it will show the loading_screen for 2 seconds (adding a delay() i think ) then it will go to the second_screen (), which I made a function of, it will display some other buttons.

So far my code is:

Code: Select all

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>

#ifndef USE_ADAFRUIT_SHIELD_PINOUT 
#error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif

// These are the pins for the shield!
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0

// Assign human-readable names to some common 16-bit color values:
#define   BLACK   0x0000
#define   BLUE    0x001F
#define   RED     0xF800
#define   GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define name    0x4DBD33
#define backgroun1  0xe6f8e5

enum ButtonState_t {off, on};
ButtonState_t currentState = off;

Adafruit_TFTLCD tft;

#define BOXSIZE   40
#define BANNED  4
int oldcolor, currentcolor;

void setup(void) {
  Serial.begin(9600);
  progmemPrintln(PSTR("Paint!"));

  tft.reset();

  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    return;
  }

  tft.begin(identifier);

  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(5, 10);
  tft.fillRect(0, 0, 260, 80, name);
  tft.setTextColor(0xe32b13);    tft.setTextSize(3.6);
  tft.println("W E L C O M E");
  tft.setCursor(130, 10); 
  tft.setTextSize(3.0);
  tft.println();
  tft.setTextColor(0x281855);
  tft.println();
  tft.setCursor(15, 56); tft.println("   S");
  tft.setTextColor(0x6E0EF9);
  tft.setTextColor(BLACK);
  tft.setCursor(88, 63); tft.setTextSize(2.8); tft.println("tanley");
  //circles
  tft.fillCircle(12, 92, 12, 0xb46455);
  tft.fillCircle(225, 92, 12, 0xb46455);
  tft.fillCircle(12, 305, 12, 0xb46455);
  tft.fillCircle(225, 305, 12, 0xb46455);
  //bottom text
  tft.setCursor(60, 310);
  tft.setTextColor(WHITE); tft.setTextSize(1.2);
  tft.println("@2013 Electro BANNED");
  
  // the 2 buttons
  tft.fillRect(0, 105, 110, 187.5, 0x0c775d);
  tft.fillRect(130, 105, 110, 187.5, 0x9e9b4e);
  // OFF text
  tft.setCursor(155, 240);
  tft.setTextColor(BLACK);
  tft.setTextSize(4); tft.println("OFF");
  //on symbol
  tft.fillCircle(53, 173, 35, BLACK); 
  tft.fillCircle(53, 173, 25, 0xe4D86EA);
  tft.fillRect(50,130, 8,40, BLACK);
  //off symbol
  tft.fillCircle(188, 173, 35, BLACK);
  tft.fillCircle(188, 173, 25, 0x9e9b4e);
  // ON TEXT
  tft.setTextColor(BLACK);
  tft.setCursor(30, 240);
  tft.setTextSize(4); tft.println("ON");

// pinMode(13, OUTPUT);
}

// this screen should show up after the loading screen,
// i should be able to go back to main menu from here

unsigned long second_screen() {
  tft.reset();
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.println();
  tft.setTextColor(YELLOW);    tft.setTextSize(2.7);
  tft.println("Your arm is now");
  tft.println("        in position");
  tft.fillRect(5, 60, 220, 60, 0xb46455);
  tft.setTextColor(WHITE);    tft.setTextSize(2.1);
  tft.setCursor(5, 70);
  tft.println("Go back to menu");
  
  tft.setTextColor(WHITE);    tft.setTextSize(2.1);
  tft.drawRect(5, 180, 110, 110, YELLOW);
  tft.drawRect(130, 180, 110, 110, RED);
  tft.setCursor(155, 200);
  tft.setTextColor(RED);
  tft.println("FOLD");
  tft.fillRect(5, 180, 110, 110, WHITE);
  tft.fillRect(13, 188, 94, 94, BLACK);
  tft.setTextColor(GREEN);
  tft.setCursor(20, 200);
  tft.println("SHUTTER");
  return micros() - start; 
}

//this loading screen should be 2 seconds long

unsigned long loading_screen() {
            tft.fillScreen(BLACK);
            tft.setTextColor(GREEN);    tft.setTextSize(2.5);
            tft.println("Please wait..");
            tft.println("for the arm to be ");
            tft.println("in position");
}




#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

void loop()
{
  Point p = ts.getPoint();
  
  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    

     
    //on button
    
    if ((p.x > 8) && (p.x < 110)) { 
      if ((p.y > 100) && (p.y < 289)) { 
         Serial.println("On");
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z);
         
         second_screen();
         /*
         if (off == currentState)
         {
            currentState = on;
            tft.fillScreen(BLACK);
            tft.setTextColor(GREEN);    tft.setTextSize(2.5);
            tft.println("Please wait..");
            tft.println("for the arm to be ");
            tft.println("in position");
         }
         */
       }  
     }
         
     //off button
     if ((p.x > 143) && (p.x < 245)) { 
      if ((p.y > 99) && (p.y < 290)) {
         Serial.println("Off");
    
         Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z); }
         //if (on == currentState)
         //{
          // currentState = off;
         //}
       }         
     }
}


// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}
Can you tell me where should I add the delay and the second_screen() function? Because currently, i added it into the ON loop, the second_screen shows up after I press ON, but why is it that I can still press that ON area? The TFT doesnt reset?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Making a button do something

Post by adafruit_support_rick »

second_screen is a function. You call it, it draws the screen, and then returns to loop(), where you're still checking for the on and off button locations.
That's why you need to keep track of the current state. When you're in the 'off' state, you draw your basic screen and check the 'on' and 'off' button locations. When you're in the 'on' state, you draw your second screen and check the locations of whatever buttons are on that screen.

Here's a simple outline of a state-machine:

Code: Select all

enum ButtonState_t {init, off, on};
ButtonState_t currentState = init;

void loop()
{

   <read  touch location>

  switch (currentState)
  {
    case init:
      <draw first screen>
      currentState = off;
      break;
    case off:
      if <touch location is inside ON button>
      {
        <draw second screen>
        currentState = on;
      }
      break;
    case on:
      if <touch location is inside second screen's OFF button>
      {
        <draw first screen>
        currentState = off;
      }
       if <touch location is inside of a second screen action button>
      {
        <perform action>
      }
      break;
     if <touch location is inside some other second screen button>
      {
        <draw third screen>
        currentState = <third screen state identifier>;
      }
      break;
    case <third screen state identifier>:
      <etc...>
      break;
      
    default:
      break;
  } //end state-machine switch statement
     
  <...etc....>

} //end loop()  
  

kqpro
 
Posts: 24
Joined: Sun Feb 10, 2013 2:41 pm

Re: Making a button do something

Post by kqpro »

Thanks for the structure example, i followed the structure and have the following:

Code: Select all

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#ifndef USE_ADAFRUIT_SHIELD_PINOUT 
#error "This sketch is intended for use with the TFT LCD Shield. Make sure that USE_ADAFRUIT_SHIELD_PINOUT is #defined in the Adafruit_TFTLCD.h library file."
#endif
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0

// Assign human-readable names to some common 16-bit color values:
#define   BLACK   0x0000
#define   BLUE    0x001F
#define   RED     0xF800
#define   GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define name    0x4DBD33
#define backgroun1  0xe6f8e5

enum ButtonState_t {off, on, main, shutter};
ButtonState_t currentState = main;

Adafruit_TFTLCD tft;



void setup(void) {
  Serial.begin(9600);
  tft.reset();
  uint16_t identifier = tft.readID();

  if(identifier == 0x9325) {
    progmemPrintln(PSTR("Found ILI9325 LCD driver"));
  } else if(identifier == 0x9328) {
    progmemPrintln(PSTR("Found ILI9328 LCD driver"));
  } else if(identifier == 0x7575) {
    progmemPrintln(PSTR("Found HX8347G LCD driver"));
  } else {
    progmemPrint(PSTR("Unknown LCD driver chip: "));
    Serial.println(identifier, HEX);
    return;
  }

  tft.begin(identifier);

  main_menu();

// pinMode(13, OUTPUT);
}

unsigned long main_menu() {
  tft.fillScreen(BLACK);
  unsigned long start = micros();
  tft.setCursor(5, 10);
  tft.fillRect(0, 0, 260, 80, name);
  tft.setTextColor(0xe32b13);    tft.setTextSize(3.6);
  tft.println("W E L C O M E");
  tft.setCursor(130, 10); tft.setTextSize(3.0);
  tft.println(); tft.setTextColor(0x281855);
  tft.println(); tft.setCursor(15, 56); tft.println("   S");
  tft.setTextColor(0x6E0EF9);
  tft.setTextColor(BLACK);
  tft.setCursor(88, 63); tft.setTextSize(2.8); tft.println("tanley");
  //circles
  tft.fillCircle(12, 92, 12, 0xb46455); tft.fillCircle(225, 92, 12, 0xb46455);
  tft.fillCircle(12, 305, 12, 0xb46455); tft.fillCircle(225, 305, 12, 0xb46455);
  //bottom text
  tft.setCursor(60, 310); tft.setTextColor(WHITE); tft.setTextSize(1.2);
  tft.println("@2013 Electro BANNED");
  
  // the 2 buttons
  tft.fillRect(0, 105, 110, 187.5, 0x0c775d);
  tft.fillRect(130, 105, 110, 187.5, 0x9e9b4e);
  // OFF text
  tft.setCursor(155, 240);
  tft.setTextColor(BLACK);
  tft.setTextSize(4); tft.println("OFF");
  //on symbol
  tft.fillCircle(53, 173, 35, BLACK); 
  tft.fillCircle(53, 173, 25, 0xe4D86EA);
  tft.fillRect(50,130, 8,40, BLACK);
  //off symbol
  tft.fillCircle(188, 173, 35, BLACK);
  tft.fillCircle(188, 173, 25, 0x9e9b4e);
  // ON TEXT
  tft.setTextColor(BLACK);
  tft.setCursor(30, 240);
  tft.setTextSize(4); tft.println("ON");
  
  
  
}

unsigned long second_screen() {
  tft.reset();
  tft.fillScreen(0x1a3ab2);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.println();
  tft.setTextColor(YELLOW);    tft.setTextSize(2.7);
  tft.println("Your arm is now");
  tft.println("        in position!");
  tft.fillRect(0, 60, 240, 70, 0xb46455);
  tft.setTextColor(BLACK);    tft.setTextSize(2.1);
  tft.setCursor(5, 80);
  tft.println("  Go back to menu.");
  
  tft.fillRect(130, 130, 113, 200, WHITE);
  tft.fillRect(138, 138, 95, 176, BLACK);
  tft.setCursor(155, 200);
  tft.setTextColor(RED);
  tft.println("  ");
  
  tft.fillRect(0, 130, 110, 200, WHITE);
  tft.fillRect(7, 138, 95, 176, BLACK);
  tft.setTextColor(GREEN);
  tft.setCursor(15, 200);
  tft.println("");
  return micros() - start; 
}

//this loading screen should be 2 seconds long

unsigned long loading_screen() {
            tft.fillScreen(BLACK);
            tft.setTextColor(GREEN);    tft.setTextSize(2.5);
            tft.println("Please wait..");
            tft.println("for the arm to be ");
            tft.println("in position");
}




#define MINPRESSURE 10
#define MAXPRESSURE 1000

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

void loop()
{
  Point p = ts.getPoint();
  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  //pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  //pinMode(YM, OUTPUT);
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!
    p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
    p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
    

         
    switch (currentState)
  {
    case main:
      main_menu();
      currentState = off;
      break;
    case off:
    if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
      if ((p.x > 8) && (p.x < 110)) { 
        if ((p.y > 100) && (p.y < 289)) 
          {
           loading_screen();
           delay(300);
           second_screen();
           currentState = on;
           
          }}}
      break;
    case on:
    if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
      if ((p.x > 17) && (p.x < 24)) { 
        if ((p.y > 50) && (p.y < 127)) {
      
         Serial.print("\nX = "); Serial.print(p.x);
         Serial.print("Y = "); Serial.print(p.y);
        main_menu();
        currentState = off;
          }
         }
      }    

      break;
  
  }
    
}
 

// Copy string from flash to serial port
// Source string MUST be inside a PSTR() declaration!
void progmemPrint(const char *str) {
  char c;
  while(c = pgm_read_byte(str++)) Serial.print(c);
}

// Same as above, with trailing newline
void progmemPrintln(const char *str) {
  progmemPrint(str);
  Serial.println();
}
It goes to the second_screen when i press ON, which is good, but why does it not go back, I measured the corrds and put it in the if-statement for the "Go back to menu" button in the second_screen. When i click the "Go back to menu" it should go back to the main_menu().
PS. why does it flash once when i power on to the main_menu everytime?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Making a button do something

Post by adafruit_support_rick »

The X coordinate range of your "go back to main menu" is only seven pixels. That's probably pretty hard to hit. Close to the edge, too - the touch screen might not even register a touch there. In contrast, your On button is about 100 pixels wide.

The screen flashes because you call main_menu() from setup(), and then call it again immediately from case main:

kqpro
 
Posts: 24
Joined: Sun Feb 10, 2013 2:41 pm

Re: Making a button do something

Post by kqpro »

Oh I might've measured the wrong coords.. let me re try it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”