Combinding code?

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.

If you are struggliing with the language, it might save time in the long run to buy or borrow a decent text book and read it.

If you want to replace the temperature sensor code with new code, my advice would be to start a new sketch, which only reads the temperature sensor using the new code, and get that to work, and understand it, and then put the new code into your main sketch.

t I try putting the various #include and #define at the top of the code, but then there's lots of compiling errors, etc..

Like what ?

Well, well that's very disappointing, come on chaps! I'm not asking anyone to rewrite the code, it don't need that!! I did hope that someone in the know might just give a hand or something... Is'nt that the idea of forums! that we help and share our ideas, etc...

I'm sure michinyon knows how to do it, but perhaps he wants to keep it to himself.........

But all's well because I did it myself, just looked at it, and logically moved the code sections to where I thought they should go and here's my new code, fully working.

#include <FastIO.h>
#include <I2CIO.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#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); 
/*
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,Heater1=3,Heater2=4,TempPin=12;
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
float TEMP;
void setup()
//----------------Start of Set up---------------------
{
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
// 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
}
//------------End of Setup loop------------------------
//--------------Start of Main loop------------------
void loop()  
{
  GetTime();
  GetTemp();
  TestIt();
  digitalWrite(13,HIGH);    //Flash LED
  delay(100);               //For 100mS
  digitalWrite(13,LOW);     //LED off
}
//------------End of main loop-----------------------------
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);
}
}
//------------------------------------------------------------------------------------------
void GetTemp()
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Send the command to get temperatures
TEMP=(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
}
//-----------------------------------------------------------------------------------------------------------
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("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();
}
}
//*************************************************************************************************************

// 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 would like to say Thanks, but what for??

Regards

Mel.

I would like to say Thanks, but what for??

Well, thank you for making it our fault that you didn't provide enough information to start with.

Cactusface:
I'm sure michinyon knows how to do it, but perhaps he wants to keep it to himself.........

That's unkind.

All he asked was that you provide details of the errors you are experiencing.

You must bear in mind that you know all about your problem and people here know little or nothing about it - unless you tell them.

You will understand when you come to help other people with their problems.

...R

Hi All,
Sorry I was not trying to blame anyone! but perhaps the frustration got to me. I've seen some very stupid questions on this forum, but they usually get some kind of response, which might include, go read a book. I just thought someone might come up with something a bit more constructive or an idea or 2. Perhaps I didn't make myself clear, suppose it always sounds OK to the writer.

In the end perhaps the lack of an answer was the best lesson, as I had no alternative but work it out for myself, and sure I learnt something!!

While my C skills may not be that good, I've been into electronics for many years, before the Arduino I used the Picaxe with a poor simple basic, this didn't lead to good programming practice, also a bit of Z80 asm many years ago.

I do try and help others out where I can......

Here's a picture of the seed propagator project working. Or is that the project working on a breadboard, make the PCB next I think.

Regards

Mel.