DallasTemp + LCD3wire - Cant work it out

Hi everyone,

Only newly registered to the forums, although I have been browsing around a little. Glad I signed up, start giving and getting some more ideas for projects :slight_smile:

Im having a bit of a problem with a script I am trying to work out. I am getting a temperature from a Dallas1wire setup and then posting to serial aswell as to a 20x4 LCd with LCD3wire.

My serial prints out fine, but when I am printing to the LCD it is just a blank space. Its been bugging me for ages and I just cant figure it out. Here is the code:

#include <OneWire.h>
#include <LCD3Wire.h> 
#include <DallasTemperature.h>
#include <stdio.h>

#define LCD_LINES 2
#define DOUT_PIN 3
#define STR_PIN 2
#define CLK_PIN 4
#define ONE_WIRE_BUS 5
#define RELAY_A 6
#define RELAY_B 7

#define LDR 0
#define POT 1
#define BUTTON 2

int temp = 0;
int button_val = 0;
int lcd_clear = 0;
int ldr_val = 0;
char buf[8]="0";

/* LCD KS0066 4-bit data interface, 3 Arduino pins and MC14094 8-bit register
 *
 * MC14094 input:  Arduino digital pin 2=Clock, pin 4=Data, pin 7=Strobe
 * MC14094 output: Q8=DB4, Q7=DB5, Q6=DB6, Q5=DB7, Q4=E, Q3=RW, Q2=RS, Q1=None
 *
 *   +--------------------------------------------+
 *   |    Arduino (ATMega 168 or 328)             |
 *   |    D02           D03           D04         |
 *   +----+-------------+-------------+-----------+
 *        |4            |5            |6
 *        |1            |2            |3
 *   +----+-------------+-------------+-----------+
 *   |    Strobe        Data          Clock       |
 *   |    MC14094 8-bit shift/latch register      |
 *   |    Q8   Q7   Q6   Q5   Q4   Q3   Q2   Q1   |
 *   +----+----+----+----+----+----+----+----+----+
 *        |11  |12  |13  |14  |7   |6   |5   |4
 *        |11  |12  |13  |14  |6   |5   |4 
 *   +----+----+----+----+----+----+----+---------+
 *   |    DB4  DB5  DB6  DB7  E    RW   RS        |
 *   |               LCD KS0066                   |
 *   +--------------------------------------------+
 */
 
 /*  Button Input values
 *
 *
 *   +-----------+-----------+
 *   |           |           |
 *   |           |           |
 *   1           2           3
 *   |           |           |
 *  815         703         512
 *    \         / \          /
 *        597          416
 *          \          /
 *              377
 */

LCD3Wire lcd = LCD3Wire(LCD_LINES, DOUT_PIN, STR_PIN, CLK_PIN);
  
// 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();
  
  lcd.init();
  pinMode(RELAY_A, OUTPUT);
  pinMode(RELAY_B, OUTPUT);
  lcd_clear = 1;
}

void loop(void)
{ 

   //Clear LCD once only
   if(lcd_clear == 1){
     lcd.clear();
     //Add +1 to make sure clear is not triggered again
     lcd_clear ++;
   }
   
  lcd.cursorTo(1,0);
  lcd.printIn("Welcome to Pebble");
  lcd.cursorTo(2,1);
  lcd.printIn("v1.02 CCHS");
  //delay(50);
  
  button_val = analogRead(BUTTON);
  
  //When button is pressed, show temp
  while(button_val >= 810 && button_val <= 820) {
  
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  
  Serial.print("Temperature for the device 1 (index 0) is: ");
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);  
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("Temperature:");
  lcd.cursorTo(1,13);
  lcd.print(temp);
  
  //Update lcd_clear to trigger a lcd.clear() when Porgram is restarted
  lcd_clear = 1;
  //Update value to check if state has changed
  button_val = analogRead(BUTTON);
  }
  
  //When button is pressed, Show LDR sensor
  while(button_val >= 700 && button_val <= 708) {
  
  // request to all devices on the bus
  Serial.print("Reading LDR...");
  ldr_val = analogRead(LDR);
  Serial.println("DONE");
  
  Serial.print("Light reading for sensor is: ");
  Serial.println(ldr_val);  
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("Light Level:");
  lcd.cursorTo(1,12);
  lcd.printIn(itoa(ldr_val, buf, 10));
  delay(500);
  
  //Update lcd_clear to trigger a lcd.clear() when Porgram is restarted
  lcd_clear = 1;
  //Update value to check if state has changed
  button_val = analogRead(BUTTON);
  }
}

