Issue : Adding additional code to Void Loop .....

Hi,
I’ve been working on the following code for several weeks now as a first project.
Everything is working out slowely, but when i added a new peice of code to the void loop to add temperature probe then the touch screen buttons no longer work . :frowning:
Im guessing for some reason its not going back to the top of the loop, :S…
im a newbie at this so the code below im sure is not tidy or well written.
Any changes or help would be appreciated.

#include <UTFT.h>
#include <UTouch.h>
#include <DS1307.h>
#include <Servo.h>

/*************************
**   New Code for Temp   **
*************************/

#include <OneWire.h>
#define RELAY_PIN 3
#define RELAY_PIN1 6
#define ledPin2 5
#define ledPin3 4


// Init the DS1307
DS1307 rtc(20, 21);

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)



// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];


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

int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";
int ledPin =  13;
int relayPin1 = 8; // for SKIMMER
int relayPin2 = 9; // for PUMP
int relayPin3 = 11; // for EXT FILTER
int reset = 30; // reset Button

int ledPin1 = 2;  // LED Blue for Cold
//int ledPin2 = 5; // LED RED for HOT
//int ledPin3 = 4; // LED Green for Normal
int fanPin1 = A1; //Fan pin

long num;

char num1;

float tempW = 0;



/*************************
**   Custom functions   **
*************************/

void drawButtons()
{
// Draw the buttons
 
 // Setting title with blue colour and big text
myGLCD.setBackColor(0x0000);
myGLCD.setFont(SmallFont);
myGLCD.setColor(0xC618);
myGLCD.print("Aquarium Information", 60, 0);


//draw button 1 TIME 
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (1, 15, 155, 65);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (1, 15, 155, 65);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xFD20);
  myGLCD.print("TIME:", 10, 20);

//draw button 2  DATE 
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (160, 15, 310, 65);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (160, 15, 310, 65);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xFD20);
  myGLCD.print("DATE:", 170, 20);


//draw button 3 Water Temperature 
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (1, 70, 155, 125);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (1, 70, 155, 125);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xFD20);
  myGLCD.print("Water Temperature:", 10, 75);


//draw button 4  Feed Fish
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (160, 70, 310, 125);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (160, 70, 310, 125);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xFD20);
  myGLCD.print("Feed Fish:", 170, 85);


//draw button 5 PH 
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (1, 130, 155, 175);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (1, 130, 155, 175);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xFD20);
  myGLCD.print("PH LVL:", 10, 140);
  
  
//draw button 6  Reset Button
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (160, 130, 310, 175);
  myGLCD.setColor(0xC618);
  myGLCD.drawRoundRect (160, 130, 310, 175);
  myGLCD.setBackColor(0x0000);
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0xF800);
  myGLCD.print("RESET", 170, 140);
  
  
 //draw button 7 SKIMMER
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (1, 180, 100, 230);
  myGLCD.setColor(0x07FF);
  myGLCD.drawRoundRect (1, 180, 100, 230);
  myGLCD.setBackColor(0x0000);
  myGLCD.print("SKIMMER", 20, 190);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0x07FF); 
  myGLCD.print("ON", 10, 210);
  myGLCD.setColor(0x07E0);
  myGLCD.setColor(0x07FF);
  myGLCD.print("OFF", 50, 210);
  
//draw button 8 PUMP
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (105, 180, 200, 230);
  myGLCD.setColor(0x07FF);
  myGLCD.drawRoundRect (105, 180, 200, 230);
  myGLCD.print("PUMP", 135, 190);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0x07FF);
  myGLCD.print("ON", 110, 210);
  myGLCD.setColor(0x07FF);
  myGLCD.print("OFF", 150, 210);
 
 
 
 //draw button 8 External Filter
  myGLCD.setFont(SmallFont);
  myGLCD.setColor(0x0000);
  myGLCD.fillRoundRect (205, 180, 310, 230);
  myGLCD.setColor(0x07FF);
  myGLCD.drawRoundRect (205, 180, 310, 230);
  myGLCD.print("EXT FILTER", 215, 190);
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0x07FF);
  myGLCD.print("ON", 210, 210);
  myGLCD.setColor(0x07FF);
  myGLCD.print("OFF", 260, 210);
  
}

void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}


/*************************
**  Required functions  **
*************************/

