Solar water heater code

Hello,
I'm a beginner in Arduino, I took a code from the internet and tried to adapt it to my needs. The code :

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

//These first three variables are where the adjustments are made.

//set maximum tank temperature in C
int tankmax = 65;

//set how much higher collector temperature should be, in C, before turning on pump
int diff = 8;

//Turn off pump whan temp is within this many degrees of the collector
float werethere = 2;

//set up pins for lcd display
LiquidCrystal lcd (5, 6, 7, 8, 9, 10);

// Set input pin for onewire bus
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

//The thermometers are identified by their unique identifier

byte tanktopThermometer[8] = {0x28, 0x4F, 0xB2, 0x46, 0x92, 0x10, 0x02, 0x5D};

byte collectorThermometer[8] = {0x28, 0xAE, 0xEB, 0x46, 0x92, 0x11, 0x02, 0xE1};

static bool pumpOff=false;
//set pin for pump relay
int pump = 3;

//set pin for led pump indicator
int pumpLed = 4;

int pumpstate;

void setup ()
{
//start serial port
Serial.begin(9600);
Serial.println ("Solar hot water Differential Controller");

// Start up the onewire library
sensors.begin();

// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");

// set the sensor resolution from 9 to 12 bit
sensors.setResolution(tanktopThermometer, 9);
sensors.setResolution(collectorThermometer, 9);

//initialize lcd
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print ("CL");
lcd.setCursor (0,1);
lcd.print ("TT");

//set pins to output
pinMode (pump, OUTPUT);
pinMode (pumpLed, OUTPUT);
digitalWrite (pump,HIGH);

}

//functions that reads temp from sensors and returns as floating point value.

float sensorValue (byte deviceAddress[])
{
float tempC = sensors.getTempC (deviceAddress);
return tempC;

}

void loop ()
{

//get all sensor values
sensors.requestTemperatures();

//set variables
float tank = (sensorValue(tanktopThermometer));
float collector = (sensorValue(collectorThermometer));

//prints temperature values to serial port and lcd
Serial.print ("tank ");
Serial.println (tank);
Serial.print ("collector ");
Serial.println (collector);
lcd.setCursor (2,0);
lcd.print (collector);
lcd.setCursor (2,1);
lcd.print (tank);
Serial.print ("pump state ");
Serial.println (pumpstate);
Serial.println ("");

/turns on pump when collector is (diff) degrees above tank.
and tank is below (tankmax) temperature.
once pump turns on, pumpstate variable turns to "1" and causes pump to remain
on as long as collector temp is higher than tank temp.
If max tank temp is reached, pump always turns off.
/

static bool pumpOn=false;
if ( pumpOn)
{
if (((collector - werethere) < tank ) || (tank >= tankmax) )
{
digitalWrite (pump, LOW);
digitalWrite (pumpLed, LOW);
pumpOn =false;
}
}
else
{
if (((collector - diff) > tank ) && ( tank < tankmax ) )
{
digitalWrite (pump, HIGH);
digitalWrite (pumpLed, HIGH);
pumpOn = true;
}
}

delay(1000);

}

  1. One of the sensors displays 4 decimals instead of 2 on the LCD, after the pump LED lights up and sometimes it also displays negative temperatures like -170 degrees which appear in "serial monitor". In the "serial monitor" temperatures appear with only 2 decimal places.

  2. I would like to show the status of the pump on the LCD when it is on or off.

I do not know how to upload LCD photos because it seems that the files are too large

valbo:

  1. One of the sensors displays 4 decimals instead of 2 on the LCD,

Possibly bad formatting. The extra digits are left over from a previous reading. If this is the case, you might simply add a " " to the print command.

I do not know how to upload LCD photos because it seems that the files are too large.

Make the files smaller

Please edit your post to add code tags, as describe in "How to use this forum".

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

Hi,

One of the sensors displays 4 decimals instead of 2 on the LCD, after the pump LED lights up and sometimes it also displays negative temperatures like -170 degrees which appear in "serial monitor". In the "serial monitor" temperatures appear with only 2 decimal places.

Some of this could be due to the fact the when you write a number to the LCD, you are over writing the previous number.
If the previous number was 4 digits and the next 3 digits, you will only over write 3 digits and leave the old 4th digit still displayed.
The solution is to only update the LCD when there is a change in displayed data, but before doing the update, print spaces over the old data, that way the new data will not have remnants of old data displayed.

Also using this edit in lcd.print (collector); to lcd.print(collector,2); will print to the LCD with 2 decimal places.
You can do the same in Serial.print as well.

Tom... :slight_smile: