Help detecting button hold

Im having trouble with this part of my code:

  if (digitalRead(BUTTON_PIN) == LOW) {
    if (!buttonPressed) {
      buttonPressed = true;
      buttonPressTime = millis();
    } else if (millis() - buttonPressTime > 3000) {
      buttonLongPress = true;
    }
  } else {
    if (buttonPressed) {
      if (buttonLongPress) {
        maxTemp = -1000.0;
        minTemp = 1000.0;
        EEPROM.put(maxAddress, maxTemp);
        EEPROM.put(minAddress, minTemp);
        buttonLongPress = false;
      } else {
        isCelsius = !isCelsius;
      }
      buttonPressed = false;
    }
  }
}

I cant detect the button holding, i tried using a if (digitalRead(BUTTON_PIN) == LOW) delay(2000) if (digitalRead(BUTTON_PIN) == LOW) and it didnt worked too, any help appreciated. This is the full code:

#include <LiquidCrystal.h>
#include <EEPROM.h>

#define TEMP_SENSOR_PIN A0
#define BUTTON_PIN 8

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

bool isCelsius = true;
unsigned long buttonPressTime = 0;
bool buttonPressed = false;
bool buttonLongPress = false;

int maxAddress = 0; 
int minAddress = 0;

float currentTemp;
float maxTemp;
float minTemp;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  EEPROM.get(maxAddress, maxTemp);
  EEPROM.get(minAddress, minTemp);
  
  
  if (isnan(maxTemp) || isnan(minTemp)) {
    maxTemp = 0; 
    minTemp = 999; 
  }


  lcd.print("Temp: ");
  lcd.setCursor(0, 1);
  lcd.print(maxTemp);
  lcd.print(minTemp);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW){
  Serial.print("A");
}
  currentTemp = readTemperature();
  if (isCelsius) {
    lcd.setCursor(6, 0);
    lcd.print(currentTemp);
    lcd.print(" C  ");
  } else {
    lcd.setCursor(6, 0);
    lcd.print(currentTemp * 9 / 5 + 32);
    lcd.print(" F  ");
  }

  if (currentTemp > maxTemp) {
    maxTemp = currentTemp;
    EEPROM.put(maxAddress, maxTemp);
  }
  if (currentTemp < minTemp) {
    minTemp = currentTemp;
    EEPROM.put(minAddress, minTemp);
  }

  lcd.setCursor(0, 1);
  lcd.print(maxTemp);
  lcd.print(" -- ");
  lcd.print(minTemp);

  if (digitalRead(BUTTON_PIN) == LOW) {
    if (!buttonPressed) {
      buttonPressed = true;
      buttonPressTime = millis();
    } else if (millis() - buttonPressTime > 3000) {
      buttonLongPress = true;
    }
  } else {
    if (buttonPressed) {
      if (buttonLongPress) {
        maxTemp = -1000.0;
        minTemp = 1000.0;
        EEPROM.put(maxAddress, maxTemp);
        EEPROM.put(minAddress, minTemp);
        buttonLongPress = false;
      } else {
        isCelsius = !isCelsius;
      }
      buttonPressed = false;
    }
  }
}

float readTemperature() {
  int sensorValue = analogRead(TEMP_SENSOR_PIN);
  float val = sensorValue * 5.0 / 1024.0;
  return val * 100.0;
}

my objetive is so pressing the push button will change the value from Celsius to Fahrenheit. Pressing again returns to Celsius. Im using the EEPROM to store the maximum and minimum temperature values. If you press the button for more than 3 seconds, the maximum and minimum values are reset.

I’d suggest you check the code for one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, to understand how to deal with a button or just use one of the libraries…

1 Like
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.