Here is a snippet of the serial output:

Requesting temperatures…DONE

Temperature for the device 1 (index 0) is: 28.25

Requesting temperatures…DONE

Temperature for the device 1 (index 0) is: 28.25

Requesting temperatures…DONE

Temperature for the device 1 (index 0) is: 28.31

Requesting temperatures…DONE

Temperature for the device 1 (index 0) is: 28.31

The temperature part is in the first while() loop, I just included the whole script for general reference.

Any help would be great thank you.
John

button_val = analogRead(BUTTON);
  
  //When button is pressed, show temp
  while(button_val >= 810 && button_val <= 820) {

What kind of button do you have that is connected to an analog pin? Why does it return these strange values? The button is either pressed, or not, isn’t it?

// request to all devices on the bus
  Serial.print("Reading LDR...");
  ldr_val = analogRead(LDR);
  Serial.println("DONE");

The LDR is on the bus?

Im having a bit of a problem with a script I am trying to work out. I am getting a temperature from a Dallas1wire setup and then posting to serial aswell as to a 20x4 LCd with LCD3wire.

My serial prints out fine, but when I am printing to the LCD it is just a blank space. Its been bugging me for ages and I just cant figure it out.

Have you written a simple script that tells the LCD to display some text? Without any other hardware (buttons, LDRs, temperature sensors) attached, did that script successfully display text?

What kind of button do you have that is connected to an analog pin? Why does it return these strange values? The button is either pressed, or not, isn't it?

I have 3 buttons hooked up to a single input with different value resistors. Just to save pins and get more actions.

The LDR is on the bus?

that's my error :\ In the rush of doing some copying and pasting that comment somehow got in there. The LDR is on its own pin

Have you written a simple script that tells the LCD to display some text? Without any other hardware (buttons, LDRs, temperature sensors) attached, did that script successfully display text?

Yep, every other command that is getting used by the LCD is working fine. the only part that isnt working is to display the temp.

The temp is also sending fine to serial, and my LCD is displaying the "Temperature:" line fine. I think it has to do with syntax maybe when I try to display the variable temp; :(

So, it appears as though the LCD3Wire class does not have an overloaded print method that handles floats.

You could convert the float variable to two ints, and print them, with a . in between.

Or he can use itoa or ftoa and simply print the string, google about itoa avr and ftoa avr.

I have managed to get to LCD to print out the base temp. Eg: temp = 24.12 Serial will print 24.12, but LCD will only print 24

  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  
  Serial.print("Temperature for the device 1 (index 0) is: ");
  float temp = sensors.getTempCByIndex(0);
  Serial.println(temp);
  
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("Temperature:");
  lcd.cursorTo(1,12);
  lcd.printIn(itoa(temp, buf, 10));

You could convert the float variable to two ints, and print them, with a . in between.

Im interested in doing this, could you please give me an example or link me to somewhere showing how to do this?

Im interested in doing this, could you please give me an example or link me to somewhere showing how to do this?

It's simple arithmetic - what is there to show?

Save the float variable into an integer variable: int intTemp = temp;

Then, subtract the integer value from temp: temp = temp - intTemp; // leaves 0.24

Then, multiply temp by some large value: temp *= 1000;

Now store the result in an int: int decTemp = temp + 0.5;

Finally, convert decTemp to a string, with leading 0s: char buffer[4]; sprintf(buffer, "%.3d", decTemp);

The temp variable, after multiplying by 1000, will contain a number between 0 and 999.999. Adding 0.5 will handle rounding. Storing it in an integer, and then using the .3d format will pad the value with leading spaces, using no more than 3 digits.

You should do this stuff first, in case the value in decTemp rounds up to 1000. If that happens, the temperature was something like 24.9999, which you would want to display as 25.000.