Hi everyone,
I'm new to arduino and coding in general, but I'm really enjoying it so far. I've been slowly working my up starting from the basic examples. I then moved onto using the ds18b20 temp sensor to display min max and current temp for two sensors on a 20x4 lcd.
I have everything working how I like, except after running for some time (not sure how long, few hours or more), I end up with a screen full of scrambled characters. See the photos.
How it looks when it's working:
When it scrambles:
The code:
/* YourDuino.com Example: Multiple DS18B20 Temperature Sensors
Displayed on 4x20 character LCD display
DS18B20 Pinout (Left to Right, pins down, flat side toward you)
- Left = Ground
- Center = Signal (Pin 10): (with 3.3K to 4.7K resistor to +5 or 3.3 )
- Right = +5 or +3.3 V
/*-----( Import needed libraries )-----*/
// Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html
//Get DallasTemperature Library here: http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
//Download: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move original LiquidCrystal library elsewhere, copy this in it's place
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal.h>
//-----( Declare Constants and Pin Numbers )-----
#define ONE_WIRE_BUS 3 // Data wire is plugged into port 3 on the Arduino (can be changed)
//I2C LCD ONLY - UNCOMMENT
/*#define I2C_ADDR 0x27 // Define I2C Address for the PCF8574A
//---(Following are the PCF8574 pin assignments to LCD connections )----
// This are different than earlier/different I2C LCD displays
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define LED_OFF 0
#define LED_ON 1*/
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass address of our oneWire instance to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// DS18B20 Probes
DeviceAddress Probe1 = {
0x28, 0xC5, 0xEC, 0x34, 0x05, 0x00, 0x00, 0xE2 }; // ROOM
DeviceAddress Probe2 = {
0x28, 0x6B, 0x17, 0xCB, 0x04, 0x00, 0x00, 0x3B }; // TANK
LiquidCrystal lcd(8, 7, 5, 4, 16, 2);
float nowTemp1;
float nowTemp1w;
float nowTemp1d;
float minTemp1=255;
float minTemp1w;
float minTemp1d;
float maxTemp1=-255;
float maxTemp1w;
float maxTemp1d;
float nowTemp2;
float nowTemp2w;
float nowTemp2d;
float minTemp2=255;
float minTemp2w;
float minTemp2d;
float maxTemp2=-255;
float maxTemp2w;
float maxTemp2d;
void setup() /****** SETUP: RUNS ONCE ******/
{
//------- Initialize the Temperature measurement library--------------
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe1, 12);
sensors.setResolution(Probe2, 12);
//---------------- Initialize the lcd ------------------
lcd.begin (20,4); // 20 characters, 4 lines
float temp1 = sensors.getTempC(Probe1);
float maxTemp1 = temp1;
float minTemp1 = temp1;
float temp2 = sensors.getTempC(Probe2);
float maxTemp2 = temp2;
float minTemp2 = temp2;
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
sensors.requestTemperatures(); // Send the command to get temperatures
Probe1Loop();
Probe2Loop();
displayTemperature1Loop();
displayTemperature2Loop();
rounding1Loop();
rounding2Loop();
}
void displayTemperature1Loop()
{
lcd.setCursor(2,0);
lcd.print("ROOM");
lcd.setCursor(0,1);
lcd.print("Now ");
{
if (nowTemp1 == -127) lcd.print("Error");
else{
lcd.print(nowTemp1w,1);
lcd.setCursor(7,1);
lcd.print(nowTemp1d,0);
}
}
lcd.setCursor(0,2);
lcd.print("Max ");
{
if (maxTemp1 == -127) lcd.print("Error");
else{
lcd.print(maxTemp1w,1);
lcd.setCursor(7,2);
lcd.print(maxTemp1d,0);
}
}
lcd.setCursor(0,3);
lcd.print("Min ");
{
if (minTemp1 == -127) lcd.print("Error");
else{
lcd.print(minTemp1w,1);
lcd.setCursor(7,3);
lcd.print(minTemp1d,0);
}
}
}
void displayTemperature2Loop()
{
lcd.setCursor(14,0);
lcd.print("TANK");
lcd.setCursor(12,1);
lcd.print("Now ");
{
if (nowTemp2 == -127) lcd.print("Err");
else{
lcd.print(nowTemp2w,1);
lcd.setCursor(19,1);
lcd.print(nowTemp2d,0);
}
}
lcd.setCursor(12,2);
lcd.print("Max ");
{
if (maxTemp2 == -127) lcd.print("Err");
else{
lcd.print(maxTemp2w,1);
lcd.setCursor(19,2);
lcd.print(maxTemp2d,0);
}
}
lcd.setCursor(12,3);
lcd.print("Min ");
{
if (minTemp2 == -127) lcd.print("Err");
else{
lcd.print(minTemp2w,1);
lcd.setCursor(19,3);
lcd.print(minTemp2d,0);
}
}
}
//Temp Calculations Probe 1
void Probe1Loop()
{
float temp1 = sensors.getTempC(Probe1);
nowTemp1=temp1;
if (temp1 > maxTemp1) maxTemp1 = temp1;
if (temp1 < minTemp1) minTemp1 = temp1;
}
//Temp Calculations Probe 2
void Probe2Loop()
{
float temp2 = sensors.getTempC(Probe2);
nowTemp2=temp2;
if (temp2 > maxTemp2) maxTemp2 = temp2;
if (temp2 < minTemp2) minTemp2 = temp2;
}
void rounding1Loop()
{
int temp1r = nowTemp1 * 10 + 0.5;
nowTemp1w=temp1r/10;
nowTemp1d=temp1r%10;
int maxTemp1r = maxTemp1 * 10 + 0.5;
maxTemp1w = maxTemp1r/10;
maxTemp1d = maxTemp1r%10;
int minTemp1r = minTemp1 * 10 + 0.5;
minTemp1w = minTemp1r/10;
minTemp1d = minTemp1r%10;
}
void rounding2Loop()
{
int temp2r = nowTemp2 * 10 + 0.5;
nowTemp2w=temp2r/10;
nowTemp2d=temp2r%10;
int maxTemp2r = maxTemp2 * 10 + 0.5;
maxTemp2w = maxTemp2r/10;
maxTemp2d = maxTemp2r%10;
int minTemp2r = minTemp2 * 10 + 0.5;
minTemp2w = minTemp2r/10;
minTemp2d = minTemp2r%10;
}
//*********( THE END )**********
I have tried using both 2.2k and 4.7k pullup resistors, with no change. I have also tried both sensors individually, same results again. Not sure it it's a hardware fault or more likely, my noob coding.
Really stuck on this, any help would be much appreciated.