LCD Project with TMP36 & 2 x switchbutton

Hi I’m trying to do this project to switch from Celsius to Fahrenheit with the help of two push buttons, while showing the gradient temperature on my LCD.

But my program wont loop, it only runs once. I think my lcd.clear(); is messing with the project but i don’t know how.

Can anybody help me out?

Thank you,

tinker

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int TMP36 = 1; 
int Button = 6;    
int Button2 = 8;

int temperatureCelsius;
int temperatureFarenheit;
int voltage = TMP36;

void setup() {
  pinMode(Button, INPUT);   //pushbutton 1
  pinMode(Button2, INPUT); // pushbutton2
  
  lcd.begin(16, 2);
  lcd.clear(); // lcd clear function
}
 
void loop(){
  

 if(digitalRead(Button)==HIGH){ //if button1 is at logic high
   digitalWrite(Button,HIGH);
   digitalWrite(Button2,LOW); //then button2 is low
   
 int lecture = analogRead(TMP36);//reads the voltage from the tmp36
 
 float voltage = lecture * 5.0 / 1024;  // float voltage * 5 Volt / 1024 echellons
 
 float temperatureCelsius = (voltage - 0.5) * 100 ;//temperature
                
                 lcd.setCursor(1, 0);  
                 lcd.print(temperatureCelsius); lcd.print(" degres C   ");
                 delay(200);  
                 lcd.setCursor(1,1);
                 lcd.print("               ");
                 Button=!Button2;
                

}
    
 
         //part 2 
         if(digitalRead(Button2)==HIGH){
         digitalWrite(Button,LOW);
         digitalWrite(Button2,HIGH);
         
         int lecture = analogRead(TMP36);
 
         float voltage = lecture * 5.0 / 1024;  // float voltage * 5 Volt / 1024 echellons
 
         
           float temperatureFarenheit = (((voltage - 0.5)* 100)*1.8)+ 32.0; //conversion to Fahrenheit
                 lcd.setCursor(1,0);
                 lcd.print("                 ");
                lcd.setCursor(1, 1);
                lcd.print(temperatureFarenheit); lcd.println(" degres F ");
                delay(200);
                Button2=!Button;
               
         }
             
}

If you write the question in English, you are obviously assuming the people reading it understand English.

// le résultat est affiché par l'option outil/moniteur série

it might be a good idea to translate all of the code into English if it was not originally written that way

sorry about that, i thought i didn't leave any commentary in another language...

I fixed it though, i don't usually pay that much attention to my comments hope it wasn't too much of a pain!

:P

I haven’t got the suitable hardware to test your sketch, but it appears to me your button press logic is at fault. What I mean is, you should use a state variable, and not try to digitalWrite to your inputs… Just a thought?

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int TMP36 = 1;
int Button1 = 6;
int Button2 = 8;

bool showFarenheit = false;
long mymillis, oldmillis = millis()-1000;

void setup() {
  pinMode(Button1, INPUT); // pushbutton 1
  pinMode(Button2, INPUT); // pushbutton 2

  lcd.begin(16, 2);
  lcd.clear(); // lcd clear function
}

void loop() {

  if (digitalRead(Button1) == HIGH && digitalRead(Button2) == LOW) {
    showFarenheit = false; //if button1 is at logic high
  }
  if (digitalRead(Button2) == HIGH && digitalRead(Button1) == LOW) {
    showFarenheit = true;  //if button2 is at logic high
  }

  float lecture = analogRead(TMP36);//reads the voltage from the tmp36
  float voltage = lecture * 5.0 / 1024;  // float voltage * 5 Volt / 1024 echellons
  float temperatureCelsius = (voltage - 0.5) * 100 ;//temperature
  float temperatureFarenheit = (temperatureCelsius  * 1.8) + 32.0; //conversion to Fahrenheit
  mymillis = millis();

  if (mymillis - oldmillis > 1000) {
    // Update display every second but constantly read switches
    if (showFarenheit == false) {
      //show Celsius
      lcd.setCursor(1, 0);
      lcd.print(temperatureCelsius); lcd.print(" degres C   ");
      lcd.setCursor(1, 1);
      lcd.print("               ");
    }
    else
    {
      //show farenheit
      lcd.setCursor(1, 0);
      lcd.print("                 ");
      lcd.setCursor(1, 1);
      lcd.print(temperatureFarenheit); lcd.println(" degres F ");
    }
    oldmillis = mymillis;
  }
}

Can’t promise it will work, but try that?

Regards,

Graham

Works very well!!

What made it click compared to mine, is it the millis??

Thanks

Tinker

Ideally, you don’t want to use delay() in your loop() function, it stops everything in the loop while waiting for the delay. Also, I think you were upsetting things by using digitalWrite to your input pins…

Doing it the way I have, the loop runs at full speed, as such, no matter when or how long you press a button, it will be detected, although it could take upto a second for the display to change. Done the way you did it, if you happened to push the button for less than 200ms and during the delay, your loop would not even see a button press.

  if (digitalRead(Button1) == HIGH && digitalRead(Button2) == LOW) {
    showFarenheit = false; //if button1 is at logic high
    oldmillis=oldmillis-1000;
  }
  if (digitalRead(Button2) == HIGH && digitalRead(Button1) == LOW) {
    showFarenheit = true;  //if button2 is at logic high
    oldmillis-=1000;
  }

If you change your button functions to include oldmillis-=1000, your display would change instantly on button press :wink:

Pleased it works for you.

Regards,

Graham