DS18B20 - temperature sensor code Help Request

LATEST CODE - with 2 leds 1 for each alarm

With no alarm lcd displays: ALL GOOD

TEMP0 = 22.25 ( CURRENT TEMP)
TEMP1 = 22.25 ( CURRENT TEMP)

When alarm occurs on temp1 LCD displays: ALL GOOD

TEMP0 = 22.25 ( CURRENT TEMP)
Alarm1 = 29.50 ( CURRENT TEMP) plus Led is ON Steady

When alarm occurs on temp0 LCD displays:

Alarm0 = 25.50 ( CURRENT TEMP) "Alarm0" is flashing between Temp0 and Alarm0 and LED is blinking. BAD
TEMP1 = 22.50 ( CURRENT TEMP)

#include <OneWire.h>

#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27  16/2
// LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27  20/


// pin connected to sensor
int tempPin = 2;
// define the onewire obj needed for connecting to onewire components
OneWire oneWire(tempPin);
// define dallas obj, makes it easier to read temp
DallasTemperature tempSens(&oneWire);

DeviceAddress insideThermometer = { 
  0x28, 0x25, 0x33, 0xDC, 0x03, 0x00, 0x00, 0x7E };
DeviceAddress outsideThermometer = { 
  0x28, 0x67, 0x3D, 0xDC, 0x03, 0x00, 0x00, 0xC7 };


void setup() {
  // set up the serial console
  Serial.begin(9600);
  pinMode(7, OUTPUT); //pin selected to control OVER TEMP LED red temp0  
  pinMode(8, OUTPUT); //pin selected to control OVER TEMP LED green temp1
  
  // digitalWrite(7, LOW);


  lcd.init(); // initialize the lcd
  lcd.backlight();
  //lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
  lcd.clear(); // start with a blank screen



  // initialise the temp sensor

    tempSens.begin();
  // set the resolution to 10 bit (good enough?)
  tempSens.setResolution(insideThermometer, 10);
  tempSens.setResolution(outsideThermometer, 10);

}

void loop() {
  // get the current temperature

  tempSens.requestTemperatures();

  // get the temperature in centigrade
  // index 0 as multiple temp sensors can be connected on same bus

    float t = tempSens.getTempCByIndex(0);

  Serial.print("TEMP0 = ");
  Serial.print(t);
  lcd.setCursor(0,0);
  lcd.print("TEMP0 = ");
  lcd.print(t);
  delay(200);

  float t1 = tempSens.getTempCByIndex(1);
  lcd.setCursor(0,1);
  lcd.print("TEMP1 = ");
  lcd.print(t1);
  Serial.print("TEMP1= ");
  Serial.print(t1);
  //delay(200);

  if  (t  > 29) //sensor0
  {
    lcd.setCursor(0,0);
    Serial.println("Alarm0= ");
    lcd.print("Alarm0= ");
    lcd.setCursor(8,0);
    Serial.println(t);
    lcd.print(t);
    digitalWrite(7,HIGH);
  }

  if  (t1  > 25) //sensor 1
  {
    lcd.setCursor(0,1);
    Serial.println("Alarm1= ");
    lcd.print("Alarm1= ");
    lcd.setCursor(8,1);
    Serial.println(t1);
    lcd.print(t1);
    digitalWrite(8,HIGH);
  }
  else 
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }









}

Snipping a bunch of code, you have this:

  if  (t  > 29) //sensor0
  {
  }
  if  (t1  > 25) //sensor 1
  {
  }
  else 
  {
  }

You should have an else block for the first condition, and the t and t1 blocks should be independent, in terms of control of the two LEDs.

I hate t and t1 as names. t1 and t2 would, in my opinion, be better, but tempIn and tempOut would be even better. If you are going to number things, number ALL of them; don't start numbering with the 2nd one.

How about sensor0 and sensor1?

How about sensor0 and sensor1?

Marginal. sensor is too generic, for one thing. The numbering is wrong, too. If you lay a bunch of sensors on a table, and ask 100 people to count them, 99 or more will count 1, 2, ...

If the sensors are really that generic, then they belong in an array, anyway.

