//libraries
#include<EEPROM.h>
#include<LiquidCrystal.h>
#include<OneWire.h>
#include<PID_v1.h>
OneWire ds(11);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//pushbuttons
constint Button_up = A3;
constint Button_dn = A2;
constint Button_prev = A1;
constint Button_nxt = A0;
//outputs
constint Heat = 9;
constint Pump = 8;
//variables
int WindowSize = 5000;
unsigned long windowStartTime;
double Setpoint, Input, Output;
boolean autoLoop = false;
boolean manuLoop = false;
boolean waterAdd = false;
boolean Conv_start = false;
boolean mpump = false;
boolean mheat = false;
boolean wtBtn = false;
float mset_temp = 35;
float Temp_c;
int temp;
byte mainMenu = 0;
byte setupMenu = 0;
byte pidMenu = 0;
byte autoMenu = 0;
byte stepTime;
byte pumpTime;
byte data[2];
byte i;
byte Busy = 0;
//Specifythelinksandinitialtuningparameters
PID myPID(&Input, &Output, &Setpoint,200,5,3, DIRECT);
void setup()
{
// Start up the library
lcd.begin(16,2);
pinMode (Button_up,INPUT);
pinMode (Button_dn,INPUT);
pinMode (Button_prev,INPUT);
pinMode (Button_nxt,INPUT);
pinMode (Heat,OUTPUT);
pinMode (Pump,OUTPUT);
windowStartTime=millis();
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
myPID.SetMode(AUTOMATIC);
}
void loop()
{
switch (mainMenu){
case (0):
Temperature();
lcd.setCursor(0,0);
lcd.print("The Brauduino");
lcd.setCursor(0,1);
lcd.print("Temp=");
lcd.setCursor(6,1);
lcd.print(Temp_c);
//read the buttons for menu selection
if (digitalRead(Button_dn)==0){
delay(1000);
if (digitalRead(Button_dn)==0){
lcd.setCursor(0,0);
lcd.print(" Manual ");
lcd.setCursor(0,1);
lcd.print(" ");
mainMenu=1;
while(digitalRead(Button_dn)==0){
}
}
}
if (digitalRead(Button_prev)==0){
delay(1000);
if (digitalRead(Button_prev)==0){
lcd.setCursor(0,0);
lcd.print(" Auto ");
lcd.setCursor(0,1);
lcd.print(" ");
mainMenu=2;
while(digitalRead(Button_prev)==0){
}
}
}
if (digitalRead(Button_nxt)==0){
delay(1000);
if (digitalRead(Button_nxt)==0){
lcd.setCursor(0,0);
lcd.print(" Setup ");
lcd.setCursor(0,1);
lcd.print(" ");
mainMenu=3;
while(digitalRead(Button_nxt)==0){
}
}
}
break;
case(1): //manual mode
delay(1000);
lcd.setCursor(0,0);
lcd.print(" Water added? ");
lcd.setCursor(0,1);
lcd.print(" Yes Quit");
wtBtn=true;
while (wtBtn){
if (digitalRead(Button_prev)==0){
manuLoop=true;
wtBtn=false;
while(digitalRead(Button_prev)==0){
}
}
if (digitalRead(Button_nxt)==0){
mainMenu=0;
wtBtn=false;
while(digitalRead(Button_nxt)==0){
}
}
}
while (manuLoop){
Temperature();
Setpoint=mset_temp*16;
Input=Temp_c*16;
lcd.setCursor(0,0);
lcd.print(" Manual Mode ");
lcd.setCursor(0,1);
lcd.print("ST=");
lcd.print(mset_temp);
lcd.setCursor(8,1);
lcd.print("AT=");
lcd.print(Temp_c);
if (digitalRead(Button_up)==0){
delay(200);
if (digitalRead(Button_dn)==0){
digitalWrite(Heat,LOW);
digitalWrite(Pump,LOW);
mainMenu=0;
manuLoop=false;
lcd.clear();
}
else{
mset_temp=mset_temp+0.25;
}
while (digitalRead(Button_up)==0){
}
}
if (digitalRead(Button_dn)==0)
{
delay(200);
if (digitalRead(Button_up)==0){
digitalWrite(Heat,LOW);
digitalWrite(Pump,LOW);
mainMenu=0;
manuLoop=false;
lcd.clear();
}
else{
mset_temp=mset_temp-0.25;
}
while (digitalRead(Button_dn)==0){
}
}
//turns heat on or off
if (digitalRead(Button_prev)==0){
while(digitalRead(Button_prev)==0){
}
if (mheat==false){
mheat=true;
}
else{
mheat=false;
digitalWrite(Heat,LOW);
}
}
//turns the pump on or off
if (digitalRead(Button_nxt)==0){
if (mpump == false){
mpump=true;
digitalWrite(Pump,HIGH);
}
else{
mpump=false;
digitalWrite(Pump,LOW);
}
while(digitalRead(Button_nxt)==0){
}
}
if (mheat){
PID_HEAT();
}
}
break;
case(2):
if (digitalRead(Button_up)==0){
delay(100);
if (digitalRead(Button_dn)==0){
mainMenu=0;
while ( digitalRead(Button_dn)==0){
}
}
}
break;
case(3):
if (digitalRead(Button_up)==0){
delay(100);
if (digitalRead(Button_dn)==0){
mainMenu=0;
while ( digitalRead(Button_dn)==0){
}
}
}
break;
}
}
void Temperature(void)
{
ds.reset();
ds.skip();
// start conversion and return
if (!(Conv_start)){
ds.write(0x44,0);
Conv_start=true;
return;
}
// check for conversion if it isn't complete return if it is then convert to decimal
if (Conv_start){
Busy=ds.read_bit();
if (Busy == 0){
return;
}
ds.reset();
ds.skip();
ds.write(0xBE);
for ( i = 0; i < 2; i++) { // we need 2 bytes
data=ds.read();
}
unsigned int raw = (data[1] << 8) + data[0];
Temp_c=(raw&0xFFFC)*0.0625;
Conv_start=false;
return;
}
return;
}
void PID_HEAT (void){
myPID.Compute();
/************************************************
*turntheoutputpinon/offbasedonpidoutput
************************************************/
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{//time to shift the Relay Window
windowStartTime+=WindowSize;
}
if(Output > now - windowStartTime) digitalWrite(Heat,HIGH);
else digitalWrite(Heat,LOW);
return;
}