matho
The Braumiser
- Joined
- 30/4/08
- Messages
- 1,403
- Reaction score
- 142
I have played around a bit tonight and I'm pleased with how the function is working it has speed up the loop immensely here is the code of the thermostat i have been playing around with, I know it's not the neatest bit of code but it works, now next thing to do is add a PID
Oh and it has also reduced the code size from 7500 bytes to 5200
cheers matho
//asimplethermostat
#include<LiquidCrystal.h>
#include<OneWire.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;
constint Heat = 9;
boolean Start = false;
byte Busy = 0;
boolean Conv_start = false;
float set_temp = 20;
float Temp_c;
int temp;
byte data[12];
byte i;
void setup(void)
{
// 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);
}
void loop(void)
{
Temperature();
lcd.setCursor(0,0);
lcd.print("set temp =");
lcd.setCursor(10,0);
lcd.print(set_temp);
lcd.setCursor(0, 1);
lcd.print("Temp =");
lcd.setCursor(7,1);
lcd.print(Temp_c);
if (!(digitalRead(Button_up))&& !(Start))
{
while (digitalRead(Button_up)==0){
}
set_temp=set_temp+0.25;
}
if (!(digitalRead(Button_dn))&& !(Start))
{
while (digitalRead(Button_dn)==0){
}
set_temp=set_temp-0.25;
}
if (digitalRead(Button_prev)==0)
{
while (digitalRead(Button_prev)==0){
}
Start=true;
}
if (digitalRead(Button_nxt)==0)
{
while (digitalRead(Button_nxt)==0){
}
Start=false;
digitalWrite(Heat,LOW);
}
if (Start)
{
if (set_temp > Temp_c){
digitalWrite (Heat,HIGH);
}
if (set_temp < Temp_c){
digitalWrite (Heat,LOW);
}
}
}
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 < 9; i++) { // we need 9 bytes
data=ds.read();
}
unsigned int raw = (data[1] << 8) + data[0];
Temp_c=raw*0.0625;
Conv_start=false;
return;
}
return;
}
Oh and it has also reduced the code size from 7500 bytes to 5200
cheers matho