If then for digital temperature sensor

Hi all,

I am trying to make an LED turn on when the temperature reaches a certain temperature value but I am unsure where to add my modification lines of code inside the open source code for the exact same sensor I am using. I got the code for my digital sensor on THIS forum thread. HIs code is un commented for the most part and I am having difficulty following to some extent.

I converted his code to produce in celsius. The code and my addition to make an LED light at a temperature less than 26 degrees is shown below.

Can someone help me to get an LED to come on at less than 26 degrees please.

#include <OneWire.h>// http://playground.arduino.cc/Learning/OneWire (One Wire Libary)
#include <Wire.h> 

OneWire  ds(12);  // on pin 12------------T1
int led = 7; //                     <<<<<<<<<<<<<<<<<< LED PIN
void setup(void)
{
  Serial.begin(9600);
  ds.reset();
pinMode(led, OUTPUT); //            <<<<<<<<<<<<<<<<<<< LED PinMode
}

unsigned int readBytes(int count)
{
    unsigned int val = 0;
    for (int i = 0; i < count; i++)
    {
        val |= (unsigned int)(ds.read() << i * 8);
    }
    return val;
}


void loop(void) 
{
 float temp1= Print_T1();
 Serial.print(temp1);
 Serial.print("\t");
 if (temp1 <29 )//                 <<<<<<<<<<<<<<<<<<<<<<<<<<<< LED Temp condition
 {    
digitalWrite(led, LOW);   // turn the LED on (HIGH is the voltage level)
  ; 
 }  
}

float Print_T1()
{
byte temp_read = 0;
  unsigned int count_remain = 0;
  unsigned int count_per_c = 0;
  byte configuration_register = 0;

  ds.reset();
  ds.write(0xEE); //Start Converting the temperatures  
  
   do {
        delay(1);
        configuration_register = 0;
        ds.reset();
        ds.write(0xAC);

        // Read the configuration Register from the DS1821
        configuration_register = readBytes(1);
    } while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp

  
    // Get Temp
    ds.reset();
    ds.write(0xAA);
    temp_read = readBytes(1); ;

    // Get Count Remaining
    ds.reset();
    ds.write(0xA0);
    count_remain = readBytes(2);

    // Load The Counter to populate the slope accumulator
    ds.reset();
    ds.write(0x41);

    // Read Count Per Deg
    ds.reset();
    ds.write(0xA0);
    count_per_c = readBytes(2);

    // If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
    if (temp_read >= 200) temp_read -= 256;

    float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);

     if(highResTemp > 200)
    {
      highResTemp= highResTemp - 256.00;
    }

   delay(1000);
   return highResTemp;
  
}

You have
Serial.print(temp1);
just before your IF statement.

Is the IF statement behaving consistently with the value that is printed ?

Is the LED inserted the correct way round ?
Try using the on-board led on Pin 13 (assuming you are using an Uno)

less than 26 degrees

Your code is testing for 29

Take the delay(1000) out of the function Print_T1(). If you need it, put it into loop()

...R

try something simple on the LED like:

int led = 7;
void setup(void)
{
  Serial.begin(9600);
  pinMode(led, OUTPUT); 
}

void loop(void) 
{
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  delay(1000);
}
 float temp1= Print_T1();

Why would anyone expect a function with Print in it's name to return a value. That LOOKS like a mistake to me.

If the function really should return a value, then it's got a piss-poor name.