Hello people!
I am using a Mega 2560 Arduino. It is powered with a regulated 5V power supply and not via USB.
I have a Nextion display with this page:

I'm using the EasyNextionLibrary for Nextion and have set up the buttons with triggers.
It's basically a PID controller, PV (n1 field in Red) displays value in real time of a temperature sensor.
SP (n3 field in Green) is the desired set temperature that turns ON or OFF a heater.
The EEPROM only get updated if the b21 SET button is pressed.
When you touch the n3 field, a numeric keypad opens where you enter your desired set value temperature. Now, I want this n3 value to be stored on the EEPROM so that it is saved and displayed on this field on boot-up.
I have the following sketch that stores the n3 value on the EEPROM, this work as I can serial.print this value to confirm that it has been saved.
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary
#include <EEPROM.h>
#include <Thermocouple.h>
#include <MAX6675_Thermocouple.h>
#include <SmoothThermocouple.h>
#define SCK_PIN 3
#define CS_PIN 4
#define SO_PIN 5
/**
Smoothing factor of a temperature value.
*/
#define SMOOTHING_FACTOR 5
Thermocouple* thermocouple = NULL;
EasyNex myNex(Serial1); // Create an object of EasyNex class with the name < myNex >
// Set as parameter the Hardware Serial you are going to use
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int address16 = 16;
byte value16; // Value stored for PID variable
void setup() {
myNex.begin(115200); // Nextion Display begin the object with a baud rate of 115200
Serial.begin(115200); // Use to see commands on the Serial Monitor
Thermocouple* originThermocouple = new MAX6675_Thermocouple(SCK_PIN, CS_PIN, SO_PIN);
thermocouple = new SmoothThermocouple(originThermocouple, SMOOTHING_FACTOR);
value16 = EEPROM.read(address16);
int PID = value16;
myNex.writeNum("n3.val", PID);
}
void loop() {
myNex.NextionListen(); // This function must be called repeatedly to response touch events
delay(300);
// Reads temperature
const double celsius = thermocouple->readCelsius();
// Output Temperature on Nextion n1 text field
myNex.writeNum("n1.val", celsius);
Serial.println(value16); // Diplays EEPROM value16
}
// PID Buttons Page 1
void trigger10() {
/* Create a button on Nextion
* Write in the Touch Release Event of the button
* this command: printh 23 02 54 0A
* Every time the button is pressed, the trigger10() function will run
* and the code inside will be executed once
*/
int PID; // What is stored on Value16 and displayed on n3
PID = myNex.readNumber("n3.val"); // Store to PIDTemp the value of text box n3;
int val16 = PID;
EEPROM.update(address16, val16);
delay(10);
value16 = EEPROM.read(address16);
PID = value16;
myNex.writeNum("n3.val", PID);
}
void trigger11() {
/* Create a button on Nextion
* Write in the Touch Release Event of the button
* this command: printh 23 02 54 0B
* Every time the button is pressed, the trigger11() function will run
* and the code inside will be executed once
*/
Serial.println("Clear button pressed");
}
void trigger12() {
/* Create a button on Nextion
* Write in the Touch Release Event of the button
* this command: printh 23 02 54 0C
* Every time the button is pressed, the trigger12() function will run
* and the code inside will be executed once
*/
Serial.println("Down button pressed");
//myNex.writeNum("n3.val") +1;
}
void trigger13() {
/* Create a button on Nextion
* Write in the Touch Release Event of the button
* this command: printh 23 02 54 0D
* Every time the button is pressed, the trigger13() function will run
* and the code inside will be executed once
*/
Serial.println("Up button pressed");
//myNex.writeNum("n3.val") -1;
}
The problem is that if I cut the 5V power and turn power back on, the saved value is only redisplayed on the n3 field when I plug back the USB while Arduino IDE with the sketch is open.
Can anyone help me fix this so that the EEPROM value gets display on the n3 field after boot-up?
Also, I'd like the UP button (b24) to increase the value that is displayed on n3 field by 1 every time it is triggered (void trigger13) and the DOWN button (b23) to decrease the value that is displayed on n3 field by 1 every time it is triggered (void trigger12).
Cheers,