Mega2560 + 3.2 TFT Touch, automated terrarium control + manual buttons

Hi,

I started Arduino programming a few days ago, because a coworker asked me if I wanted to use it to control my terrariums in a convenient way. Well, I said yes and I don’t regret it so far. Until now I used a RaspberryPi and cronjob to switch the lights, which didn’t work very well.
But now it seems I am stuck, so I hope some of you are willing to give me an advice.

My setup:

What am I trying to do?

  • automatically switch the lights in my terrariums on/off, while simulating the different seasons (no light during winter, longest during summer, etc.) → this is the main reason why I don’t want to use standard time switches, since they ususally do not allow many different times depending on the date
  • print buttons on the touch screen so I can manually switch additional remote sockets (e.g. TV, PC, lights, …)

Right now, the buttons are used in case something goes wrong. So I got one button that switches everything in the terrariums on, another one that switches everything off and one that doesn’t do anything so far.

My problem:

  • the timer works just fine. The lights are switched on/off as I want them to.
  • BUT I can’t use the buttons. In the examples the buttons are usually implemented using an infinite while loop, but I can’t use this because the timer wouldn’t work if I did. The if-functions to switch the lights are not included in the while loop that controls the buttons, so if the while loop is infinite, the if-functions would never be reached.

I guess one of the problems is that I don’t draw the buttons during the execution of the “void loop” function. But if i do that, they are drawn continously, resulting in flickering buttons. But even if I add the “Draw Buttons” part to the "void loop"function, it doesn’t work. I can press the button, it even shows the red frame around the button while pressed, but it doesn’t execute the commands.

I included my code (with part of the timer cut out to keep it short). The important part is right at the beginning of the void loop function, where I tried to make the buttons work.

P.S, - just in advance:

  1. Sorry for the long post, but I wanted to make my problem as clear as possible, and I hope it worked…
  2. Sorry for my english, but as you may already have noticed, it’s not my mothers tongue. I am trying to improve though, so if you spot any mistakes, feel free to tell me. :wink:
    (3. I hope my code is readable, I tried to keep it structured.)
//Include Libraries
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_Buttons.h>
#include <DS1307.h>
#include <RCSwitch.h>

//Initialise everything
UTFT myGLCD(ITDB32S,38,39,40,41);
UTouch myTouch(6,5,4,3,2);
UTFT_Buttons myButtons(&myGLCD, &myTouch);
DS1307 rtc(A0,A2);
RCSwitch mySwitch = RCSwitch();
Time t;

//Declare the used fonts
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

//--------------Setup everything--------------
//Setup Function
void setup() {
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_LOW);
  myGLCD.setFont(SmallFont);
  myGLCD.setBackColor(0,0,0);
  myButtons.setTextFont(BigFont);
  
  //Draw fixed parts of date and time  
  myGLCD.print("|",175,215);
  myGLCD.print("|",257,215);
  myGLCD.drawRoundRect(140,208,315,232);
  
  //Settings Transmitter  
  mySwitch.enableTransmit(8);
  mySwitch.setRepeatTransmit(10);
  
  //Status Messages
  myGLCD.drawRoundRect(10,80,265,190);
  myGLCD.print("Status Roehren Reptilien:",20,90);
  myGLCD.print("Status Spots Reptilien:",20,110);
  myGLCD.print("Status Roehren Froesche:",20,130);
  myGLCD.print("Status Spot Froesche:",20,150);
  myGLCD.print("Status Nachtlicht Leos:",20,170);
  
  //Add the buttons  
  int off, on, other, pressed_button;
  off = myButtons.addButton(10,10,90,50,"Aus");
  on = myButtons.addButton(115,10,90,50,"An");
  other = myButtons.addButton(220,10,90,50,"Sonst");
  myButtons.drawButtons();
}

//--------------Loop Function--------------

