Temperature won't update automatically on lcd display

I am doing a project for a temperature sensor. I have an LCD display, a temperature sensor, a keypad, a neo pixel and a sounder. The LCD displays a line of text which says to select A or B. When A of the keypad is selected, the LCD display shows the temperature in degrees Celsius, When B is selected it shows in Fahrenheit. Neither of the temperatures will update automatically on the display, they will only update when the key is pressed again. The neo pixel changes colour for different temperatures and appears to update itself as the temperature varies and the sounder goes off at a certain temperature also.
I have tried everything to get the temperature to update itself but nothing seems to work.
Any ideas would be really helpful.
My program is below.

#include <Key.h>
#include <Keypad.h>

#include <Adafruit_NeoPixel.h>

#include <Adafruit_LiquidCrystal.h>  //Include the LCD library

#define NUM_LEDS 1  //This is the number of pixels in the strip (just 1 in our case)
#define PIN 8   //This is the pin number your neopixel is connected to

Adafruit_NeoPixel strip = Adafruit_NeoPixel (NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);   //Required by library
Adafruit_LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  //I/O pin numbers for the LCD

const int sensor = A0;     //TMP36 connected to analog input A0
const int buzzer = A5;     //Buzzer connected to analog input A5

const byte ROWS = 4;  //the keypad has four rows
const byte COLS = 1;  //and four columns
char keys [ROWS] [COLS] = {
{'A'},
{'B'},
{'C'},
{'D'},
};  //use an array to hold the key values

byte rowPins [ROWS] = {13, 12, 11, 10}; //define uno's I/O pin numbers
byte colPins [COLS] = {9};

Keypad keypad =
Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup () {
lcd.clear();
//set up the LCD's number of columns and rows:
lcd.begin (16, 2);   //Display has 16 columns and 2 rows
lcd.print ("Select A or B."); //Display a message
pinMode (sensor, INPUT);   //A0 is an input
pinMode (buzzer, INPUT);   //A5 is an input
strip.begin ();  //Function called from the library
Serial.begin(9600);
keypad.setDebounceTime (100);
}



void loop () {

float sensorVoltage, sensorTemperature, sensorTempf;
int sensorValue;
//Get sensor value and convert to deg.C
sensorValue = analogRead (sensor);
//Convert value to voltage
sensorVoltage = (sensorValue / 1024.0) * 4.88;
//Convert voltage to temperature celsius
sensorTemperature = (sensorVoltage - 0.5) * 100;
//convert temperature c to f
sensorTempf = (sensorTemperature * 1.8) + 32;
//display temperature
lcd.setCursor (0, 1);   //Begin on the second line

char key = keypad.getKey();  //let's check the keypad

if (key == 'A') { //lcd to display in degrees celsius when button A of keypad is pressed
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Temperature:");
 lcd.setCursor(0,1);
 lcd.print (sensorTemperature);  //print the temperature
 lcd.print (" ");
 lcd.write (B11011111);  //upper 'o' symbol
 lcd.print ("C");

}
else if (key == 'B') {  //lcd to display in degrees farenheit when button B of keypad is pressed
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("Temperature:");
 lcd.setCursor(0,1);
 lcd.print (sensorTempf);  //print the temperature
 lcd.print (" ");
 lcd.write (B11011111);  //upper 'o' symbol
 lcd.print ("F");

}


if (sensorTemperature >= 17 && sensorTemperature <= 24) {
 Serial.println("green");
 for (int i = 0; i <= 255; i++)
   strip.setPixelColor (i, strip.Color (0, 255, 0));    //green led
 strip.show ();   //sends data to pixel
}
else if (sensorTemperature < 17) {
 Serial.println("blue");
 for (int i = 0; i <= 255; i++)
   strip.setPixelColor (i, strip.Color (0, 0, 255));    //blue led
 strip.show ();   //sends data to pixel
}
else if (sensorTemperature > 24) {
 Serial.println("red");
 for (int i = 0; i <= 255; i++)
   strip.setPixelColor (i, strip.Color (255, 0, 0));    //red led
 strip.show ();   //sends data to pixel
}
Serial.println (sensorTemperature);

if (sensorTemperature >= 27){
tone (buzzer, 2000);   //buzzer on 
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (255, 0, 0));  // red flashes
strip.show ();
delay (50);
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (0, 0, 0));
strip.show ();
delay (50);
}
else {
 tone(buzzer, 0); //buzzer off
}
}

You are among the hundreds of users who do not read the introductory posts in the forum.
You should please your code post with code tags </>.
After that you may get an answer.