void setup()
{

  // Set the clock to run-mode
  rtc.halt(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  
  rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
  rtc.setTime(17, 13, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(6, 7, 2013);   // Set the date to October 3th, 2010
  
  // Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);
  drawButtons();
  
  //set pin as output
  pinMode(ledPin, OUTPUT);
 //set relaypin as output
 pinMode(relayPin1, OUTPUT);
 pinMode(relayPin2, OUTPUT);
 pinMode(relayPin3, OUTPUT);
// pinMode(reset, OUTPUT);

  pinMode(RELAY_PIN, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);


}

}

here is the void loop ( sorry code to long to put together )

void loop()
{
  
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
        {
          
          if ((x>=210) && (x<=250) && (y>=210)) // Button: Feed Fish
        {        
           //  servoOne.write(10); // Turn Servo Left to 45 degrees
           //  delay(1000); // Wait 1 second
          //  servoOne.write(170);
           //   delay(1000); // Wait 1 second
           //  servoOne.write(10);
           //delay(1000); // Wait 1 second
           //   servoOne.write(170);
            
            myGLCD.setColor(0x07E0);
            myGLCD.setFont(BigFont);
            myGLCD.print("FEEDING FISH", 170, 85);
            myGLCD.setColor(0x07FF);

        }
          
          if ((x>=250) && (x<=300) && (y>=130)&&(y<=210) ) // Button: RESET
        {        
         reset;  
          //digitalWrite(resetPin, LOW);
           myGLCD.setFont(BigFont);
           myGLCD.setColor(0xF800);
           myGLCD.print("RESET", 170, 140);
  
        }
          
        if ((x>=210) && (x<=250) && (y>=210)) // Button: EXT FILTER ON
        {        
            digitalWrite(relayPin3, HIGH);
            
            myGLCD.setColor(0x07E0);
            myGLCD.setFont(BigFont);
            myGLCD.print("ON", 210, 210);
            myGLCD.setColor(0x07FF);
            myGLCD.print("OFF", 260, 210);

        }
        
        
        
        if ((x>=270) && (x<=295)&& (y>=210)) // Button: EXT FILTER OFF
        {
          
            digitalWrite(relayPin3, LOW);
            
            myGLCD.setColor(0x07FF);
            myGLCD.setFont(BigFont);
            myGLCD.print("ON", 210, 210); 
            myGLCD.setColor(0xF800);
            myGLCD.print("OFF", 260, 210);

        }
        
        if ((x>=105) && (x<=140)&& (y>=210)) // Button: PUMP ON
        {
            digitalWrite(relayPin2, HIGH);
          
            myGLCD.setFont(BigFont);
            myGLCD.setColor(0x07E0);
            myGLCD.print("ON", 110, 210);
            myGLCD.setColor(0x07FF);
            myGLCD.print("OFF", 150, 210);
        }
        
        if ((x>=175) && (x<=200)&& (y>=210)) // Button: PUMP OFF
        {
            digitalWrite(relayPin2, LOW);
            
            myGLCD.setFont(BigFont);
            myGLCD.setColor(0x07FF);
            myGLCD.print("ON", 110, 210);
            myGLCD.setColor(0xF800);
            myGLCD.print("OFF", 150, 210);
        }

          if ((x>=05) && (x<=30)&& (y>=210)) // Button: SKIMMER ON
        {
            digitalWrite(ledPin, HIGH);          
            digitalWrite(relayPin1, HIGH);
            myGLCD.setColor(0x07E0);
            myGLCD.setFont(BigFont);
            myGLCD.print("ON", 10, 210);           
            myGLCD.setFont(BigFont);
            myGLCD.setColor(0x07FF);
            myGLCD.print("OFF", 50, 210);

        }
        
       
        if ((x>=70) && (x<=85)&& (y>=210))  // Button: SKIMMER OFF
        {
          digitalWrite(ledPin, LOW);
          digitalWrite(relayPin1, LOW);
          myGLCD.setColor(0x07FF);
          myGLCD.setFont(BigFont);
          myGLCD.print("ON", 10, 210);
          myGLCD.setColor(0xF800);
          myGLCD.setFont(BigFont);
          myGLCD.print("OFF", 50, 210);
          myGLCD.setFont(BigFont);
       
          if (stCurrentLen>0)
          {
            for (x=0; x<stCurrentLen+1; x++)
            {
              stLast[x]=stCurrent[x];
            }
            stCurrent[0]='\0';
            stCurrentLen=0;
            myGLCD.setColor(0, 0, 0);
            myGLCD.fillRect(0, 208, 319, 239);
            myGLCD.setColor(0, 255, 0);
            myGLCD.print(stLast, LEFT, 208);
          }
          else
          {
            myGLCD.setColor(255, 0, 0);
            myGLCD.setColor(0, 255, 0);
          }
        }
      }
    }
    
    {
  // Send Day-of-Week
  myGLCD.setFont(BigFont);
  myGLCD.print(rtc.getDOWStr(), 180, 32);
  Serial.print(" ");
  
  // Send date
  myGLCD.setFont(SmallFont);
  myGLCD.print(rtc.getDateStr(), 190, 50);
  Serial.print(" -- ");

  // Send time
  myGLCD.setFont(BigFont);
  myGLCD.setColor(0XA651);
  myGLCD.print(rtc.getTimeStr(), 10, 40);
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating :)
  //delay (500);

here is the code i added to the end of the Void Loop, which resulted in the touch screen buttons to stop working :
( No errors compiling or running )

{
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    //Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

 //Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
   //Serial.write(' ');
   // Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
    //  Serial.println("CRC is not valid!");
      return;
  }
Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
  //  Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
  // Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
  //  Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
   //   Serial.println("Device is not a DS18x20 family device.");
      return;
  } 
  
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