void loop() {
  
  int off, on, other, pressed_button;
    
  t = rtc.getTime();
  
  while (myTouch.dataAvailable()==true) {
    
    pressed_button=myButtons.checkButtons();
      
    if (pressed_button==off) {
        
        roehrenrepaus();
        roehrenfraus();
        roehrenfraus();
        spotfraus();
        spotrepaus();
    }
    
    if (pressed_button==on) {
        
        roehrenrepan();
        roehrenfran();
        roehrenfran();
        spotfran();
        spotrepan();
    }
  } 
//--------------Timer--------------
  //Geckos NL
  if (t.hour==22 && t.min==02) {
    nlleoan();
  }
  if (t.hour==1 && t.min==00) {
    nlleoaus();
  }
  
  //January
  if (t.mon==1) {
    
    //Frogs
    if (t.hour==10 && t.min==15) {
      roehrenfran();
    }
    if (t.hour==20 && t.min==45) {
      roehrenfraus();
    }
    
    if (t.hour==10 && t.min==45) {
      spotfran();
    }
    if (t.hour==20 && t.min==15) {
      spotfraus();
    }
    
    //Other terrariums
    //15. - 16.
    if (t.date==15 || t.date==16) {
            
[Cut out to keep it short]

  //December
  if (t.mon==12) {
    
    if (t.hour==10 && t.min==00) {
      roehrenfran();
    }
    if (t.hour==21 && t.min==00) {
      roehrenfraus();
    }
    
    if (t.hour==10 && t.min==30) {
      spotfran();
    }
    if (t.hour==20 && t.min==30) {
      spotfraus();
    }
  }  
  
//--------------Display Date and Time--------------    
    myGLCD.print(rtc.getDOWStr(FORMAT_SHORT),150,215);
    myGLCD.print(rtc.getDateStr(FORMAT_SHORT),190,215);
    myGLCD.print(rtc.getTimeStr(FORMAT_SHORT),270,215);
}

//--------------Used functions--------------
void roehrenrepan() {
  myGLCD.print("An ",230,90);
  mySwitch.switchOn("11000", "01000");
  delay(1000);
  mySwitch.switchOn("10001", "00100");
  delay(1000);
  mySwitch.switchOn("11000", "00100");
}

void roehrenrepaus() {
  myGLCD.print("Aus",230,90);
  mySwitch.switchOff("11000", "01000");
  delay(1000);
  mySwitch.switchOff("10001", "00100");
  delay(1000);
  mySwitch.switchOff("11000", "00100");
}

void spotrepan() {
  myGLCD.print("An ",230,110);
  mySwitch.switchOn("11000", "10000");
  delay(1000);
  mySwitch.switchOn("10001", "00010");
  delay(1000);
  mySwitch.switchOn("11000", "00010");
}

void spotrepaus() {
  myGLCD.print("Aus",230,110);
  mySwitch.switchOff("11000", "10000");
  delay(1000);
  mySwitch.switchOff("10001", "00010");
  delay(1000);
  mySwitch.switchOff("11000", "00010");
}

void roehrenfran() {
  myGLCD.print("An ",230,130);
  mySwitch.switchOn("10001", "10000");
}

void roehrenfraus() {
  myGLCD.print("Aus",230,130);
  mySwitch.switchOff("10001", "10000");
}

void spotfran() {
  myGLCD.print("An ",230,150);
  mySwitch.switchOn("10001", "01000");
}

void spotfraus() {
  myGLCD.print("Aus",230,150);
  mySwitch.switchOff("10001", "01000");
}

void nlleoan() {
  myGLCD.print("An ",230,170);
  mySwitch.switchOn("11001", "10000");
}

void nlleoaus() {
  myGLCD.print("Aus",230,170);
  mySwitch.switchOff("11001", "10000");
}

If the buttons are giving you issues, you can try my library, it works with UTFT and UTouch. Do a search for TFT_Extension in either the forum or arduino playground. I included plenty of examples. Give it a try.

Thank you for the answer, but that didn’t really help me. If I use the examples for the “standard” button library, they work just fine. I can adjust position and size, edit the text, give them something to do when pressed, it does work. The problem occurs when I try to add other functions, thus editing the infinite while loop to a while loop that only is active if a button is pressed. But I don’t see why using your library would change that, since it is used in a very similar way…

So I guess the problem is how I am trying to call the while loop so it only is active during a button press, not the buttons themselves.

Edit: Fixed it finally. All I had to do was declare the “off, on, other” int variables as global.