Go Down

Topic: Temp Sensor How do I limit to 2 decimal places??? (Read 250 times) previous topic - next topic

PiMonkey

Jan 12, 2018, 05:46 pm Last Edit: Jan 12, 2018, 06:26 pm by PiMonkey
So to start off with I am a beginner to the arduino would.
I have merged two pieces of code to make a Temp sensor outputting to a 16x2 display.
It works fine to start with it gives me something like 10.00°C but after a 2 or 3 minutes it changes to 10.00.00°C My question is how do I make it stay 10.00°C permanently

there are two photos of the problem:
https://www.dropbox.com/s/mx8r3p680xmjgby/IMG_0688.JPG?dl=0[/img]
and this is the one after 2 minutes
https://www.dropbox.com/s/qq9jc2ewax42y5g/IMG_0687.JPG?dl=0[/img]

Here is my code: p.s. there is also a custom character


// include the library code:
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

int temp_sensor = 6;       // Pin DS18B20 Sensor is connected to
float temperature = 0;      //Variable to store the temperature in
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
OneWire oneWirePin(temp_sensor);

DallasTemperature sensors(&oneWirePin);

byte celsius[8] = {
  0b00100,
  0b01010,
  0b00100,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

void setup() {
  lcd.createChar(0, celsius);// create a new character

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  sensors.begin(); // activates the temperature sensor
  lcd.print("Temperature is ");
  lcd.setCursor(5, 2);
  lcd.write(byte(0));
  lcd.setCursor(6, 2);
  lcd.print("C");
  }
void loop(){
  sensors.requestTemperatures();
 
  temperature = sensors.getTempCByIndex(0); // sets up  the temperature variable
 
  lcd.setCursor(0, 2); // puts the display cursor at the bottom left
  lcd.print(temperature); // prints the temperature stored in the variable on the screen
  delay(750); // waits half a second before starting the loop again
}

larryd

#1
Jan 12, 2018, 10:47 pm Last Edit: Jan 12, 2018, 10:49 pm by larryd


Sounds like you are experiencing an artifact from a previous number on the screen.

Erase the line before you print anything on that line.


No technical PMs.
The last thing you did is where you should start looking.

avr_fred

#2
Jan 13, 2018, 01:22 am Last Edit: Jan 13, 2018, 02:26 am by avr_fred
Your cursor positioning codes are invalid, you have a two line display so the second number must be either 0 or 1. Correct the cursor positions and you'll fix your problem.

Code: [Select]
lcd.setCursor(5, 2);
lcd.setCursor(6, 2);
lcd.setCursor(0, 2); // puts the display cursor at the bottom left

PiMonkey

Thank you "larryd" your ideal wored great I used the lcd.clear() and now it works great I also had to move some things from the setup to the loop.
So Thank you

here is my new working code:

// include the library code:
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>

int temp_sensor = 6;       // Pin DS18B20 Sensor is connected to
float temperature = 0;      //Variable to store the temperature in
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
OneWire oneWirePin(temp_sensor);

DallasTemperature sensors(&oneWirePin);

byte celsius[8] = {
  0b00100,
  0b01010,
  0b00100,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

void setup() {
  lcd.createChar(0, celsius);// create a new character
  lcd.begin(16, 2);
  sensors.begin(); // activates the temperature sensor
  }
void loop(){
  sensors.requestTemperatures();
 
  temperature = sensors.getTempCByIndex(0); // sets up  the temperature variable
  lcd.clear();
  lcd.print("Temperature is ");
  lcd.setCursor(5, 2);
  lcd.write(byte(0));
  lcd.setCursor(6, 2);
  lcd.print("C");
  lcd.setCursor(0, 2); // puts the display cursor at the bottom left
  lcd.print(temperature); // prints the temperature stored in the variable on the screen
  delay(500); // waits half a second before starting the loop again
}

larryd

lcd.clear() this clears the whole display.



This would clear a line:
lcd.setCursor (0, 1);
lcd.print("                ") ;  
lcd.setCursor (0, 1);

Or

lcd.setCursor (0, 1) ;
for (int i = 0; i < 16; ++i)
{
 lcd.write(' ');
}
lcd.setCursor (0, 1) ;


No technical PMs.
The last thing you did is where you should start looking.

Go Up