All your display code is conditional upon entering a key.

Just remember the last key entered, and make the display conditional on that value.

And use code tags.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

TheMemberFormerlyKnownAsAWOL:
All your display code is conditional upon entering a key.

Just remember the last key entered, and make the display conditional on that value.

And use code tags.

I'm new to arduino's so not sure what you mean by 'make the display conditional'?

Thanks

The only time that you print the temperature is when a keypress of 'A' or 'B' is detected such as this portion of code. Note how Auto Formatting it in the IDE shows up the problem by indenting the code and putting each { and } on its own line

  char key = keypad.getKey();  //let's check the keypad
  if (key == 'A')   //lcd to display in degrees celsius when button A of keypad is pressed
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperature:");
    lcd.setCursor(0, 1);
    lcd.print (sensorTemperature);  //print the temperature
    lcd.print (" ");
    lcd.write (B11011111);  //upper 'o' symbol
    lcd.print ("C");
  }

Personally I would change the code by creating a function to print the temperature and call it both when an 'A' or 'B' is entered or when the temperature changes

'Personally I would change the code by creating a function to print the temperature and call it both when an 'A' or 'B' is entered or when the temperature changes'

Could you give me an example for the function as I can't seem to get it to work.

Thanks

dulcie:
Could you give me an example for the function as I can't seem to get it to work.

Usually you would post your failed attempt in order to get help to fix it.

I have tried to add in a function and have only done it for the celcius when selecting A so far but it still gives me the same result where it won't update unless the button is pressed.

#include <Key.h>
#include <Keypad.h>

#include <Adafruit_NeoPixel.h>

#include <Adafruit_LiquidCrystal.h>  //Include the LCD library

#define NUM_LEDS 1  //This is the number of pixels in the strip (just 1 in our case)
#define PIN 8   //This is the pin number your neopixel is connected to

Adafruit_NeoPixel strip = Adafruit_NeoPixel (NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);   //Required by library
Adafruit_LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  //I/O pin numbers for the LCD

const int sensor = A0;     //TMP36 connected to analog input A0
const int buzzer = A5;     //Buzzer connected to analog input A5

const byte ROWS = 4;  //the keypad has four rows
const byte COLS = 1;  //and four columns
char keys [ROWS] [COLS] = {
  {'A'},
  {'B'},
  {'C'},
  {'D'},
};  //use an array to hold the key values

byte rowPins [ROWS] = {13, 12, 11, 10}; //define uno's I/O pin numbers
byte colPins [COLS] = {9};

Keypad keypad =
  Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  // put your setup code here, to run once:
 lcd.clear();
  //set up the LCD's number of columns and rows:
  lcd.begin (16, 2);   //Display has 16 columns and 2 rows
  lcd.print ("Select A or B."); //Display a message
  pinMode (sensor, INPUT);   //A0 is an input
  pinMode (buzzer, INPUT);   //A5 is an input
  strip.begin ();  //Function called from the library
  Serial.begin(9600);
  keypad.setDebounceTime (100);
}

float Celcius (float sensorVoltage){
  float resultC;
  resultC = (sensorVoltage-0.5)*100;
  return resultC;
}

float Farenheit (float sensorTemperature){
  float resultF;
  resultF = (sensorTemperature*1.8)+32;
  return resultF;
}
void loop() {
  // put your main code here, to run repeatedly:
 float sensorVoltage, sensorTemperature, sensorTempf;
  int sensorValue;
  //Get sensor value and convert to deg.C
  sensorValue = analogRead (sensor);
  //Convert value to voltage
  sensorVoltage = (sensorValue / 1024.0) * 4.88;
  //Convert voltage to temperature celsius
  sensorTemperature = (sensorVoltage - 0.5) * 100;
  //convert temperature c to f
  sensorTempf = (sensorTemperature * 1.8) + 32;
float C;
C = Celcius (sensorVoltage);

  char key = keypad.getKey();  //let's check the keypad


if (key == 'A'){
lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temperature:");
    lcd.setCursor(0, 1);
    lcd.print (C);
    lcd.print (" ");
    lcd.write (B11011111);  //upper 'o' symbol
    lcd.print ("C");
}


}

You need to read the sensor and if there has been a significant change since the previous reading then output the temperature

Pseudo code :

start of loop()
  read the current temperature
  if previous temperature differs from the current temperature by a significant amount
    output the temperature
  end if
  copy the current temperature to the previous temperature

  read the keypad
  if the 'A' key has been pressed
    output the temperature
  end if
end of loop()

function_to_output_temperature goes here