Go Down

Topic: UTFT on/off buttons wont work (Read 19 times) previous topic - next topic

Hey forumers,
I am trying to make 4 (simple??) buttons, that can switch a relay on or off. You should think that this a easy task, but i cant make it working. i have a if statement for different buttons, but when i switch 1 on, button 2 goes off.. etc. So i can have 1 button on at the time, when i push a other button on the rest go off.
Can someone with some more experience then me, push me in the right direction?

I have a 7" TFT CPLD + Shield +MEGA 2560 from coldtears electronics.

I hope someone can push me in the right direction, thanks!

my code:
Code: [Select]

#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_Buttons.h>
//#include <UTFT_tinyFAT.h>
//#include <tinyFAT.h>

extern uint8_t SmallFont[];
extern uint8_t BigFont[];


// Remember to change the model parameter to suit your display module!
UTFT    myGLCD(CTE70CPLD,38,39,40,41);
UTouch  myTouch( 6, 5, 4, 3, 2);

// Finally we set up UTFT_Buttons :)
UTFT_Buttons  myButtons(&myGLCD, &myTouch);
// Finally we set up UTFT_tinyFAT :)
//UTFT_tinyFAT myFiles(&myGLCD);

void setup()
{
  //file.initFAT();
   
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.fillScr(VGA_GRAY);
 
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  //myFiles.loadBitmap(30, 100, 300, 300, "power.raw");
  //myFiles.loadBitmap(100, 400, 700, 50,"shutdown.bmp");
 
 /*
  // On / Off button 1
  int Status1;
  Status1 = 0;
  // On / Off button 2
  int Status2;
  Status2 = 0;
  // On / Off button 2
  int Status3;
  Status3 = 0;
  */
}

void loop()


  int but1, but2, but3, but4, but5, but6, butX, butY, pressed_button, x, y;
  int Status1, Status2, Status3, Status4;


 
  // Statusbar UP
  myGLCD.drawRect(0, 1, 798, 40);
  myGLCD.setBackColor(VGA_GRAY);
  myGLCD.print( "=== Header ===",CENTER, 12);
   
  // Statusbar DOWN
  myGLCD.drawRect(0, 439, 798, 479);
  myGLCD.print( "=== Footer ===",CENTER, 450);
  //myGLCD.setBackColor(VGA_GRAY);
 
 
  // Menu
  myButtons.setTextFont(BigFont);
  myButtons.setButtonColors(VGA_BLACK, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_WHITE);
  but1 = myButtons.addButton(10,    100, 200,  70,  "Relay 1");
  but2 = myButtons.addButton(220,   100, 180,  70, "Relay 2");
  but3 = myButtons.addButton(10,    180, 200,  70,  "Relay 3");
  but4 = myButtons.addButton(220,   180, 180,  70, "Relay 4");
 
 
  //but5 = myButtons.addButton(409, 100, 379,  70, "LED slang");
  myButtons.drawButtons();   
   

 
while (true)
  {
    if (myTouch.dataAvailable())
    {
      pressed_button = myButtons.checkButtons();
           
      if ((pressed_button==but1) && (Status1 == 0 ))
      {
       myButtons.relabelButton(but1, "ON");
       myButtons.drawButton(but1);
       Status1 = 1;
      }
      else
      {
       myButtons.relabelButton(but1, "OFF");
       myButtons.drawButton(but1);
       Status1 = 0;
      }
       
      if ((pressed_button==but2) && (Status2 == 0 )) {
       myButtons.relabelButton(but2, "ON");
       myButtons.drawButton(but2);
       Status2 = 1;
      }
      else {
       myButtons.relabelButton(but2, "OFF");
       myButtons.drawButton(but2);
       Status2 = 0;
      }
         
      if ((pressed_button==but3) && (Status3 == 0 )) {
       myButtons.relabelButton(but3, "ON");
       myButtons.drawButton(but3);
       Status3 = 1;
      }
      else {
       myButtons.relabelButton(but3, "OFF");
       myButtons.drawButton(but3);
       Status3 = 0;
      }

      if ((pressed_button==but4) && (Status4 == 0 )) {
       myButtons.relabelButton(but4, "ON");
       myButtons.drawButton(but4);
       Status4 = 1;
      }
      else {
       myButtons.relabelButton(but4, "OFF");
       myButtons.drawButton(but4);
       Status4 = 0;
      }   
    }
  }   
}





 
 
 

