Link button colour to pin output

Hi.

I am using the following libraries

#include <UTFT.h>
#include <UTouch.h>
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <SdFat.h>
#include <UTFT_SdRaw.h>
#include <UTFT_Buttons.h>

And have a 4 way relay shield connected to the following pins

int powerPin1 = 31;      // Power relay 1
int powerPin2 = 33;      // Power relay 2
int powerPin3 = 35;      // Power relay 3
int powerPin4 = 37;      // Power relay 4

They are also setup as follows:

  pinMode(powerPin1, OUTPUT);      // Power relay 1
  pinMode(powerPin2, OUTPUT);      // Power relay 2
  pinMode(powerPin3, OUTPUT);      // Power relay 3
  pinMode(powerPin4, OUTPUT);      // Power relay 4

I basically want buttons on the screen that is grey if the corresponding pin is LOW, but green if the PIN is HIGH. But the button has also got to flip the state of the pin from LOW to HIGH and vice versa. I have been playing around and kind of know what needs to be done but being a beginner, can’t quite seem to get it right. My workings are below (I have only included the stuff I think is relevant.)

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  

void setup()
{  
  pinMode(powerPin1, OUTPUT);      // Power relay 1
  pinMode(powerPin2, OUTPUT);      // Power relay 2
  pinMode(powerPin3, OUTPUT);      // Power relay 3
  pinMode(powerPin4, OUTPUT);      // Power relay 4  
}

void Menu1()    // HOME
{
  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      
}

void loop()
{

if (menu==1)       
  {
     if ((x>=x151) && (x<=x152))       // Right row
        {     
          if ((y>=y151) && (y<=y152))  // Top button
            { 
              if (powerPin1 == LOW)
                {
                  digitalWrite(powerPin1, HIGH);
                }
              else
                {
                  digitalWrite(powerPin1, LOW);
                }
              }
            }
          } 
       }
  }

void buttonP9(int x1, int y1, int x2, int y2)      // START OF FOUR CHANNEL MODE
{
  if (powerPin1 == HIGH)
    {
      myGLCD.setColor(0, 255, 00);
      myGLCD.fillRoundRect (x1,y1,x2,y2);
    }
  else
    {
      myGLCD.setColor(130, 130, 130);
      myGLCD.fillRoundRect (x1,y1,x2,y2);
    }
}

This doesn’t work and I’m hoping its almost there, just need an expert to tweak it. Unless its completely wrong which I hope it isn’t. :cold_sweat:

Many thanks,
Steve

LOL, This just looks too familiar, I already wrote all that earlier this year, persevere, best way to learn!!

;) :P

G

int x151=335;

The meaningfulness of this completely escapes me.

PaulS: int x151=335;

The meaningfulness of this completely escapes me.

??

Something along the lines of this should do you

////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////    Relay handlers    /////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

void procRelays(byte relay)
{
  char relays[][10] = {"swon.raw" , "swoff.raw" };
  if (relay != prevRelay)
  {
    if ((relay & (1 << pwrLight2)) == (1 << pwrLight2)) // Lights 2
    {
      myFiles.loadcpld(555, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(555, 418, 22, 22, relays[0], 1, 16);
    }
    if ((relay & (1 << pwrLight1)) == (1 << pwrLight1)) // Lights 1
    {
      myFiles.loadcpld(580, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(580, 418, 22, 22, relays[0], 1, 16);
    }
    if ((relay & (1 << pwrTemp1)) == (1 << pwrTemp1)) // Temp 1
    {
      myFiles.loadcpld(614, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(614, 418, 22, 22, relays[0], 1, 16);
    }
    if ((relay & (1 << pwrFogger1)) == (1 << pwrFogger1)) // Fog 1
    {
      myFiles.loadcpld(655, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(655, 418, 22, 22, relays[0], 1, 16);
    }
    if ((relay & (1 << pwrMisting1)) == (1 << pwrMisting1)) // Mist 1
    {
      myFiles.loadcpld(694, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(694, 418, 22, 22, relays[0], 1, 16);
    }
    if ((relay & (1 << pwrFan1)) == (1 << pwrFan1)) // Fan 1
    {
      myFiles.loadcpld(732, 418, 22, 22, relays[1], 1, 16);
    }
    else
    {
      myFiles.loadcpld(732, 418, 22, 22, relays[0], 1, 16);
    }
    prevRelay = relay;
    testRelays();
  }
}

And that manages the row of icons in the bottom right corner of this screen. It should NOT be too hard to see how to adapt something like this to your own purposes?

Regards,

Graham

Stevelondon: ??

Later, in the code, what is there that suggests that x151 is the correct variable to be referencing?

buttonOneLeftEdge - is there any doubt what this variable means?

Steve , maybe should clue you in on this also....

http://forum.arduino.cc/index.php?topic=319111.msg2213714#msg2213714

Now I am going to call it a night...

Goodnight

G