Adding 3 warning LEDS to a working LCD shield Project.. Help !!!!!!

Hello All

I am a complete newbie with a Ardunio UNO, but have managed to get two of the Maxim DS18B20 thermometers working on one digital pin on my Uno and have the output temperatures displayed on separate lines of a 16x2 Robot LCD shield.

That part of my code works exactly as i want it too

But !!

What I am banging my head against a brick wall with is trying to get 3 LEDs to work that I am going to use as warning lights along with the temps displayed on the LCD shield.

These would light up to show temperatures below 70 degC a blue led would light up to show too cold

71 to 99 degC a green would light up to show normal temperature

and 100 degC and above a red led would light up to show too hot.
I know that my sketch does not show this, but I need to be able to warm or cool the DS18B20 to prove that the LED thing works

I could really do with some help in getting the LED thing to work as I have now spent 4 nights trying to find the code I need to get this to work and I am not getting anywhere as the last thing I attempted to programme on was my trusty old Atari STFM !!...

I have attached my attempt so far for you guys to laugh at !!

So if some one could help me with this sketch I would much appreciate the help and any hints on best way to construct the code needed would be vastly welcomed

Thank you

// DS18B20 2 sensors with LCD

// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>B20
#include <LiquidCrystal.h>
#include <SPI.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 8
// rw (LCD pin 5) to Arduino pin ground
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 4, 5, 6, 7
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int backLight = 13; // pin 13 will control the backlight

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress roomTemp = {
  0x28, 0xF8, 0x2C, 0x47, 0x05, 0x00, 0x00, 0x47 };
DeviceAddress ductTemp = {
  0x28, 0xD6, 0x8F, 0x47, 0x05, 0x00, 0x00, 0x9F
 };

void setup(void)
{
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(roomTemp, 10);
  sensors.setResolution(ductTemp, 10);

  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
  lcd.clear(); // start with a blank screen
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    lcd.print("Error");
  }
 
  else {
   tempC=tempC*1.8+32;
    lcd.print(tempC);
    lcd.print("/");
    lcd.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(0);
  sensors.requestTemperatures();
  lcd.setCursor(0,0);
  lcd.print("Outs Temp: ");
  printTemperature(roomTemp);
  lcd.setCursor(0,1);
  lcd.print("Insi Temp: ");
  printTemperature(ductTemp);
}


//set ledPin variables
#define bluePin 3 //dig pin 3
#define greenPin 4 //dig pin 4
#define redPin 5 //dig pin 5

 
  //set as output so LEDs work
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  
    tr = data[0];   if(temp <= 75) //cool
  {
    digitalWrite(bluePin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
  }
  else if(temp >= 76 && temp <= 80) //mid
  {
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
  }
  else if(temp >= 81) //hot
  {
    digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);
  }

You don't understand at all about program structure, you've got lots of statements
at top level, rather than in a function. Put them in loop () perhaps?

Hi Mark

Thanks for trying to help me

Unfortunatly i have only been playing with Arduino for 3 weeks and i have much to learn about it.

I have tried to add the LED code from what i could find on the net and i guess its not as easy as just pasting code into a sketch

I just thought there would be some simple code that i could insert to switch the LEDs on at certain temperatures

Is this some thing easy to do or is it more complex than i think it is ??

Is some thing eassy or is it more complex than i think it is ??

It's very easy.

When you copy code from the global variable section, you need to paste it into the global variable section, not at the end of the sketch.

Ditto for cope from setup() and loop(). Paste into the function of interest, not at the end.

While you are at it, get rid of the stupid printTemperature() function if you expect to have the temperature available in loop().

Hi Paul

Thank you for trying to point me in the right direction , but as my ability to write code is virtually zero at the moment i am struggling to take on board what yourself and Mark are trying to say.

I have searched the internet for the last 5 night trying to find a sketch to use or modify to allow the use of a LCD Shield to display the two lines of temperature and also have 3 leds lighting up to show temperature zones that i would like to look at inmy car without having to stare at the display to read the temperature

Hope some one can help me losing my mind...lol

Many thanks

I have searched the internet for the last 5 night trying to find a sketch to use or modify to allow the use of a LCD Shield to display the two lines of temperature

There is nothing even remotely complicated about that sketch you posted.

It creates an instance of the LiquidCrystal class, called lcd. It has instructions to clear the LCD and print some data on the LCD.

It does use a piss-poor function, printTemperature(). Whoever wrote that in the first place deserves to be horse-whipped. The stupid function needs to be replaced with one that GETS the temperature. The temperature should be PRINTED in loop(), after getTemperature() is called.

But, really, changing that function to return a value is trivial. One line needs to change. 8 lines need to be moved to loop(), and one line needs to be added, to return the value.

In loop(), then, it really isn't rocket science to be able to control WHERE in the LCD to print, and to print the two temperatures in different places.

As for the LEDs, the code to define the pin numbers goes before setup(). The pinMode() calls go in setup(). The rest of the code goes in loop(), and needs to refer to the temperature from ONE of the sensors. Which one is up to you.

Paul..