ghlawrence2000

#1
Today at 07:10 am Last Edit: Today at 07:19 am by ghlawrence2000
Code: [Select]
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_Buttons.h>
//#include <UTFT_tinyFAT.h>
//#include <tinyFAT.h>

extern uint8_t SmallFont[];
extern uint8_t BigFont[];

// Remember to change the model parameter to suit your display module!
UTFT    myGLCD(CTE70CPLD, 28, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);

// Finally we set up UTFT_Buttons :)
UTFT_Buttons  myButtons(&myGLCD, &myTouch);
// Finally we set up UTFT_tinyFAT :)
//UTFT_tinyFAT myFiles(&myGLCD);

void setup()
{
  //file.initFAT();

  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.fillScr(VGA_GRAY);

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  //myFiles.loadBitmap(30, 100, 300, 300, "power.raw");
  //myFiles.loadBitmap(100, 400, 700, 50,"shutdown.bmp"); // <=== Good luck with that! ;)

}

void loop()

{
  int but1, but2, but3, but4, but5, but6, butX, butY, pressed_button, x, y;
  int Status1 = 0, Status2 = 0, Status3 = 0, Status4 = 0; // <== ! You had not initialised these

  // Statusbar UP
  myGLCD.drawRect(0, 1, 798, 40);
  myGLCD.setBackColor(VGA_GRAY);
  myGLCD.print( "=== Header ===", CENTER, 12);

  // Statusbar DOWN
  myGLCD.drawRect(0, 439, 798, 479);
  myGLCD.print( "=== Footer ===", CENTER, 450);
  //myGLCD.setBackColor(VGA_GRAY);

  // Menu
  myButtons.setTextFont(BigFont);
  myButtons.setButtonColors(VGA_BLACK, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_WHITE);
  but1 = myButtons.addButton(10,    100, 200,  70, "Relay 1");
  but2 = myButtons.addButton(220,   100, 180,  70, "Relay 2");
  but3 = myButtons.addButton(10,    180, 200,  70, "Relay 3");
  but4 = myButtons.addButton(220,   180, 180,  70, "Relay 4");


  //but5 = myButtons.addButton(409, 100, 379,  70, "LED slang");
  myButtons.drawButtons();

  while (true)
  {
    if (myTouch.dataAvailable())
    {
      pressed_button = myButtons.checkButtons();

      if (pressed_button == but1)
        if (Status1 == 0 ) {
          myButtons.relabelButton(but1, "ON");
          myButtons.drawButton(but1);
          Status1 = 1;
        }
        else
        {
          myButtons.relabelButton(but1, "OFF");
          myButtons.drawButton(but1);
          Status1 = 0;
        }

      if (pressed_button == but2)
        if (Status2 == 0 ) {
          myButtons.relabelButton(but2, "ON");
          myButtons.drawButton(but2);
          Status2 = 1;
        }
        else {
          myButtons.relabelButton(but2, "OFF");
          myButtons.drawButton(but2);
          Status2 = 0;
        }

      if (pressed_button == but3)
        if (Status3 == 0 ) {
          myButtons.relabelButton(but3, "ON");
          myButtons.drawButton(but3);
          Status3 = 1;
        }
        else {
          myButtons.relabelButton(but3, "OFF");
          myButtons.drawButton(but3);
          Status3 = 0;
        }

      if (pressed_button == but4)
        if (Status4 == 0 ) {
          myButtons.relabelButton(but4, "ON");
          myButtons.drawButton(but4);
          Status4 = 1;
        }
        else {
          myButtons.relabelButton(but4, "OFF");
          myButtons.drawButton(but4);
          Status4 = 0;
        }
    }
  }
}


Look at what your 'if' statements were actually doing the way you had written them.... Because all of them had 2 outcomes, if or else.... one or the other would ALWAYS happen....... But what you actually wanted was those 2 outcomes ONLY for the button it referred to..... Hence the nested 'if' works..... Because only when button 2 is pressed, do you care what the status of button 2 is?? Hope that makes sense.

Regards, Merry Christmas! :D

Graham

Go Up