# LCD Reading Temperature / Millis() function help

Hello everyone!
I am still learning this programming stuff and one of my codes is giving me a little trouble. Could you either tell me what I have done wrong or point me in the right direction? The goal is to have the LCD display the temp + a message and the corresponding LED illuminate (green and yellow solid, red and buzzer intermittent. My LCD seizes up and flashes all different shapes. I know its not the wiring because it only started acting up when I changed delay to millis(). Period2 = 1000ms. Any help would be amazing! Thanks

``````//CORRECT TEMP ----------------------------------------
// GREEN LED ON STEADY & LCD UPDATE EVERY 1 SECOND

if (temp > 150 && temp <= 160) {
digitalWrite(2, HIGH); //GREEN ON SOLID
startMillis = millis();
if (currentMillis - startMillis >= period2)
{lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.clear();}
}

//SLIGHTLY LOW TEMP ---------------------------------
//YELLOW LED ON STEADY & LCD UPDATE EVERY 1 SECOND

if (temp < 150 && temp >= 140) {
digitalWrite(4, HIGH); //YELLOW ON SOLID
startMillis = millis();
if (currentMillis - startMillis >= period2){
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" TEMP LOW ");
lcd.clear();}
}

//LOW TEMP ------------------------------------------

if (temp < 140) {
startMillis = millis();//SET TIME
if (currentMillis - startMillis >= period2){
digitalWrite(3, HIGH); //RED ON
digitalWrite(A1, HIGH);//BUZZER ON
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" STOP PRODUCTION ");
lcd.clear();}
}

digitalWrite(3, LOW); //RED OFF
digitalWrite(A1, LOW);//BUZZER OFF

//HIGH TEMP -----------------------------------------

if (temp > 160) {
startMillis = millis();//SET TIME
if (currentMillis - startMillis >= period2){
digitalWrite(3, HIGH); //RED ON
digitalWrite(A1, HIGH);//BUZZER ON
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" STOP PRODUCTION ");
lcd.clear();}
}

digitalWrite(3, LOW); //RED OFF
digitalWrite(A1, LOW);//BUZZER OFF

}
``````

Post ALL the code. The snippet is useless.

This seems odd:
startMillis = millis();//SET TIME
if (currentMillis - startMillis >= period2){

Where is currentMillis initialized?

“My LCD seizes up and flashes all different shapes.“
What kind of buzzer are you using?

``````#include <LiquidCrystal.h>
#define THERMISTORPIN A0 //  10K THERMISTOR
#define THERMISTORNOMINAL 10000  // RESISTANCE @ 25 DEGREES C
#define TEMPERATURENOMINAL 25  // TEMP FOR NOMINAL RESISTANCE C
#define NUMSAMPLES 5  // SAMPLES
#define BCOEFFICIENT 3950  // COEFFICIENT OF THE THERMISTOR 3000-4000)
#define SERIESRESISTOR 10000   //RESISTOR VALUE 10K
unsigned long startMillis;  //  START TIME
unsigned long currentMillis; //  CURRENT TIME
const unsigned long period = 500;  //  MILLISECONDS
const unsigned long period2 = 1000;  //  MILLISECONDS
uint16_t samples[NUMSAMPLES];
int enA = 6;
int in1 = 5;
int in2 = 7;
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() {
Serial.begin(9600);
startMillis = millis();  //initial start time
pinMode(3, OUTPUT); //red
pinMode(4, OUTPUT); //yellow
pinMode(2, OUTPUT); //green
pinMode(A0, INPUT); //temp
pinMode(A1, OUTPUT); //buzzer
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
lcd.begin(16, 2);
lcd.print("XENTRIC GUITARS         ");
delay(5000);
}

void loop() {
lcd.begin(16, 2); //START LCD DISPLAY
uint8_t i;
float average;

//DC MOTOR CONTROL --------------------------------

digitalWrite(in1, HIGH); //  FORWARD
digitalWrite(in2, LOW);
analogWrite(enA, 10);  //0-255

for (i=0; i< NUMSAMPLES; i++) {
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];}
average /= NUMSAMPLES;
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float temp;
float steinhart;
steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
steinhart = log(steinhart);                  // ln(R/Ro)
steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart;                 // INVERT
steinhart -= 273.15;                         // CONVERT TO C
temp = steinhart * 1.8 + 32;  // FINAL TEMP READING

//CORRECT TEMP ----------------------------------------
// GREEN LED ON STEADY & LCD UPDATE EVERY 1 SECOND

if (temp > 150 && temp <= 160) {
digitalWrite(2, HIGH); //GREEN ON SOLID
startMillis = millis();
if (currentMillis - startMillis >= period2)
{lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.clear();}
}

//SLIGHTLY LOW TEMP ---------------------------------
//YELLOW LED ON STEADY & LCD UPDATE EVERY 1 SECOND

if (temp < 150 && temp >= 140) {
digitalWrite(4, HIGH); //YELLOW ON SOLID
startMillis = millis();
if (currentMillis - startMillis >= period2){
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" TEMP LOW ");
lcd.clear();}
}

//LOW TEMP ------------------------------------------

while (temp < 140) {
startMillis = millis();//SET TIME
if (currentMillis - startMillis >= period2){
digitalWrite(3, HIGH); //RED ON
digitalWrite(A1, HIGH);//BUZZER ON
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" STOP PRODUCTION ");
lcd.clear();}
}

digitalWrite(3, LOW); //RED OFF
digitalWrite(A1, LOW);//BUZZER OFF

//HIGH TEMP -----------------------------------------

while (temp > 160) {
startMillis = millis();//SET TIME
if (currentMillis - startMillis >= period2){
digitalWrite(3, HIGH); //RED ON
digitalWrite(A1, HIGH);//BUZZER ON
lcd.print("Temp = ");
lcd.print(temp);
lcd.print(" F ");
lcd.setCursor(0,1);
lcd.print(" STOP PRODUCTION ");
lcd.clear();}
}

digitalWrite(3, LOW); //RED OFF
digitalWrite(A1, LOW);//BUZZER OFF

}
``````

larryd I just posted the whole code. Sorry I thought it was just in the last section that I changed. Its a little active buzzer which came in a starter kit. I actually have it disconnected now to stop the noise. Thanks

Where is ‘currentMillis’ initialized?

It's not...stupid mistake. I have changed this code so many times I'm surprised I didn't mess up more. Thank you. I'll add that in and see if that eliminates my problems.

This belongs in setup, not loop:

``````lcd.begin(16, 2); //START LCD DISPLAY
``````

What are you trying to achieve with the IF statements and the millis variables? Even if the IF statement is true, you are printing to the lcd display then immediately clearing the display, so you would have no time to actually see what was being displayed.

Got it thanks! I am trying to have the temperature update every second. If the temp falls within certain categories the LED's respond accordingly. I think my problem now is a setup issue. I want the temperature to update routinely and only print certain warnings if applicable. I probably don't explain things well...
So the LED color and message will change but the temp always prints every second.