Problem with temperature updating in the code

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <Encoder.h>
#include <max6675.h> // Include the MAX6675 library

// Define pins for Rotary Encoder
#define ROTARYPIN1 2
#define ROTARYPIN2 3
#define ROTARYSWITCH 4

// LCD setup
hd44780_I2Cexp lcd;

// Encoder setup
Encoder encoder(ROTARYPIN1, ROTARYPIN2);
long lastPosition = -999;

// MAX6675 setup
int thermoDO = 5;
int thermoCS = 6;
int thermoCLK = 7;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

// System settings
float temperatureThreshold = 0.0;
int wateringDuration = 5;
int wateringInterval = 5;
bool pumpStatus = false; // false = OFF, true = ON

// Pump control variables
unsigned long lastPumpTime = 0;  // Last time the pump was activated
bool pumpActive = false;         // Current status of the pump

// Variable to store the current menu page
int currentPage = 0;
bool buttonPressed = false;
unsigned long buttonPressTime = 0;
const unsigned long longPressDuration = 1000; // Long press duration in milliseconds
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds
unsigned long lastDebounceTime = 0;

// Timer variables for temperature update
unsigned long lastTempUpdateTime = 0;
const unsigned long tempUpdateInterval = 1000; // Update temperature every second

void setup() {
  Serial.begin(9600);
  
  // Initialize Wire library
  Wire.begin();

  // Initialize LCD
  lcd.begin(20, 4);
  lcd.clear();

  // Set LCD backlight brightness (0-255, 0 = off, 255 = max brightness)
  lcd.setBacklight(5);  // Set backlight brightness to 5

  // Initialize encoder
  encoder.write(0);

  // Show initial page
  displayPage();
}

void loop() {
  long newPosition = encoder.read();

  if (newPosition != lastPosition) {
    if (!buttonPressed && currentPage != 6) { // Ignore encoder when on page 6
      adjustValue(newPosition);
    }
    lastPosition = newPosition;
  }

  // Check for button press
  if (digitalRead(ROTARYSWITCH) == LOW && !buttonPressed) {
    buttonPressed = true;
    buttonPressTime = millis();
  }

  // Check for button release
  if (digitalRead(ROTARYSWITCH) == HIGH && buttonPressed) {
    if (millis() - buttonPressTime >= debounceDelay) {
      if ((millis() - buttonPressTime) < longPressDuration) {
        // Short click
        handleShortClick();
      } else {
        // Long click
        handleLongClick();
      }
    }
    buttonPressed = false;
  }

  // Check the pump status continuously
  updatePumpStatus();

  // Update temperature continuously in case 6
  if (currentPage == 6) {
    displayPage();
  }
}

void adjustValue(long newPosition) {
  // Adjust value based on encoder rotation
  if (newPosition > lastPosition) {
    // Increase value
    switch (currentPage) {
      case 2:
        if (temperatureThreshold < 255.0) {
          temperatureThreshold += 0.1;
        }
        break;
      case 3:
        if (wateringDuration < 255) {
          wateringDuration += 1;
        }
        break;
      case 4:
        if (wateringInterval < 255) {
          wateringInterval += 1;
        }
        break;
      default:
        break;
    }
    displayPage();
  } else if (newPosition < lastPosition) {
    // Decrease value
    switch (currentPage) {
      case 2:
        if (temperatureThreshold > 0.0) {
          temperatureThreshold -= 0.1;
        }
        break;
      case 3:
        if (wateringDuration > 0) {
          wateringDuration -= 1;
        }
        break;
      case 4:
        if (wateringInterval > 0) {
          wateringInterval -= 1;
        }
        break;
      default:
        break;
    }
    displayPage();
  }
}

void handleShortClick() {
  unsigned long currentMillis = millis();
  if (currentMillis - lastDebounceTime > debounceDelay) {
    lastDebounceTime = currentMillis;
    // Toggle between page 5 and 6 when either is the current page
    if (currentPage == 5) {
      currentPage = 6;
    } else if (currentPage == 6) {
      currentPage = 5;
    } else {
      currentPage++;
      if (currentPage > 6) currentPage = 0;
    }
    displayPage();
  }
}

void handleLongClick() {
  // Handle long click only on allowed pages
  if (currentPage > 2) {
    currentPage = (currentPage == 0) ? 6 : currentPage - 1; // Navigate to previous page or last page if currently on the first page
    displayPage();
  }
}

void updatePumpStatus() {
  float currentTemp = readTemperature();
  unsigned long currentTime = millis();
  if (currentTemp > temperatureThreshold && currentTime - lastPumpTime > wateringInterval * 3600000UL && !pumpActive) {
    pumpActive = true;
    lastPumpTime = currentTime;
    lcd.setCursor(0, 1);
    lcd.print("PUMP: ON           ");
  } else if (pumpActive && currentTime - lastPumpTime >= wateringDuration * 1000UL) {
    pumpActive = false;
    lcd.setCursor(0, 1);
    lcd.print("PUMP: OFF          ");
  }
}

void displayPage() {
  lcd.clear();
  switch (currentPage) {
    case 0:
      lcd.print("Temp-Based System");
      break;
    case 1:
      lcd.print("Click to Start Setup");
      break;
    case 2:
      lcd.print("Threshold: "); lcd.print(temperatureThreshold); lcd.print(" C");
      lcd.setCursor(0, 1);
      lcd.print("CLICK TO CONFIRM");
      break;
    case 3:
      lcd.print("Duration: "); lcd.print(wateringDuration); lcd.print(" sec");
      lcd.setCursor(0, 1);
      lcd.print("CLICK TO CONFIRM");
      break;
    case 4:
      lcd.print("Interval: "); lcd.print(wateringInterval); lcd.print(" hr");
      lcd.setCursor(0, 1);
      lcd.print("CLICK TO CONFIRM");
      break;
    case 5:
      lcd.print("Review Settings");
      lcd.setCursor(0, 1);
      lcd.print("Threshold: "); lcd.print(temperatureThreshold); lcd.print(" C");
      lcd.setCursor(0, 2);
      lcd.print("Duration: "); lcd.print(wateringDuration); lcd.print(" sec");
      lcd.setCursor(0, 3);
      lcd.print("Interval: "); lcd.print(wateringInterval); lcd.print(" hr");
      break;
    case 6:
      // Update temperature display every time page 6 is displayed
      lcd.print("Temperature: "); lcd.print(readTemperature()); lcd.print(" C");
      lcd.setCursor(0, 1);
      lcd.print(pumpActive ? "PUMP: ON" : "PUMP: OFF");
      break;
    default:
      currentPage = 0;
      displayPage();
      break;
  }
}

float readTemperature() {
  return thermocouple.readCelsius(); // Read temperature from MAX6675 sensor
}

Can anyone help me understand why the temperature in case 6 does not update continuously?

The sensor datasheet should give an indication of the time between sensor data updates, usually two seconds. Your Case 6 has no timer, so it is probably reading the sensor too often to get a valid reading.

I see your code has a debounce timer and a longpress timer. Use a similar timer to restrict "case 6" to two-seconds after the previous temperature reading.

Change your temperature reading to TWO seconds (currently at ONE second).

1 Like

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