Pages: [1]   Go Down
Author Topic: A little trouble with my thermostat sketch.  (Read 216 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Everything works wonderfully except for one portion of the sketch.
In the sketch, I am telling the arduino to set pin 7 HIGH in the case that certain conditions are met and then once those conditions are satisfied to turn it off again. this works fine for when my switch pin is reading 1 (HIGH) but not when its reading 0 (LOW). Please help.. this is driving me MAD.
Code:

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

int sensorPin = A0;    // select the input pin for the 10K potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int setTemp = 0; // variable to store temp desired
int SSRCPin = 5; //Turn on A/C unit
int SSRHPin = 6; //Turn on heat (electric or gas)
int hcLED = 4; //indicator for Cooling mode
int SwitchPin = 1; // To switch between Cooling and Heating
int SSRFan = 7; // To turn on and off the air handler fan
char* heat;
float currentTemp = 0;
LiquidCrystal lcd(9, 10, 11, 12, 13, 8);



// Data wire is plugged into pin 12 on the Arduino
#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);

DeviceAddress insideThermometer = { 0x28, 0xF0, 0xEA, 0x73, 0x03, 0x00, 0x00, 0xCF };
// DeviceAddress outsideThermometer = { 0x28, 0x20, 0x04, 0xA8, 0x02, 0x00, 0x00, 0x4D };

void setup(void)
{
// Start up the library
sensors.begin();
// set the resolution to 9 bit (good enough?)
sensors.setResolution(insideThermometer, 9);
// sensors.setResolution(outsideThermometer, 9);

lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
pinMode(SSRFan, OUTPUT); //set airhandler fan pin as output
digitalWrite(SSRFan, LOW);
pinMode(SSRHPin, OUTPUT);
digitalWrite(SSRHPin, LOW);
pinMode(SSRCPin, OUTPUT);
digitalWrite(SSRCPin, LOW);
pinMode(hcLED, OUTPUT);
digitalWrite(hcLED, LOW);
pinMode(SwitchPin, INPUT);
}

void printTemperature(DeviceAddress deviceAddress)
{
 
sensors.requestTemperatures();  // was in loop

float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
// lcd.print(tempC);
// lcd.print("/");
currentTemp = (DallasTemperature::toFahrenheit(tempC));
lcd.print(currentTemp);
}

}


void loop(void)
{
delay(1000);

  sensorValue = analogRead(sensorPin); 
 
  setTemp = sensorValue / 10.24; //Gives us a set temp range between 0 and 99 degrees

lcd.clear(); // start with a blank screen
lcd.setCursor(0,0);
lcd.print("Current:");
lcd.setCursor(0,1);
lcd.print("Set:");
//lcd.setCursor(0,3);
//lcd.print("Heat: ");



lcd.setCursor(8,0);
printTemperature(insideThermometer);
lcd.setCursor(6,1);
lcd.print(setTemp);
//lcd.setCursor(7,3);
//lcd.print(heat);


int val = digitalRead(SwitchPin) ;
  if (val == 1)
  {
  digitalWrite(hcLED, LOW);
  }
  if (val == 1 && (currentTemp > setTemp))
 {
  digitalWrite(SSRFan, HIGH);
  digitalWrite(SSRHPin, LOW);
  digitalWrite(SSRCPin, HIGH);
  }
 else if (currentTemp < setTemp)
 {
   digitalWrite(SSRCPin, LOW);
   digitalWrite(SSRFan, LOW);
 }

if (val == 0)
  {
  digitalWrite(hcLED, HIGH);
  }
  if (val == 0 && (currentTemp < setTemp))
  {
  digitalWrite(SSRFan, HIGH);
  digitalWrite(SSRCPin, LOW); 
  digitalWrite(SSRHPin, HIGH);
  }
  else if (currentTemp > setTemp)
  {
    digitalWrite(SSRHPin, LOW);
    digitalWrite(SSRFan, LOW);
}

 }

