Greetings everyone,
Brand-noob here trying to get his first "real" Arduino project off the ground. I'm working on building an HVAC thermostat that can function on its own as well as accept (serial) input from an external PC/server.
The problem I'm running into is that I can manually set a global variable and make the thing decide whether to turn on the AC or heat based on the setpoint vs. inside temperature, but I haven't figured out how to change that setpoint variable over serial because the sketch is constantly looping, and unless the serial data is received at exactly the precise moment in the program, the data is ignored. The concept of a serial interrupt seems like the answer, but the few days of searching has led me to things that appear to be over my head (at least at this point).
Here's my code so far. I still have quite a bit to do (add hysteresis where appropriate, add hardware interrupts to accept manual input, etc.) but I have it in my head that those things should be relatively easy compared to what I'm up against at the moment.
Thanks in advance for any help/suggestions/reality-checks!
/*
code to read serial port from command-line:
while head -c 1 /dev/ttyUSB0; do : ;done
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int tempInside; // create a variable for the inside temp
int tempOutside; // create a variable for the outside temp
int insidePin = 5; // connect inside temp sensor to pin 5
int outsidePin = 4; // connect outside temp sensor to pin 4
int fan = 8; // fan control on pin 8
int cool = 9; // AC control on pin 9
int heat = 10; // heat control on pin 10
int setpoint; // create a variable for the desired temp. we'll set this later
int hum = 23; // if we had a humidity sensor, it would likely read much higher than this around here!
int hvacPin = 3; // this pin would read the state of the fan. Probably not going to be used for this in the end design
int hvacState = 0; // create a variable for whether or not the hvacPin is high or low
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(fan, OUTPUT);
pinMode(cool, OUTPUT);
pinMode(heat, OUTPUT);
}
void loop()
{
tempInside = analogRead(insidePin);
tempOutside = analogRead(outsidePin);
lcd.print("Inside | Outside");
lcd.setCursor(1, 4);
lcd.print(tempInside);
lcd.print(char(223));
lcd.print(" ");
lcd.print(tempOutside);
lcd.print(char(223));
Serial.print(tempInside);
Serial.print(", ");
Serial.println(tempOutside);
delay(3000);
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
delay(50);
}
lcd.clear();
lcd.print("Humidity | HVAC");
lcd.setCursor(1, 4);
lcd.print(hum);
lcd.print("%");
lcd.print(" ");
if (tempInside > setpoint)
{
lcd.print("Cool");
digitalWrite(heat, LOW);
digitalWrite(fan, HIGH);
digitalWrite(cool, HIGH);
} else {
lcd.print("Heat");
digitalWrite(cool, LOW);
digitalWrite(fan, HIGH);
digitalWrite(heat, HIGH);
}
delay(3000);
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
delay(50);
}
lcd.clear();
}