//Serial.print("  Data = ");
//Serial.print(present, HEX);
//Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
  }
//Serial.print(" CRC=");
//Serial.print(OneWire::crc8(data, 8), HEX);
//Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  int tempW = celsius;
  myGLCD.setFont(BigFont);
  myGLCD.printNumI(tempW, 70, 100);
  myGLCD.setFont(SmallFont);
  myGLCD.print(" oC ", 100, 100);
  fahrenheit = celsius * 1.8 + 32.0;
  
  
  //myGLCD.print(rtc.getDateStr(), 190, 50);


  if (celsius >= 26) {
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.print(" Fahrenheit");
  Serial.println(" Temp too high");  
digitalWrite(ledPin1 , LOW);
digitalWrite(ledPin2 , HIGH);
digitalWrite(ledPin3 , LOW);
digitalWrite(fanPin1, LOW);
digitalWrite(RELAY_PIN, HIGH);
}

 else if (celsius < 24.5) {
 Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.print(" Fahrenheit");
  Serial.println(" Temp Too low");
  digitalWrite(ledPin1 , HIGH);
digitalWrite(ledPin3 , LOW);
digitalWrite(RELAY_PIN, LOW);
 }
 else if(celsius < 25.5) {
   Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.print(" Fahrenheit");
  Serial.println(" Temp Normal");
  digitalWrite(ledPin1 , LOW);
digitalWrite(ledPin2 , LOW);
digitalWrite(ledPin3 , HIGH);
digitalWrite(RELAY_PIN, LOW);
 }
 
 else {
  
  
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.print(" Fahrenheit");
  Serial.println(" Temp Normal");
  digitalWrite(ledPin1 , LOW);
  digitalWrite(ledPin2 , HIGH);
  digitalWrite(ledPin3 , LOW);
	}

First, there is no such thing as "the void loop". There is a function called loop. The loop function has a void return type. Secondly, If your code is too big to put in a single post, use the "Additional Options" link to attach it. Lastly, pretend like we don't know anything about your project and give a more details of what you are expecting your code to do, and what it is actually doing.

You need to see exactly where the code is staying at by using the serial monitor if possible. OR use your own screen to display a simple number in the corner of the screen, to tell you where your code is at.

If the numbers continue to loop at a certain spot, then you'll know where the code is getting hung up, and probably why your buttons are not working.

the code is for an aquarium controller.
i have the following attached to the arduino
1: RTC clock
2: temp prob
3: 6 relays.

what ive achieved so far is ive drawn the buttons on the UTFT, and 3 of them control the relays.
got time and date to display on the screen

what im trying to get now is the temperature to display on the screen, and with the code i added the temperature shows ont he screen but then the touch screen buttons no longer function.
When i press the on , the On goes green and increases in size which no longer works after adding the temperature code into the end of the code.
Here is a pic of the code running atm

Controlle-v1.3.jpg

You should consider putting constant strings Serial.print ("xyz"); in program memory using the "F()" macro Serial.print (F("xyz"));

thanks for that suggestion HazardsMinds, ive never actually thought of using the serial.print to track the code. Will give it a shot.

, ive never actually thought of using the serial.print to track the code.

But if you're running short of RAM, it can make the problem worse.
Make sure you use the F() macro.

I may be wrong but you have lots of

return;

in your code
that will cause immediate exit of

loop()

I tried as hazard suggested by putting in Serialprintln at the start, mid and end of code.
I see it is looping around , the temp code i added must be somehow causing a conflict with the touch buttons…

i’ll keep staring at the code to see if i can see what could be causing the issue.
If anyone can notice why this could be happening , would be great.

Anarkie:
I tried as hazard suggested by putting in Serialprintln at the start, mid and end of code.
I see it is looping around , the temp code i added must be somehow causing a conflict with the touch buttons...

During your delays, nothing else will happen (like the touch buttons being read). To get round this, look up 'blink without delay' in the playground.

Get rid of all the delays first!