Your star and i cannot thank you enough

If were was nearer i would buy you a beer !!

2500+ miles is along way for a beer :astonished:

i have seen you help out many others trawling through this forum and its people like yourself who selflessly help idiots like me thats makes forums like this one so valuable.

Again i thank you

God help me when i play with the Udoo i bought =( =(

Taking on board what Paul stated, i have had a read a lot more about C++ and now i can get the sketch compile until i get the temperature greater or less than statements at the very end of the sketch and then it throws up error messages.

I am also finding it hard to find out how to get the outside temperature sensor code to be the source for lighting the LEDs at the greater or less than part of the sketch.

The LCD part of this works fine and reads both sensors and displays it on my LCD fine.
Its just the LEDS i am struggling with.

Can anyone throw some pointers or help with this??

Many Thanks

// DS18B20 2 sensors with LCD

// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>B20
#include <LiquidCrystal.h>
#include <SPI.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 8
// rw (LCD pin 5) to Arduino pin ground
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 4, 5, 6, 7
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int backLight = 13; // pin 13 will control the backlight

// Data wire is plugged into pin 8 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);


#define LOWTEMP  73 // Threshold for too cold
#define MIDTEMP  75 //  Threshold for Correct Temp
#define HIGHTEMP 80  // Threshold for too hot






//set ledPin variables
#define bluePin 4 //dig pin 4
#define greenPin 5 //dig pin 5
#define redPin 6 //dig pin 6

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress roomTemp = {
  0x28, 0xF8, 0x2C, 0x47, 0x05, 0x00, 0x00, 0x47 };
DeviceAddress ductTemp = {
  0x28, 0xD6, 0x8F, 0x47, 0x05, 0x00, 0x00, 0x9F
 };

void setup(void)
{
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(roomTemp, 10);
  sensors.setResolution(ductTemp, 10);

  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
  lcd.clear(); // start with a blank screen
  
  //set as output so LEDs work
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    lcd.print("Error");
  }
 
  else {
   tempC=tempC*1.8+32;
    lcd.print(tempC);
    lcd.print("/");
    lcd.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{
  delay(0);
  sensors.requestTemperatures();
  lcd.setCursor(0,0);
  lcd.print("Outs Temp: ");
  printTemperature(roomTemp);
  lcd.setCursor(0,1);
  lcd.print("Insi Temp: ");
  printTemperature(ductTemp);
  
  delay(0);
  sensors.requestTemperatures();
  lcd.setCursor(0,0);
  lcd.print("Outs Temp: ");
  printTemperature(roomTemp);
  lcd.setCursor(0,1);
  lcd.print("Insi Temp: ");
  printTemperature(ductTemp);
}

   if(temp <= 75) //cool
  {
    digitalWrite(bluePin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, LOW);
  }
  else if(temp >= 76 && temp <= 80) //mid
  {
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
    digitalWrite(redPin, LOW);
  }
  else if(temp >= 81) //hot
  {
    digitalWrite(bluePin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(redPin, HIGH);
  }

I am also finding it hard to find out how to get the outside temperature sensor code to be the source for lighting the LEDs at the greater or less than part of the sketch.

You don't have an outside temperature sensor, so it will never control the lights.

You have a roomTemp sensor and a ductTemp sensor. If you want to ask questions about them, feel free.

The LCD part of this works fine and reads both sensors

No, it doesn't. The LCD has nothing to do with reading the sensors. The Arduino does that, and gives the information to the LCD to display.

It does that using the greedy Mine! Mine! Mine! function called printTemperature() that doesn't share squat. I told you to shitcan that function, but you didn't listen. I'm done.

Hi Paul

I understand what your saying as you cannot see what i have done..oppsI hope your not done with me entirely ?

And i also understand your despair at me !! But i am a complete novice !!

On my LCD it actually displays Inside and outside temps as i have modded the code to do so

I have also modded the code now to reflect what the display reads. You getting the hump with me made me try harder and i found that you cannot have spaces in some situations and it now reads correctly and i have amended the sketch above to reflect that.

I have tried to remove the function called printTemperature() but if i do it throws up an error ??

I have tried to remove code until i have balanced braces, how much do i need to cut from the sketch and it still work ??

}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    lcd.print("Error");
  }
 
  else {
   tempC=tempC*1.8+32;
    lcd.print(tempC);
    lcd.print(" ");
    lcd.print(DallasTemperature::toFahrenheit(tempC));
  }

Many thanks

I have tried to remove the function called printTemperature() but if i do it throws up an error ??

It wouldn't, if you didn't call it.

Take a look at this function:

float getTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  return tempC;
}

It gets the temperature the same way as the crappy function you are using. But, it isn't greedy. It does exactly what it's name says AND shares the value:

void loop()
{
   float roomTemperature = getTemperature(roomTemp);
   // print the temperature on the LCD

   float ductTemperature = getTemperature(ductTemp);
   // print the temperature on the LCD

   // Now use either roomTemperature or ductTemperature to control the LEDs
}

Can you see how this is better than printTemperature() which thinks you don't need to actually know the temperature?