Logged

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Forgot to say what it is doing right...LOL  smiley-red
in "cooling mode" its not turning on the SSRFan... sorry
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this the part that isn't working?  It is outside of the val == 1 check and will execute regardless of the value of the switch pin. Maybe you need to check for the value of val here too?

Code:
else if (currentTemp < setTemp)
 {
   digitalWrite(SSRCPin, LOW);
   digitalWrite(SSRFan, LOW);     <---------- 
}
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 78
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the delay.. Internet was out.
Thank you. That was definitely the problem.

Here is the finished and working code for anyone that may want it.

Code:

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

int sensorPin = A0;    // select the input pin for the 10K potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int setTemp = 0; // variable to store temp desired
int SSRCPin = 5; //Turn on A/C unit
int SSRHPin = 6; //Turn on heat (electric or gas)
int hcLED = 4; //indicator for Cooling mode
int SwitchPin = 1; // To switch between Cooling and Heating
int SSRFan = 7; // To turn on and off the air handler fan
char* heat;
float currentTemp = 0;
LiquidCrystal lcd(9, 10, 11, 12, 13, 8);



// Data wire is plugged into pin 12 on the Arduino
#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);

DeviceAddress insideThermometer = { 0x28, 0xF0, 0xEA, 0x73, 0x03, 0x00, 0x00, 0xCF };
// DeviceAddress outsideThermometer = { 0x28, 0x20, 0x04, 0xA8, 0x02, 0x00, 0x00, 0x4D };

void setup(void)
{
// Start up the library
sensors.begin();
// set the resolution to 9 bit (good enough?)
sensors.setResolution(insideThermometer, 9);
// sensors.setResolution(outsideThermometer, 9);

lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
pinMode(SSRFan, OUTPUT); //set airhandler fan pin as output
digitalWrite(SSRFan, LOW);
pinMode(SSRHPin, OUTPUT);
digitalWrite(SSRHPin, LOW);
pinMode(SSRCPin, OUTPUT);
digitalWrite(SSRCPin, LOW);
pinMode(hcLED, OUTPUT);
digitalWrite(hcLED, LOW);
pinMode(SwitchPin, INPUT);
}

void printTemperature(DeviceAddress deviceAddress)
{
 
sensors.requestTemperatures();  // was in loop

float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
lcd.print("Error");
} else {
// lcd.print(tempC);
// lcd.print("/");
currentTemp = (DallasTemperature::toFahrenheit(tempC));
lcd.print(currentTemp);
}

}


void loop(void)
{
delay(1000);

  sensorValue = analogRead(sensorPin); 
 
  setTemp = sensorValue / 10.24; //Gives us a set temp range between 0 and 99 degrees

lcd.clear(); // start with a blank screen
lcd.setCursor(0,0);
lcd.print("Current:");
lcd.setCursor(0,1);
lcd.print("Set:");
//lcd.setCursor(0,3);
//lcd.print("Heat: ");



lcd.setCursor(8,0);
printTemperature(insideThermometer);
lcd.setCursor(6,1);
lcd.print(setTemp);
//lcd.setCursor(7,3);
//lcd.print(heat);


int val = digitalRead(SwitchPin) ;
  if (val == 1)
  {
  digitalWrite(hcLED, LOW);
  }
  if (val == 1 && (currentTemp > setTemp))
 {
  digitalWrite(SSRFan, HIGH);
  digitalWrite(SSRHPin, LOW);
  digitalWrite(SSRCPin, HIGH);
  }
 else if (val == 1 && (currentTemp < setTemp))
 {
   digitalWrite(SSRCPin, LOW);
   digitalWrite(SSRFan, LOW);
 }

if (val == 0)
  {
  digitalWrite(hcLED, HIGH);
  }
  if (val == 0 && (currentTemp < setTemp))
  {
  digitalWrite(SSRFan, HIGH);
  digitalWrite(SSRCPin, LOW); 
  digitalWrite(SSRHPin, HIGH);
  }
  else if (val == 0 && (currentTemp > setTemp))
  {
    digitalWrite(SSRHPin, LOW);
    digitalWrite(SSRFan, LOW);
}

 }

Logged

Pages: [1]   Go Up
Jump to: