Hi All,
Well I've been playing with the Arduino for sometime now, BUT still struggling with C and coding. I have built a controller for my seed propagator, which is basicly a DS1307 and DS18B20 with an output to a relay.
Some of this code is mine but other parts from forums and the net. It all works fine, but the code to get the temp is a bit long winded and I can't quite get my head around it. But I found something much simpler using the DallasTemperture library! What I would like to do is get rid of the complicated? stuff and add this simpler code, but I try putting the various #include and #define at the top of the code, but then there's lots of compiling errors, etc..
Here's my? code that works.
#include <FastIO.h>
#include <I2CIO.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
/*
Connections:
Heater1 pin 3
Heater2 pin 4
SDA to Arduino Analog pin 4
SCL to Arduino Analog pin 5
LightPin Analog pin A7
TempPin pin 12
*/
#define DS1307_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE); // Set the LCD I2C address
//*****constants*****
int Light,LightPin=A7,TargetTemp,TEMP,PTemp,Heater1=3,Heater2=4,TempPin=12;
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
void setup()
{
TargetTemp=23; //Set Normal/Target temp to 23c
pinMode(Heater1,OUTPUT);
pinMode(Heater2,OUTPUT);
pinMode(13,OUTPUT);
Wire.begin();
lcd.begin(20,4); // tells Arduino the LCD dimensions
}
//------------End of Setup loop------------------------
//--------------Start of Main loop------------------
void loop()
{
GetTime();
GetTemp();
TestIt();
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
}
//---------------------------------------------------------------------------------
void GetTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
//if (second <=5)
{
//lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" ");
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
lcd.print("/");
lcd.print("20");
lcd.print(year, DEC);
//delay(1000);
}
}
//------------------------------------------------------------------------------------------
//http://sheepdogguides.com/arduino/ar3ne1tt.htm
// void OneWireReset(int Pin);//See Note 2
// void OneWireOutByte(int Pin, byte d);
// byte OneWireInByte(int Pin);
void GetTemp()
{
OneWireReset(TempPin);
OneWireOutByte(TempPin, 0xcc);
OneWireOutByte(TempPin, 0x44); // perform temperature conversion, strong pullup for one sec
OneWireReset(TempPin);
OneWireOutByte(TempPin, 0xcc);
OneWireOutByte(TempPin, 0xbe);
LowByte = OneWireInByte(TempPin);
HighByte = OneWireInByte(TempPin);
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
TEMP = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
PTemp = Tc_100 % 100;
}
void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT); // bring low for 500 us
delayMicroseconds(500);
pinMode(Pin, INPUT);
delayMicroseconds(500);
}
void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
byte n;
for(n=8; n!=0; n--)
{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(60);
}
else
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(60);
pinMode(Pin, INPUT);
}
d=d>>1; // now the next bit is in the least sig bit position.
}
}
byte OneWireInByte(int Pin) // read byte, least sig byte first
{
byte d, n, b;
for (n=0; n<8; n++)
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(5);
b = digitalRead(Pin);
delayMicroseconds(50);
d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return(d);
}
//-----------------------------------------------------------------------------------------------------------
void TestIt()
{
Light=analogRead(LightPin);
Light=Light/4;
if(Light>200)
{TargetTemp=25;}
if(Light>160 && Light<200)
{TargetTemp=18;}
if(Light<160)
{TargetTemp=14;}
// Set Temp******
lcd.setCursor(1,1);
lcd.print("Target Temp:");
lcd.print(TargetTemp);
lcd.print(".00c ");
lcd.setCursor(1,2);
lcd.print("Current Temp:");
lcd.print(TEMP);
lcd.print(".");
if(PTemp <10)
{lcd.print("0");}
lcd.print(PTemp);
lcd.print("c ");
if (TEMP < TargetTemp)
{
digitalWrite(Heater1,LOW); //Heater ON
digitalWrite(Heater2,LOW);
lcd.setCursor(1,3);
lcd.print("Heater ON ");
lcd.print(Light);
lcd.print(" ");
}
if (TEMP > TargetTemp)
{
digitalWrite(Heater1,HIGH);
digitalWrite(Heater2,HIGH); //Heater OFF
lcd.setCursor(1,3);
lcd.print("Heater OFF ");
lcd.print(Light);
lcd.print(" ");
//lcd.clear();
}
//delay(5000);
}
//*************************************************************************************************************
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// Gets the date and time from the ds1307
void getDateDs1307
(
byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read() &0x07);
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
I'd like to get rid of the function GetTemp and replace it with this.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 12 on the Arduino
#define ONE_WIRE_BUS 12
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
//Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin(); // IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print(" ");
}
Also how do I change- Serial.print(sensors.getTempCByIndex(0)) to something like TEMP=sensors.getTemp(0) to output to an LCD.
Hope someone can help or point me in the right direction..
Thanks and regards
Mel.