Hi all. I am in the process of making my first 'proper' arduino project. I'm putting together a thermstat to switch on my boiler at a set temperature. I am not experienced at all with programming so the source code has taken a while to get working as I wanted it too.
Basically I am reading the value from a pot, mapping it to a temperature range, and using this value to switch a digital output if the measured temperature is less than the desired. It does this with else, if statements.
I have also added some maths to take the average of a few temp measurements around the house, so I would like to use a switch and lets say, if its value is closed / high, the measured temperature will switch to the average reading, and if its open / low the measured temperature will switch to the single local sensor reading.
I have been trying to get this to work by reading a digital input, deciding if the value is low or high and then using that high or low variable in the 'else if' statement that switches the output, but my efforts have failed because the output pin stays active all the time
I hope its only something simple, but if someone could point it out it would save me a lot of headaches
the first lot of code below is the edited (non working) code as explained above, and the second lot is before i started to try and read the switch and stopped it working in case I have really messed it up.
/*
The circuit:
* LDR connected to analog pin 0.
* LED / transistor connected from digital pin 3 to ground
* LCD RS pin to digital pin 7
* LCD EN pin to digital pin 8
* LCD D4 pin to digital pin 9
* LCD D5 pin to digital pin 10
* LCD D6 pin to digital pin 11
* LCD D7 pin to digital pin 12
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* LCD LED+ (15) pin transistor
* LCD LED- (16) to ground
* Thermisor divider 1 output to analog pin 1
* Thermisor divider 2 output to analog pin 2
*/
// These constants won't change. They're used to give names
// to the pins used:
const int select = A0; // Analog input pin that the temp select pot is attached to
const int analogInPin = A1; // Analog input pin that the lDR is attached to
const int analogOutPin = 3; // Analog (PWM) output pin that the BACKLIGHT is attached to
const int coldPin = 2; // Digital output pin that the COLD RELAY is attached to
const int frostPin = 4; // Digital output pin that the frost warning is attached to
//const int hotPin = 5; // Digital output pin that the HOT LED is attached to
const int out = A2; // Analog input that the 'Out' thermistor is connected too
const int hall = A3; // Analog input that the 'Hall' thermistor is connected too
const int bed = A4; // Analog input that the 'Bed' thermistor is connected too
const int bath = A5; // Analog input that the 'Bath' thermistor is connected too
const int lounge = A6; // Analog input that the 'Lounge' thermistor is connected too
const int kitchen = A7; // Analog input that the 'Kitchen' thermistor is connected too
const int modeButton = 6; //Digital input that the mode button is connected too
//Parameters
const int coldTemp = 10; // lower do something threshold in degrees c
const int hotTemp = 20; // upper do something threshold in degrees c
const int setPointLow = 5; // lowest temp available
const int setPointHigh = 25; // highest temp available
const int frostWarning = 10; // outside frost warning threshold
float hysteresis = 0.4; // error margin
float averageTemp = 0; //place to store average reading
float desired = 0; //place to store selected mode
float buttonState = 0; //place to store current selected mode
const int minLight = 80; // at or below this light level, use minimum backlight intensity
const int maxLight = 920; // at or above this light level, use maximum backlight intensity
const int minBacklight = 5; // lowest backlight intensity to use
const int maxBacklight = 255; // highest backlight intensity to use
// include the library code:
#include <LiquidCrystal.h>
#include <math.h>
#define NUMSAMPLES 64
double convert(double RawADC)
{
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int sensorValue1 = 0; // value read from the pot
int sensorValue = 0; // value read from the LDR
int outputValue = 0; // value output to the PWM (analog out)
int setPoint = 0; // value set point
void setup()
{
lcd.begin(20, 4);
delay(100);
pinMode(3, OUTPUT); //LCD BACKLIGHT
pinMode(coldPin, OUTPUT); //COLD WARNING
pinMode(frostPin, OUTPUT); //OUTSIDE FROST WARNING
pinMode(modeButton, INPUT); //MODE SELECT BUTTON
}
void loop()
{
// LDR PART
int sum = 0;
for (int i=0; i<16; i++) // do some averaging too
{
sum += analogRead(analogInPin);
}
sensorValue = sum/16;
// map it to the range[0..255] of the analog out
outputValue = sensorValue/4; // easier than map function
outputValue = map(constrain(sensorValue, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight );
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// temp select part
sensorValue1 = analogRead(select);
setPoint = map (sensorValue1, 0, 1023, setPointLow, setPointHigh);
// TEMPERATURE PART
uint8_t i;
float average = 0; //input1
float average1 = 0; //input2
float average2 = 0; //input3
float average3 = 0; //input4
float average4 = 0; //input5
float average5 = 0; //input6
for (i=0; i< NUMSAMPLES; i++)
{
average += analogRead(out);
average1 += analogRead(hall);
average2 += analogRead(bed);
average3 += analogRead(bath);
average4 += analogRead(lounge);
average5 += analogRead(kitchen);
delay(10);
}
average /= NUMSAMPLES;
average1 /= NUMSAMPLES;
average2 /= NUMSAMPLES;
average3 /= NUMSAMPLES;
average4 /= NUMSAMPLES;
average5 /= NUMSAMPLES;
double outTemp = convert(average);
double hallTemp = convert(average1);
double bedTemp = convert(average2);
double bathTemp = convert(average3);
double loungeTemp = convert(average4);
double kitchenTemp = convert(average5);
//PRINT HALL TEMP AND DO THERMOSTAT DUTIES
lcd.clear();
lcd.setCursor(0,0);
lcd.print ("Hal: ");
lcd.print(hallTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
buttonState = digitalRead(modeButton);
if(buttonState == HIGH)
desired = 0;
else if (buttonState == LOW)
desired = 1;
if(desired = 0 && hallTemp <setPoint - hysteresis)
digitalWrite(coldPin, HIGH); // set the LED on
else if (desired = 0 && hallTemp >setPoint + hysteresis)
digitalWrite(coldPin, LOW); // set the LED off
else if (desired = 1 && averageTemp >setPoint + hysteresis)
digitalWrite(coldPin, HIGH); // set the LED off
else if (desired = 1 && averageTemp >setPoint + hysteresis)
digitalWrite(coldPin, LOW); // set the LED off
//print set temp
lcd.setCursor(11,4);
lcd.print ("Set: ");
if(setPoint <setPointLow +1)
lcd.print((char)42);
else if (setPoint >setPointLow)
lcd.print(setPoint);
//PRINT LOUNGE TEMP
lcd.setCursor(11,0);
lcd.print ("Lng: ");
lcd.print(loungeTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
//PRINT KITCHEN TEMP
lcd.setCursor(0,1);
lcd.print ("Kit: ");
lcd.print(kitchenTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
//PRINT BED TEMP
lcd.setCursor(11,1);
lcd.print ("Bed: ");
lcd.print(bedTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
//PRINT BATH TEMP
lcd.setCursor(0,2);
lcd.print ("Bth: ");
lcd.print(bathTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
//PRINT OUT TEMP
lcd.setCursor(0,4);
lcd.print ("Out: ");
lcd.print(outTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
if(outTemp <frostWarning - hysteresis)
digitalWrite(frostPin, HIGH); // set the LED on
else if (outTemp >frostWarning + hysteresis)
digitalWrite(frostPin, LOW); // set the LED off
//PRINT average temp
averageTemp = hallTemp + bedTemp + bathTemp + loungeTemp + kitchenTemp;
averageTemp = averageTemp/5;
lcd.setCursor(11,2);
lcd.print ("Ave: ");
lcd.print(averageTemp, 1);
//lcd.print((char)223);
//lcd.print ("c");
delay(1000);
}