Yeah, try asking someone to pick up the zero'th item on the table and see what happens.

Yeah, try asking someone to pick up the zero'th item on the table and see what happens.

The 0th element in the array is some kind of reference to some specific object on the table. The fact that the array index is 0 is not really relevant.

But, I do understand your point.

  1. As suggested, I changed the variable names as suggested temp0 and temp1.

2.Also, added the else statement to isolate each condition.
This has cleared up the LED blinking problem - on Temp0 -GREAT

  1. PROBLEM:

When Temp0 > 29 LED0 turns on steady (GOOD) but LCD flashes between the word "Temp0" and "Alarm0" - Not Good!

How do I correct Flashing LCD problem? Suggestions ?

When Temp1 > 25 LED1 turns on steady (GOOD) and "Alarm1" is displayed steady - no flashing - Good!
This is how Temp0 should work

LATEST CODE BELOW:

#include <OneWire.h>

#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27  16/2
// LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27  20/


// pin connected to sensor
int tempPin = 2;
// define the onewire obj needed for connecting to onewire components
OneWire oneWire(tempPin);
// define dallas obj, makes it easier to read temp
DallasTemperature tempSens(&oneWire);

DeviceAddress insideThermometer = { 
  0x28, 0x25, 0x33, 0xDC, 0x03, 0x00, 0x00, 0x7E };
DeviceAddress outsideThermometer = { 
  0x28, 0x67, 0x3D, 0xDC, 0x03, 0x00, 0x00, 0xC7 };


void setup() {
  // set up the serial console
  Serial.begin(9600);
  pinMode(7, OUTPUT); //pin selected to control OVER TEMP LED red temp0  
  pinMode(8, OUTPUT); //pin selected to control OVER TEMP LED green temp1

  // digitalWrite(7, LOW);


  lcd.init(); // initialize the lcd
  lcd.backlight();
  //lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
  lcd.clear(); // start with a blank screen



  // initialise the temp sensor

    tempSens.begin();
  // set the resolution to 10 bit (good enough?)
  tempSens.setResolution(insideThermometer, 10);
  tempSens.setResolution(outsideThermometer, 10);

}

void loop() {
  // get the current temperature

  tempSens.requestTemperatures();

  // get the temperature in centigrade
  // index 0 as multiple temp sensors can be connected on same bus

    float temp0 = tempSens.getTempCByIndex(0);

  Serial.print("TEMP0 = ");
  Serial.print(temp0);
  lcd.setCursor(0,0);
  lcd.print("TEMP0 = ");
  lcd.print(temp0);
  delay(200);

  float temp1 = tempSens.getTempCByIndex(1);
  lcd.setCursor(0,1);
  lcd.print("TEMP1 = ");
  lcd.print(temp1);
  Serial.print("TEMP1= ");
  Serial.print(temp1);
  //delay(200);

  if  (temp0  > 29) //sensor0 temp0
  {
    lcd.setCursor(0,0);
    Serial.println("Alarm0= ");
    lcd.print("Alarm0= ");
    lcd.setCursor(8,0);
    Serial.println(temp0);
    lcd.print(temp0);
    digitalWrite(7,HIGH); //LED0 /FAN0
  }
else 
  {
    digitalWrite(7,LOW);
   
  }


  if  (temp1  > 25) //sensor 1 temp1
  {
    lcd.setCursor(0,1);
    Serial.println("Alarm1= ");
    lcd.print("Alarm1= ");
    lcd.setCursor(8,1);
    Serial.println(temp1);
    lcd.print(temp1);
    digitalWrite(8,HIGH); //LED1 /FAN1
  }
  else 
  {
    
    digitalWrite(8,LOW);
  }









}

How do I correct Flashing LCD problem? Suggestions ?

Simple. Move the code for printing to the LCD into the if and else blocks, too.

That did the trick - putting the temp code in the else area

Thanks to all for the help!!

Code working fine!!

LCD DISPLAY - i2c 20/4
3 SENSORS - ds18B20
2 LEDS - 1 for red LED for high temp alarm and another LED for normal temperature

Joe