Unwanted symbols in LCD after button push

Hi,
I’m new in arduino, but everything is going pretty good except one thing. I have made aquarium controller which is showing temperature, time, has timer function for lights with pause (turns off at midday or other fixed time) and turns off filter for 30 min when pin 6 button is pushed. For lcd and DS18B20 I am using I2C. When i start arduino everything is ok, I push button pin 6 and filter is off for 30 min. The problem is that when i push button after some time, lcd shows something like -1000 temperature for second and after that it shows temperature again and extra 00C. I think the problem might be in the code, so here it is:

#include <Wire.h> 
#include <OneWire.h> 
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <RTClib.h>
#include <TimeAlarms.h>

//LCD
  byte termometer[8] = //icon for termometer
{
    B00100,
    B01010,
    B01010,
    B01110,
    B01110,
    B11111,
    B11111,
    B01110
};

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //set the LCD address to 0x27 for a 16 chars and 2 line display

//Temperature chip i/o
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin);  // on digital pin 2

//Timer
RTC_DS1307 RTC;
 
  // Start Time
int sHour = 06;
int sMinute = 30;
int sSecond = 00;

  // End Time
int eHour = 23;
int eMinute = 59;
int eSecond = 00;

  //Light pause at midday
int spHour = 11; //start of pause
int spMinute = 00;
int spSecond = 00;
int epHour = 17; //end of pause
int epMinute = 15;
int epSecond = 00;
 
int lightRelay = 4;

//Filter off for some time
int pinButton6 = 6;
int filterRelay = 5;
int stateRelay = LOW;
int stateButton6;
int previous6 = LOW;
long time = 0;
long debounce = 500;
long stayON = 1800000; //For how much time to set filter off ms
long previousMillis = 0;

void setup(void) {
  Serial.begin(9600);
  lcd.begin(16,2);                      // initialize the lcd
  //lcd.setBacklight(HIGH);
// Print a message to the LCD.
  //lcd.backlight();
  //lcd.setCursor(0, 0);
  //lcd.print("Temperatura");
//  lcd.setCursor(0, 1); 
//  lcd.print("Second Line");

  lcd.createChar(1,termometer);
  Wire.beginTransmission(0x68);
  Wire.write(0x07); // move pointer to SQW address
  Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();

//Timer
  // Set the relay to off immediately
  digitalWrite(lightRelay, LOW);
   
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
   
  // Set the pinmode
  pinMode(lightRelay, OUTPUT);
   
  // Notify if the RTC isn't running
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running");
  }
 
  // Get time from RTC
  DateTime current = RTC.now();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (current.unixtime() < compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));    
  }
    
  // Use RTC time to set the start time
  setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
  time_t s = now();
 
  // Use RTC time to set the end time 
  setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());   
  time_t e = now();

    //PAUSE Use RTC time to set the start time
  setTime(spHour, spMinute, spSecond, current.day(), current.month(), current.year());
  time_t sp = now();
 
  //PAUSE Use RTC time to set the end time 
  setTime(epHour, epMinute, epSecond, current.day(), current.month(), current.year());   
  time_t ep = now();
   
  // Use RTC time to set the current time
  setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());  
  time_t n = now();
 
  // Test if grow light should be on
  if (s <= n <= sp && ep <= n <= e) {
    digitalWrite(lightRelay, HIGH);  // Sets the grow light "on"
  }else
 /* if (e <= n <= s)*/ {
    digitalWrite(lightRelay, LOW);
    }
     // && n <= sp && ep <= n
  Alarm.alarmRepeat(sHour, sMinute, sSecond, LightOn);
  Alarm.alarmRepeat(spHour, spMinute, spSecond, LightOff); //PAUSE
  Alarm.alarmRepeat(epHour, epMinute, epSecond, LightOn);
  Alarm.alarmRepeat(eHour, eMinute, eSecond, LightOff);


//Filter off for some time
  pinMode(pinButton6, INPUT);
  pinMode(filterRelay, OUTPUT);
}

void loop() {
//Temperature
  float temperature = getTemp();
  Serial.println(temperature);
  lcd.setCursor(0, 0);
  lcd.write(1);
  lcd.setCursor(1, 0); 
  lcd.print(temperature);
  lcd.print((char)223); //degree 223
  lcd.print("C");
  delay(500); //just here to slow down the output so it is easier to read 

//Timer
  DateTime now = RTC.now();
  setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
   
  Serial.print("Current time: ");
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  lcd.setCursor(0, 1);
if (now.hour()<10){  // Add a zero, if necessary, as above
  lcd.print(0);
 }
  lcd.print(now.hour(), DEC);
  lcd.print(':');
if (now.minute()<10){  // Add a zero, if necessary, as above
  lcd.print(0);
 }
  lcd.print(now.minute(), DEC);
  lcd.print(':');
if (now.second()<10){  // Add a zero, if necessary, as above
  lcd.print(0);
 }
  lcd.print(now.second(), DEC);
 // Serial.println();
  Alarm.delay(500);

//Filter off for some time
     //Reading stateButton6 and setting delay start (count)   
     stateButton6 = digitalRead(pinButton6);
if(stateButton6 == HIGH && previous6 == LOW && millis() - time > debounce) {  
     if(stateRelay == LOW){
     digitalWrite(filterRelay, HIGH);
     } //else {
      //digitalWrite(filterRelay, LOW);
      //}                    
        time = millis();
        previousMillis = millis();
  }
    previous6 == stateButton6;
    //Checking if off time has passed
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= stayON) {
      digitalWrite(filterRelay, LOW);
    }
}
  
//Temperature
  float getTemp(){ //returns the temperature from one DS18S20 in DEG Celsius
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) { //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad
  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
  return TemperatureSum;
}

//Timer
void LightOn() {
  Serial.println("Turning Light On");
  digitalWrite(lightRelay, HIGH);
}
 
void LightOff() {
  Serial.println("Turning Light Off");
  digitalWrite(lightRelay, LOW);
}

In serial monitor everything is ok. I think its my coding mistake (might be more than one) but I cant find it.


Thank you for your help.

Grazvis:
The problem is that when i push button after some time, lcd shows something like -1000 temperature for second and after that it shows temperature again and extra 00C.

The only way something shows up on the LCD is if you write to it.

Track down every place where that is done.

Well I understand but everything is ok before I push the button. After push it shows crazy -1000 and gets back to normal temp with extra symbols. The last code edit was with filter function and I haven't done anything with writing on lcd.

(deleted)

What about this in getTemp() if you are looking for mysterious appearances of 1000 :

if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

6v6gt,
yes, this is the “bad” code part that is causing this “problem”. Just because -10000C is longer than usual temp value, leaving 00C. Thank you for ideas.

Hi,
You are over writing your LCD display, you need to clear or put spaces to clear the old value, before writing the new.
If you overwrite a value that is 3figures with a digit that is 2digit, you overwrite only 2, the third is still left in the display.

Hope this helps... Tom.. :slight_smile:

Yes, you described what I wanted to say with my last post :slight_smile: -1000 is longer than usual temp, so the last symbols are left, because they are not deleted or overwritten.

Suggestion: Change the 3 occurrences of -1000 in function getTemp() to -9. That is still an unlikely temperature for a fish tank, but a sufficient warning for you that the DS18S20 cannot be found.
The display overwrite problem will probably then go away.

Thank you for suggestions. There are few ways for solving this :slight_smile: Main idea - overwrite or use shorter line to hide it under usual temp value (or use spaces/other characters) :slight_smile: Thanks. I just was wondering where LCD gets these symbols. Need more attention on the code. :slight_smile: