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


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() {
  //Draw fixed parts of date and time  
  //Settings Transmitter  
  //Status Messages
  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");

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

void loop() {
  int off, on, other, pressed_button;
  t = rtc.getTime();
  while (myTouch.dataAvailable()==true) {
    if (pressed_button==off) {
    if (pressed_button==on) {
  //Geckos NL
  if (t.hour==22 && t.min==02) {
  if (t.hour==1 && t.min==00) {
  if (t.mon==1) {
    if (t.hour==10 && t.min==15) {
    if (t.hour==20 && t.min==45) {
    if (t.hour==10 && t.min==45) {
    if (t.hour==20 && t.min==15) {
    //Other terrariums
    //15. - 16.
    if ( || {
[Cut out to keep it short]

  if (t.mon==12) {
    if (t.hour==10 && t.min==00) {
    if (t.hour==21 && t.min==00) {
    if (t.hour==10 && t.min==30) {
    if (t.hour==20 && t.min==30) {
//--------------Display Date and Time--------------    

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

void roehrenrepaus() {
  mySwitch.switchOff("11000", "01000");
  mySwitch.switchOff("10001", "00100");
  mySwitch.switchOff("11000", "00100");

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

void spotrepaus() {
  mySwitch.switchOff("11000", "10000");
  mySwitch.switchOff("10001", "00010");
  mySwitch.switchOff("11000", "00010");

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

void roehrenfraus() {
  mySwitch.switchOff("10001", "10000");

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

void spotfraus() {
  mySwitch.switchOff("10001", "01000");

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

void nlleoaus() {
  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.