Lighting an LED when Temp is below 70

Hello all.
I am using a Grove starter kit plus. I have a temp sensor and the RGB Backlight display which is showing the room temp. That is working fine and I can see what the room temp is. I wanted to light an LED when the temp goes below 70 degrees. Later I want to hookup a relay to turn my boiler on and off based on room temp.

I am not able to make it work. I am pasting my code below. Am i missing something (which i am sure i am).? I left in my commented out code of what i tried before i tried the if statement.

Any help would be appreciated.

Thanks
Eddie


#include <Wire.h> //LDC libraries
#include "rgb_lcd.h"

rgb_lcd lcd; //Declare the LCD
const int pinTemp = A0; // pin of temperature sensor
const int pinLed = 3; // pin of led define here

//Set the color of LCD to red
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

float temperature;
int B=3975; // B value of the thermistor
float resistance;

void setup()
{
pinMode(pinLed, OUTPUT); // set led OUTPUT

Serial.begin(9600); //set to read output on serial monitor

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);

lcd.setRGB(colorR, colorG, colorB);

// Print a message to the LCD.
lcd.print("Room Temperature:");

delay(1000);
}

void loop()
{
int val = analogRead(pinTemp); // get analog value
resistance=(float)(1023-val)*10000/val; // get resistance
temperature=1/(log(resistance/10000)/B+1/298.15)-273.15; // calc temperature
temperature = (temperature * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit

Serial.println(temperature);

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(temperature);

if (temperature < 70)
{
digitalWrite (pinLed = HIGH);
}
else (temperature > 70)
{
digitalWrite (pinLed = LOW);
}

/*
{
for(int i=HIGH; temerature<70; i++)
{
analogWrite(pinLed, i);
delay(5);
}
delay(100);
for(int i=LOW; temerature>70; i--)
{
analogWrite(pinLed, i);
delay(100);
}
}
*/
delay(1000); // delay 1s

Sorry I did not enter the code correctly. I am adding it again here.

Thanks
Eddie

#include <Wire.h>  //LDC libraries
#include "rgb_lcd.h"

rgb_lcd lcd; //Declare the LCD
const int pinTemp = A0;      // pin of temperature sensor
const int pinLed    = 3;    // pin of led define here

//Set the color of LCD to red
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

float temperature;
int B=3975;                  // B value of the thermistor
float resistance;

void setup()
{
    pinMode(pinLed, OUTPUT);   // set led OUTPUT
  
    Serial.begin(9600);      //set to read output on serial monitor                  
    
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    
    lcd.setRGB(colorR, colorG, colorB);
    
    // Print a message to the LCD.
    lcd.print("Room Temperature:");

    delay(1000);
}

void loop()
{
    int val = analogRead(pinTemp);                               // get analog value
    resistance=(float)(1023-val)*10000/val;                      // get resistance
    temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;     // calc temperature
    temperature = (temperature * 9.0)/ 5.0 + 32.0;               // Convert Celcius to Fahrenheit
    
    Serial.println(temperature);
    
     // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    // print the number of seconds since reset:
    lcd.print(temperature);
    
    if (temperature < 70)
   {
     digitalWrite (pinLed = HIGH);
   }
   else (temperature > 70)
   {
     digitalWrite (pinLed = LOW);
   }
    
    
   /*
    {
    for(int i=HIGH; temerature<70; i++)
    {
      analogWrite(pinLed, i);
      delay(5);
    }
    delay(100);
    for(int i=LOW; temerature>70; i--)
    {
      analogWrite(pinLed, i);
      delay(100);
    }
    }
    */
    delay(1000);          // delay 1s

Please put your sketch in code tags. You can go back and edit the original post.

You don't need to check for both conditions <70 or >70 (though you have a dead point at exactly 70)

   if (temperature < 70.0)
   {
     digitalWrite (pinLed = HIGH);
   }
   else
   {
     digitalWrite (pinLed = LOW);
   }

You may find this Wikipedia entry on hysteresis useful, especially parts 3.1 and 3.2

.

Yes, I will look at it now. I also found many errors on my own. I fixed them. It still does not work though.

#include <Wire.h>  //LDC libraries
#include "rgb_lcd.h"

rgb_lcd lcd; //Declare the LCD
const int pinTemp = A0;      // pin of temperature sensor
const int pinLed    = 3;    // pin of led define here

//Set the color of LCD to red
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;

float temperature;
int B=3975;                  // B value of the thermistor
float resistance;

void setup()
{
    pinMode(pinLed, OUTPUT);   // set led OUTPUT
  
    Serial.begin(9600);      //set to read output on serial monitor                  
    
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    
    lcd.setRGB(colorR, colorG, colorB);
    
    // Print a message to the LCD.
    lcd.print("Room Temperature:");

    delay(1000);
}

void loop()
{
    int val = analogRead(pinTemp);                               // get analog value
    resistance=(float)(1023-val)*10000/val;                      // get resistance
    temperature=1/(log(resistance/10000)/B+1/298.15)-273.15;     // calc temperature
    temperature = (temperature * 9.0)/ 5.0 + 32.0;               // Convert Celcius to Fahrenheit
    
    Serial.println(temperature);
    
     // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    // print the number of seconds since reset:
    lcd.print(temperature);
    
    if (temperature < 70)
   {
     digitalWrite (pinLed, HIGH);
   }
   else (temperature > 70)
   {
     digitalWrite (pinLed, LOW);
   }

  
    delay(1000);          // delay 1s
}

What's this?

   if (temperature < 70)
   {
     digitalWrite (pinLed = HIGH);  // NO NO!
   }
   else (temperature > 70)  // no!
   {
     digitalWrite (pinLed = LOW);  // NO NO!
   }

Perhaps you meant

   if (temperature < 70)
   {
     digitalWrite (pinLed, HIGH);
   }
   else
   {
     digitalWrite (pinLed, LOW);
   }

or, much better:

  digitalWrite (pinLed, temperature < 70) ;  // true == HIGH, false == LOW

And the opposite condition to (temperature < 70) is (temperature >= 70), not
(temperature > 70)

I will give that a try. I saw that i made the first mistake of = instead of ,. I did fix that. I will try both styles. I like the last one though. Thank you for your help.

Eddie