Hello people, I am working on a project to replace the existing warning indicator lights on my John Deere 322 tractor with a Nextion Display that will provide lots more info on the Dashboard, basically going from this:
to this:
I got most of it working, the only thing I am having a problem is with the Progress Bars for the Engine Temperature and Gas Level. They will go up as values rise, but will not go down. Even if I reset the arduino with reset button, the Nextion Display will be stuck at the same image. The Nextion display seems to keep the last values and doesn't reset with new values. All text fields, however, are working like they should but if I click on the Arduino Reset button, All values on the display are still active.
However, the values printed on the Arduino IDE serial are all updating normally as sensor values changes. The only way to reset the Progress bars is to cut power to the Nextion Display.
Can anyone help me with this problem?
Here is my current code:
#include <Wire.h>
#include <GyverINA.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "MAX6675.h" // Include the Tillard MAX6675 library
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary
// Pin assignments for MAX6675 (Thermocouple for engine temperature)
const int dataPin = 12;
const int clockPin = 13;
const int selectPin = 10;
MAX6675 thermoCouple(selectPin, dataPin, clockPin);
uint32_t start, stop;
// Fuel sender unit voltage divider
#define FUEL_PIN A0
// Hall Effect Sensor (RPM)
#define HALL_SENSOR_PIN 2
// OneWire for DS18B20 sensors
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideSensor, outsideSensor;
// RPM Calculation Variables
volatile int pulseCount = 0;
unsigned long previousMillis = 0;
const unsigned long pulseInterval = 100; // Update RPM every 100 ms
int rpm = 0;
// EasyNextionLibrary object
EasyNex myNex(Serial2);
const int REFRESH_TIME = 100; // Refresh interval in milliseconds
unsigned long refresh_timer = millis();
// INA226 for battery voltage measurement
INA226 batteryMonitor(0x40); // Default I2C address for INA226
void hallInterrupt() {
pulseCount++;
}
void setup() {
Serial.begin(115200);
myNex.begin(115200);
sensors.begin();
SPI.begin();
thermoCouple.begin();
thermoCouple.setSPIspeed(4000000);
// Assign DS18B20 addresses if detected
if (sensors.getDeviceCount() >= 2) {
sensors.getAddress(insideSensor, 0);
sensors.getAddress(outsideSensor, 1);
}
pinMode(HALL_SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_PIN), hallInterrupt, FALLING);
// Initialize INA226
if (batteryMonitor.begin()) {
Serial.println(F("INA226 initialized successfully."));
} else {
Serial.println(F("Failed to initialize INA226."));
while (1)
;
}
}
void loop() {
unsigned long currentMillis = millis();
myNex.NextionListen(); // This function must be called repeatedly to response touch events
delay(100);
// unsigned long currentMillis = millis();
// Read DS18B20 temperatures
float insideTemp = NAN;
float outsideTemp = NAN;
// Read DS18B20 temperatures
if (sensors.getDeviceCount() >= 2) {
sensors.requestTemperatures();
insideTemp = sensors.getTempC(insideSensor);
outsideTemp = sensors.getTempC(outsideSensor);
myNex.writeStr("Tin.txt", String(insideTemp, 0));
myNex.writeStr("Tout.txt", String(outsideTemp, 0));
} else {
myNex.writeStr("Tin.txt", "-");
myNex.writeStr("Tout.txt", "-");
}
// Read MAX6675 (engine temperature)
int status = thermoCouple.read();
float engineTemp = thermoCouple.getTemperature();
myNex.writeStr("EngineT.txt", String(engineTemp, 0));
int scaledTemp = map(engineTemp, 0, 110, 0, 100);
scaledTemp = constrain(scaledTemp, 0, 100);
myNex.writeNum("TempGauge.val", scaledTemp);
// **Fuel Level Mapping**
int fuelADC = analogRead(FUEL_PIN);
float fuelPercent = map(fuelADC, 0, 292, 0, 100);
fuelPercent = constrain(fuelPercent, 0, 100);
myNex.writeNum("FuelGauge.val", fuelPercent);
// **Battery Voltage**
float batteryVoltage = NAN; // Declare batteryVoltage here
batteryVoltage = batteryMonitor.getVoltage(); // In volts
myNex.writeStr("BattV.txt", String(batteryVoltage, 1));
// **RPM Calculation**
if (currentMillis - previousMillis >= pulseInterval) {
previousMillis = currentMillis;
rpm = pulseCount * 600;
pulseCount = 0;
}
int needlePosition = map(rpm, 0, 4000, 310, 230);
myNex.writeStr("RPM.txt", String(rpm));
myNex.writeNum("Needle.val", needlePosition);
if ((millis() - refresh_timer) > REFRESH_TIME) { //IMPORTANT do not have serial print commands in the loop without a delay
// or an if statement with a timer condition like this one.
// Update display elements
myNex.writeNum("TempGauge.val", scaledTemp);
myNex.writeNum("FuelGauge.val", fuelPercent);
myNex.writeNum("Needle.val", needlePosition);
refresh_timer = millis(); // Set the timer equal to millis, create a time stamp to start over the "delay"
}
// **Serial Debug Output**
Serial.print("Fuel Level: ");
Serial.println(fuelPercent);
Serial.print("Battery Voltage: ");
Serial.println(batteryVoltage);
Serial.print("RPM: ");
Serial.println(rpm);
Serial.print("Engine Temp: ");
Serial.println(engineTemp);
Serial.print("Inside Cab Temp: ");
Serial.println(insideTemp);
Serial.print("Outside Temp: ");
Serial.println(outsideTemp);
delay(100);
}
Any help would be appreciated...
Cheers,



