/////////////////////////////////START/////////////////////////////////////////////////////
int PinTemp = 0; // LM34 temperature sensor
int PinHeater = 13; //SSR control for heating element
int PinPump = 8; //pump control
int PinValve = 10; //valve control
float timer = 0;
String decimal = ""; //string for placing decimals
int state; //used for setting heater, pump, and valve states from app
char ascii; //used to convert ascii byte to decimal
char ack; //used for communication acknowledgment with android app
int target; //varaible for target temp
int gonogo; //general variable for no and no go situations
float signal;
float millivolts;
int temp;
int heater;
int pump;
int valve;
/////////////////////////////////SETUP/////////////////////////////////////////////////////
void setup() // put your setup code here, to run once:
{
//Set pin modes
pinMode (PinTemp, INPUT); //set temperature sensor pin as input
pinMode (PinHeater, OUTPUT); //set heater pin as output
pinMode (PinPump, OUTPUT); //sets pump pin as output
pinMode (PinValve, OUTPUT); //sets valve pin as output
digitalWrite (PinHeater, LOW); //heat init to off
digitalWrite (PinPump, HIGH); //pump init to off
digitalWrite (PinValve, HIGH); //vavle init to pos A
gonogo = 0; //disables heater at begining
Serial1.begin(9600);// opens serial port, sets data rate to 9600 bps
}
////////////////////////////////////LOOP///////////////////////////////////////////////////////
void loop() // put your main code here, to run repeatedly:
{
Handshake(); //waits for clocktimer in app to send a handshake byte
Variables(); //collects variables that have been written to serial from app
Status();//run status subrotine to report variables back to app
if (gonogo == 1) //When gonogo is set to 1, control heating element based on target temp and actual temp
{
if (temp < target) //if the temperature is less than target, then
{
digitalWrite (PinHeater, HIGH); //turn the heating element on, otherwise
heater = 1;
}
else
{
digitalWrite (PinHeater, LOW); //turn it off
heater = 0;
}
}
else {
digitalWrite (PinHeater, LOW); //if nogono is 0 then disable heater
heater = 0;
}
}
///////////////////////////HANDSHAKE SUBROUTINE//////////////////////////////////////////////////////////////////////
void Handshake()
{
ack = Serial1.read(); //reads any available byte from serial
while (ack != 'A') //loops until handshake byte is recieved from app
{ack = Serial1.read();}
Serial1.print('B'); //returns handshake byte to app
ack = 'B'; //sets ack to force program to wait for next handshake byte from app
}
///////////////////////////////VARIABLES SUBROUTINE//////////////////////////////////////////////////////////////////////
void Variables() //sets & toggles variables
{
while (Serial1.available() == 0) //loops until variables from app are available
{delay(500);}
state = Serial1.read(); //stores heater, pump, and valve states from app
if (state == 1) //checks if heater is on or off
{
gonogo = 1; //sets gonogo to 1 so heater will turn on using temp control algorithym
}
else
{
gonogo = 0; //sets gonogo to 0 so heater won't turn on, regardless of temp control algorithym
}
state = Serial1.read(); //stores heater, pump, and valve states from app
if (state == 1) //checks if pump is on or off
{
digitalWrite(PinPump,LOW); //turn pump on
pump = 1;
}
else {digitalWrite(PinPump, HIGH);
pump = 0;} //turn pump off
state = Serial1.read(); //stores heater, pump, and valve states from app
if (state == 1) //checks if valve is pos A or B
{
digitalWrite(PinValve, LOW); //turn valve pos A
valve = 1;
}
else {digitalWrite(PinValve, HIGH);
valve = 0;} //turn valve pos B
while (Serial1.available() > 0) //collects all bytes for target temp and inserts into sring
{
ascii = Serial1.read();
ascii=ascii-0;
decimal += ascii;
}
target = decimal.toInt();// sets target to value that user entered in serial
decimal = ""; //clears string for future use
}
/////////////////////////////////////STATUS SUBROUTINE//////////////////////////////////////////
void Status() //reports the status of each variable back to app
{
Serial1.print(gonogo); //gonogo status
Serial1.print('C'); //delimeter
Serial1.print(heater);
Serial1.print('C'); //delimeter
Serial1.print(target); //print target temp
Serial1.print('C');
//Convert to deg F and display in serial
signal = analogRead(PinTemp); //read signal on AO1 and store as "signal"
millivolts = (signal/1024.0)*5000; //signal is divided by 1024 (resolution) and multiplied by 5000mV to get voltage
temp = millivolts/10; //converts voltage to temperature since there is 10mV per deg F
Serial1.print(temp); //print actual temp
Serial1.print('C');
Serial1.print(pump);
Serial1.print('C'); //delimeter
Serial1.print(valve);
Serial1.print('C'); //delimeter
}