Arduino sketch 29/8/2012

Here is the arduino sketch which we use to control the prototype of the experimental incubator.

 

#include <LiquidCrystal.h>
String modeStr[]={“Set Min “, “Set Max “, “Current”};
int mode=2;
int maxTemp=25;
int minTemp=23;

const int downButtonPin=2;
const int upButtonPin=13;
const int onBoardTempPin=A1;
const int lineTempPin=A0;
const int heaterPin=9;
const int coolerPin=10;

boolean upButtonState=false;
boolean downButtonState=false;
boolean heaterState=false;
boolean coolerState=false;
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);

int tempPin=0;
int tempPin2=1;
int onBoardTemp=0;
int lineTemp=0;

void setup() {
lcd.begin(8, 2);
//analogReference(INTERNAL);
attachInterrupt(1, set, FALLING);
pinMode(upButtonPin,INPUT);
pinMode(upButtonPin,INPUT);
pinMode(heaterPin, OUTPUT);
pinMode(coolerPin, OUTPUT);
}

void printStrLCD(int row, int col, String str)
{
lcd.setCursor(row, col);
lcd.print(str);
}
void loop() {
if(mode==2) //current Temp, minTemp, maxTemp
{
int midTemp= ((maxTemp*10) + (minTemp*10))/2;
onBoardTemp=0;
lineTemp=0;
for(int i=0; i<20; i++)
{
onBoardTemp+=map(analogRead(onBoardTempPin), 0, 930, 0, 1024);
lineTemp+=map(analogRead(lineTempPin), 0, 930, 0, 1024);
}
onBoardTemp/=20;
lineTemp/=20;
String topStr= String()+ (onBoardTemp/10) + “.”+ (onBoardTemp%10) + ” ” + maxTemp;
String bottomStr=String()+ (lineTemp/10) + “.”+ (lineTemp%10) + ” ” + minTemp;
if(!heaterState && lineTemp<minTemp)
{
heaterState=true;
coolerState=false;
digitalWrite(heaterPin, HIGH);
digitalWrite(coolerPin, LOW);
}
if(heaterState && lineTemp>midTemp)
{
heaterState=false;
digitalWrite(heaterPin, LOW);
}
if(!coolerState && lineTemp>maxTemp)
{
heaterState=false;
coolerState=true;
digitalWrite(heaterPin, LOW);
digitalWrite(coolerPin, HIGH);
}
if(coolerState && lineTemp<midTemp)
{
coolerState=false;
digitalWrite(coolerPin, LOW);
}
printStrLCD(0, 0, topStr);
printStrLCD(0, 1, bottomStr);
for(int i=0;i<1000;i++)
{
if(mode!=2)
break;
delay(1);
}
}

if(mode==1)
{
upButtonState=digitalRead(upButtonPin);
downButtonState=digitalRead(downButtonPin);
if(upButtonState==HIGH)
{
maxTemp+=1;
delay(300);
}
if(downButtonState==HIGH)
{
if(minTemp+1<maxTemp)
maxTemp-=1;
delay(300);
}
printStrLCD(0, 0, modeStr[1]);
String str=String()+maxTemp+”      “;
printStrLCD(0, 1, str);
}
if(mode==0)
{
upButtonState=digitalRead(upButtonPin);
downButtonState=digitalRead(downButtonPin);
if(upButtonState==HIGH)
{
if(minTemp+1<maxTemp)
minTemp+=1;
delay(300);
}
if(downButtonState==HIGH)
{
minTemp-=1;
delay(300);
}
printStrLCD(0, 0, modeStr[0]);
String str=String()+minTemp+”      “;
printStrLCD(0, 1, str);
}
}
void set()
{
switch(mode){
case 0: mode=1;
break;
case 1: mode=2;
break;
case 2: mode=0;
break;
}
}

 

Advertisements

~ by algoldor on 29/08/2012.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: