0
votes

I'm having a strange problem that I hope someone can help with. My sketch works perfectly when uploaded to my UNO, but when I unplug it and plug it back in, it doesn't work correctly. If I re-upload it, it works again until power is cycled. Once uploaded, the LCD reads:

Ferm:73.4  73/75
Room:75.1  75/75

After cycling power:

Ferm:73.45 73/18
Room:74.83 75/18

So after cycling power, I now get 2 decimal places and the "high" temp is stuck at "18".

 /*
  The circuit:
 * 5V to Arduino 5V pin
 * GND to Arduino GND pin
 * CLK to Analog #5
 * DAT to Analog #4
*/

// include the library code:
#include "Wire.h"
#include "Adafruit_LiquidCrystal.h"
#include <OneWire.h>
#include <DallasTemperature.h>

//variables for temp readings
float fermTemp;
float fermTempL=100;
float fermTempH=5;
float roomTemp;
float roomTempL=100;
float roomTempH=5;

// set OneWire bus to digital PIN 4 on the Arduino
#define ONE_WIRE_BUS 4

// Setup OneWire instance
OneWire oneWire(ONE_WIRE_BUS);

// Pass oneWire reference to Dallas Temp
DallasTemperature sensors(&oneWire);

// Connect via i2c, default address #0 (A0-A2 not jumpered)
Adafruit_LiquidCrystal lcd(0);

void setup() 
{
  // set up the LCD's number of rows and columns: 
  lcd.begin(16, 2);
  // turn on backlight
  lcd.setBacklight(HIGH);
}

void loop() {
 readtemp();
 LCDPrint(); 
}

void readtemp()
{
  // get data from sensors
  sensors.requestTemperatures();
  fermTemp = (sensors.getTempFByIndex(0));
  roomTemp = (sensors.getTempFByIndex(1));
  // check/set High and Low temp
  if (fermTemp<fermTempL) {
    fermTempL=fermTemp;
  }
  if (fermTemp>fermTempH) {
    fermTempH=fermTemp;
  }
  if (roomTemp<roomTempL) {
    roomTempL=roomTemp;
  }
  if (roomTemp>roomTempH) {
    roomTempH=roomTemp;
  }
}
  void LCDPrint() 
  {
  lcd.setCursor(0,0);  
  lcd.print("Ferm:");
  lcd.print(fermTemp,1);
  lcd.setCursor(11,0);
  lcd.print(fermTempL,0);
  lcd.print("/");
  lcd.print(fermTempH,0);
  lcd.setCursor(0,1);
  lcd.print("Room:");
  lcd.print(roomTemp,1);
  lcd.setCursor(11,1);
  lcd.print(roomTempL,0);
  lcd.print("/");
  lcd.print(roomTempH,0);
 }
1
That's weird.. What about resets? If you power it up and then press the reset button? If it works, try putting a delay in the setup function, then reset the LCD and the temperature sensors. If it doesn't, try it anyway. In any case.. I usually saw the opposite behavior (working at boot and not after a reset)frarugi87
Thanks frarugi87. Pressing the reset button worked. I will try adding a delay in the setup function, but I'm not sure what you mean by resetting the LCD and temp sensors.MarkZ
there should be some initializing code to set everything up at startup. I don't know the adafruit library nor the dallas sensor, but I think that there should be. Try reading the libraries documentation and/or the datasheetsfrarugi87

1 Answers

0
votes

I counted your characters, 16 per line. If you have a 16X2 display, there might be characters printed beyond the screen. I suspect it was not really 18, but something larger, say 180 or 1800. That could have been a result of failed first attempt to read temperature. This reading is stuck with you as temp High. In your code, you should define a reasonable high temperature, such as 125. Don't update temp High if it is above the reasonable temperature.

To confirm it, print temp High to serial port and inspect the value.