Arduino + LCD + single pushbutton

Hey guys,

I am trying to make my project switch between displaying centigrade and fahrenheit everytime i press a button. I have it all connected ok, but the only way i can seem to get it to work is if i hold the button down. But i want it to ‘switch’ everytime i press the button instead.

Any help would be excellent.

Here is my code.

#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);

int switchOne = 10;
int valOne = 0;
float analogueReading = 0;
float degreesC = 0;
float degreesF = 0;

void setup() {
  pinMode(switchOne, INPUT);
}

void loop() {
  analogueReading = analogRead(0);
  float analogueRead = analogRead(0);
  float degreesC;
  float degreesF;
  valOne = digitalRead(switchOne);

  degreesC = (analogueReading * 500) /1024;
  degreesF = (degreesC * 9.0) / 5.0 + 32.0;

  if (valOne == HIGH) {
    lcd.begin(16,2);
    lcd.print(degreesC);
    lcd.setCursor (7,0);
    lcd.print((char)223);
    lcd.setCursor (8,0);
    lcd.print("C");
    lcd.setCursor (12,0);
    lcd.print("CW17");
    delay(1000);
  }else{
    lcd.begin(16,2);
    lcd.print(degreesF);
    lcd.setCursor (7,0);
    lcd.print((char)223);
    lcd.setCursor (8,0);
    lcd.print("F");
    lcd.setCursor (12,0);
    lcd.print("CW17");
    delay(1000);
  }
}

But i want it to 'switch' everytime i press the button instead.

"Then" "you" "need" "to" "get" "rid" "of" "the" "delay()s".

Did the useless quote marks help?

cmanu83: Hey guys,

I am trying to make my project switch between displaying centigrade and fahrenheit everytime i press a button. I have it all connected ok, but the only way i can seem to get it to work is if i hold the button down. But i want it to 'switch' everytime i press the button instead. Any help would be excellent.

There is no circuit diagram or wiring plan available?

Simplest possible button circuit would be: Connect a wire from pole-1 of the push button to an Arduino pin and pole-2 of the push button to GND (Don't use any external pull up resistor or pull-down resistor)

Instead activate the "internal pull-up resistor of the Atmega controller within the setup()

pinMode(switchOne,INPUT_PULLUP);

Does it sound doable?

BTW: If you want to update the display once every second, while it is possible to change from Celsius to Fahrenheit at any time between, you better get rid of the delay and do all timings just using millis().

Which is going to be the initial state for the display, used before any button is pressed: Initially Celsius or initially Fahrenheit?

I have attached the fritzing

cmanu83:
I have attached the fritzing

I have changed your code for switching display °C/°F please give it a try:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);

const byte switchOne = 10;
const byte sensorPin=A0;
int sensorValue;
float degreesC, degreesF;
bool showCelsius=false;

bool buttonPressed()
{
 static unsigned long lastRunTime;
 static bool lastButtonState;
 if (millis()-lastRunTime<5) return false; //debounce for 5ms bounce time
 lastRunTime=millis();
 bool result=false;
 bool buttonState = digitalRead(switchOne);
 if (buttonState == LOW && lastButtonState == HIGH) result=true;
 lastButtonState=buttonState;
 return result;
}


void readSensor()
{
  sensorValue=analogRead(sensorPin);
    degreesC = sensorValue * 5.0*100/1024;
  degreesF = (degreesC * 9.0) / 5.0 + 32.0;
}


void updateDisplay()
{
  lcd.setCursor(0,0);
  if(showCelsius)
  {
    lcd.print(degreesC);
    lcd.write(223);
    lcd.print('C');
    
    }
else    
  {
    lcd.print(degreesF);
    lcd.write(223);
    lcd.print('F');
    
    }
}

void setup() 
{
  lcd.begin(16,2);
    
  pinMode(switchOne, INPUT_PULLUP);
}

void loop() 
{
  static unsigned long lastUpdateTime;
  bool displayNeedsUpdate=false;
  readSensor();
  if(buttonPressed())
  {
    showCelsius=!showCelsius; // negation switches C/F display
    displayNeedsUpdate=true;
  }
  if (millis()-lastUpdateTime>=1000) displayNeedsUpdate=true;
  if (displayNeedsUpdate)
  {
   updateDisplay();
   lastUpdateTime=millis(); 
  }
}

But be warned, untested code, I just wrote it down.
I tested only that it compiles error-ree, otherwise fully untested!

As it is, the code is activating and using internal pull-up resistor. With your Fritzing and external pull-down resistor you may find, that switching from Celsius to Fahrenheit and other way round does not happen when pressing the button, but when releasing the button.

Give it a try and tell a few words, please.