Making a button change colour upon click


hi. trying achieve following , cannot remember how it. plus want several things trigger it. doubling status buttons, , trigger buttons 4 way relay shield.

(i have arduino mega 3.6" touch screen 4 way mains relay board.)

i have 4 buttons on screen grey, representing off. when touched, want them change individually green , give digitalwrite(powerpin1, high);. when touched again, toggle digital write low , turn grey.

but... of these time triggered when happens, icon have turn green , grey respectively, without physical touch of screen.

i think know write, don't know how. think should along lines:
if powerpin1 high, display green, else display grey... , put triggers in separate entity, sun a. @ 1200pm digital write powerpin1 high , b. on button press powerpin1 high. problem is, don't know how write it, how i've done programming far (please bare me, hadn't looked @ coding language before 3 weeks ago!)

code: [select]

int x, y,g,menu;

int powerpin1 = 31;      // power relay 1
int powerpin2 = 33;      // power relay 2
int powerpin3 = 35;      // power relay 3
int powerpin4 = 37;      // power relay 4

void setup()
{
  myglcd.initlcd(landscape);                   // initialize display
  myglcd.clrscr();                             // clear screen (black)
  mytouch.inittouch(landscape);
  mytouch.setprecision(prec_medium);

  myglcd.clrscr();                            // clear screen (black)
  menu1();
  menu=1;
 
  pinmode(powerpin1, output);      // power relay 1
  pinmode(powerpin2, output);      // power relay 2
  pinmode(powerpin3, output);      // power relay 3
  pinmode(powerpin4, output);      // power relay 4
}

// button coordinates
// menu 1

int x11=25; int y11=178; int x12=75; int y12=227;         // home
int x21=85; int y21=178; int x22=135; int y22=227;        // lighting
int x31=145; int y31=178; int x32=195; int y32=227;       // feeding
int x41=205; int y41=178; int x42=255; int y42=227;       // storm
int x51=265; int y51=178; int x52=315; int y52=227;       // power
int x61=325; int y61=178; int x62=375; int y62=227;       // sleep

int x151=335; int y151=36; int x152=399; int y152=66;     // power 1
int x161=335; int y161=70; int x162=399; int y162=100;     // power 2    four
int x171=335; int y171=104; int x172=399; int y172=134;    // power 3    channel mode
int x181=335; int y181=138; int x182=399; int y182=168;    // power 4   

//=================== home menu =========================

void menu1()    // home
{
  myglcd.clrscr();                          // clear screen (black)

  myglcd.setfont(arial_bold);               // menu layout
    myglcd.setcolor(36, 123, 237);
      myglcd.print("home",35,12);
        myglcd.setbackcolor(36, 123, 237);     
        myglcd.fillroundrect(2,5,31,33);    // draw rounded rectangle
    myglcd.setcolor(235, 239, 255);
    myglcd.setbackcolor(0, 0, 0);           // bg black
      myglcd.drawline(37,33,399,33);        // top horizontal line
      myglcd.drawline(0,171,399,171);       // bottom horizontal line
      myglcd.drawline(110,33,110,171);      // 1st verticle line
      myglcd.drawline(220,33,220,171);      // 2nd verticle line
      myglcd.drawline(330,33,330,171);      // 3rd verticle line   
      myglcd.drawline(0,79,330,79);         // temp line 1
      myglcd.drawline(0,125,110,125);       // temp line 2           
   
//=================== right power buttons =========================

  buttonp9 (x151,y151,x152,y152);   // p1
  buttonp10 (x161,y161,x162,y162);  // p2       four
  buttonp11 (x171,y171,x172,y172);  // p3       channel mode
  buttonp12 (x181,y181,x182,y182);  // p4       
   
     
  menu =1; 
}


void loop()
{

 

// ---- button links ----


  while (true)
  {
    if (mytouch.dataavailable())
    {
      mytouch.read();
      x=mytouch.getx();
      y=mytouch.gety();

 //  example of how navigation links set (there menu 2,3,4 etc, deleted
//   them not relevant this
     
      if ((menu>=1) && (menu<=6))     
       {
        if ((y>=y11) && (y<=y12))  // bottom row
        {     
          if ((x>=x11) && (x<=x12))  // button: home
              {
                waitforit(x11,y11,x12,y12);         
                menu =1;
                menu1();   
              }
          if ((x>=x21) && (x<=x22))  // button: lights
              {
                waitforit(x21,y21,x22,y22);
                menu =2;
                menu2();   
              }
          if ((x>=x31) && (x<=x32))  // button: feed
              {
                waitforit(x31,y31,x32,y32);
                menu =3;
                menu3();   
              }
          if ((x>=x41) && (x<=x42))  // button: storm
              {
                waitforit(x41,y41,x42,y42);         
                menu =4;
                menu4();   
              }
          if ((x>=x51) && (x<=x52))  // button: power
              {
                waitforit(x51,y51,x52,y52);
                menu =5;
                menu5();   
              }
          if ((x>=x61) && (x<=x62))  // button: sleep
              {
                waitforit(x61,y61,x62,y62);
                menu =6;
                menu6();   
              }
             
           }
       }

//   this i'm presuming should go, not sure if below start of whats
//   needed??

  if (powerpin1 == high)
    {
        activate(x151,y151,x152,y152);
    }
      else
        {
          // action b
        }



 
// button setup

// =======power buttons======

void buttonp9(int x1, int y1, int x2, int y2)      // start of 4 channel mode
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp10(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp11(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp12(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}



// change box green
void activate(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(0,255,0);
  myglcd.fillroundrect (x1, y1, x2, y2);

}


i have deleted lots of code it's long , not relevant.

also, there anyway of consolidating parameters below same each of button voids. i've tried wording in different ways can't work.

code: [select]
void buttonp9(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp10(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp11(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}

void buttonp12(int x1, int y1, int x2, int y2)
{
  myglcd.setcolor(130, 130, 130);
  myglcd.fillroundrect (x1,y1,x2,y2);
  myglcd.setcolor(130, 130, 130);
  myglcd.drawroundrect (x1,y1,x2,y2);
}


Arduino Forum > Using Arduino > Programming Questions > Making a button change colour upon click


arduino

Comments