Volkswagen t4 dashboard nextion arduino

Good morning;

I am showing you my project, a dashboard with 4 switches on the nextion which controls 4 WPM400 relays, A DS18B20 temperature probe
.And a charge controller.
I'm very new to coding, I used 2 existing examples that I gathered but here I'm really stuck.
-The 4 switches for the 4 relays ok
-the temperature probe for graphing works but I cannot display the temperature digitally.
-the voltmeter in progress everything is in time

thanks a lot for your help

#include "EasyNextionLibrary.h"
#define baudrate 9600
EasyNex myNex(Serial);
//Variables
uint16_t analog;
uint16_t voltage;
int lectura_pot;
bool ledstate;
const int REFRESH_TIME = 100;
unsigned long refresh_timer = millis();
//
void setup() {
	myNex.begin(9600);
	pinMode(A0, INPUT);
	pinMode(A1, INPUT);
	pinMode(6, OUTPUT);
	pinMode(7, OUTPUT);
	pinMode(8, OUTPUT);
	pinMode(9, OUTPUT);
}
//
void loop(){
	myNex.NextionListen(); // WARNING: This function must be called repeatedly to response touch events
	// from Nextion touch panel. Actually, you should place it in your loop function.
	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.

		
		int tempData = analogRead(A1);  // Read the analog pin
		voltage = map(tempData, 0, 1024, 0, 5000); // same like: voltage = analogRead(A0)*5000/1024
    
		
		/* We Re-map the value of tempData from 0-1024 (steps) to 0-5000 millivolt
		* connect the pins of a Potentiometer on A0 pin, 5v (5000 millivolt) and GND. Outer pins to 5v and GND, middle pin to A0
		* https://www.arduino.cc/en/tutorial/potentiometer
		* Turn it over and read values from 0 to 5000 millivolts
		*/
		
		myNex.writeNum("Nvoltage.val", voltage); // Nvoltage.val is a variable that we have create on Nextion.
		// we send the value of the voltage variable on Arduino
		// you can use the same name for variables on Nextion for easy recognition with a capital N in front
		// Avoid the use of big variable names as every character is one byte to serial.
		// In here we use big names for the sake of example.
		refresh_timer = millis();  // Set the timer equal to millis, create a time stamp to start over the "delay"
	}
}

void trigger1(){
	// To call this void send from Nextion's component's Event:  printh 23 02 54 01
	// In this exmaple, we send this command from the Release Event of b1 button (see the HMI of this example)
	// You can send  the same `printh` command, to call the same function, from more than one component, depending on your needs
	
	digitalWrite(6, !digitalRead(6)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(6) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(7) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger2(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(7, !digitalRead(7)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(7) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(7) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger3(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(8, !digitalRead(8)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(8) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(8) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger4(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(9, !digitalRead(9)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(9) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(9) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}

NEXTION

n0.val=Nvoltage.val
sys0=Nvoltage.val*100/5000
sys1=Nvoltage.val*100/400
j0.val=120-sys0
n1.val=120-sys0
if(n0.val<730)
{
  j0.pco2=RED
}else
{
  j0.pco2=GREEN
}
if(n0.val>1650)
{
  j0.pco2=GRAY
}

as a minimum you should provide the exact type of Nextion-display that you are using and the nextion-projectfile from the nextion editor

Please explain what this means.

Are you saying that this does not write to the number field on the page?
myNex.writeNum("Nvoltage.val", voltage);

The best way for us to see the HMI file is for you to make a zip file from your HMI file and attach it to the post.

t4.zip (1,1 Mo)

Hello, sorry, I haven't had much time at the moment. really thank you for your help I would like to be able to finish this project.

hello it's a NEXTION NX4827P043_11 screen thank you.

myNex.writeNum("Nvoltage.val", voltage);

I can see the n0.val change with Nvoltage.val if I set Nvoltage vscope global

You can make the analog reading more stable by throwing away the first reading and averaging the next two

analogRead(A1);  //read the throw away first value
int tempData = (analogRead(A1) + analogRead(A1)) / 2;   

ok here I understand well for arduino writing

#include "EasyNextionLibrary.h"
#define baudrate 9600
EasyNex myNex(Serial);
//Variables
uint16_t analog;
uint16_t voltage;
int lectura_pot;
bool ledstate;
const int REFRESH_TIME = 100;
unsigned long refresh_timer = millis();
//
void setup() {
	myNex.begin(9600);
	pinMode(A0, INPUT);
	pinMode(A1, INPUT);
	pinMode(6, OUTPUT);
	pinMode(7, OUTPUT);
	pinMode(8, OUTPUT);
	pinMode(9, OUTPUT);
}
//
void loop(){
	myNex.NextionListen(); // WARNING: This function must be called repeatedly to response touch events
	// from Nextion touch panel. Actually, you should place it in your loop function.
	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.

		
		analogRead(A1);  //read the throw away first value
int tempData = (analogRead(A1) + analogRead(A1)) / 2;   
    
    
		
		/* We Re-map the value of tempData from 0-1024 (steps) to 0-5000 millivolt
		* connect the pins of a Potentiometer on A0 pin, 5v (5000 millivolt) and GND. Outer pins to 5v and GND, middle pin to A0
		* https://www.arduino.cc/en/tutorial/potentiometer
		* Turn it over and read values from 0 to 5000 millivolts
		*/
		
		myNex.writeNum("Nvoltage.val", voltage); // Nvoltage.val is a variable that we have create on Nextion.
		// we send the value of the voltage variable on Arduino
		// you can use the same name for variables on Nextion for easy recognition with a capital N in front
		// Avoid the use of big variable names as every character is one byte to serial.
		// In here we use big names for the sake of example.
		refresh_timer = millis();  // Set the timer equal to millis, create a time stamp to start over the "delay"
	}
}

void trigger1(){
	// To call this void send from Nextion's component's Event:  printh 23 02 54 01
	// In this exmaple, we send this command from the Release Event of b1 button (see the HMI of this example)
	// You can send  the same `printh` command, to call the same function, from more than one component, depending on your needs
	
	digitalWrite(6, !digitalRead(6)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(6) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(7) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger2(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(7, !digitalRead(7)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(7) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(7) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger3(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(8, !digitalRead(8)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(8) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(8) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}
void trigger4(){
	/* Create a button on Nextion
	* Write in the Touch Release Event of the button
	* this command:    printh 23 02 54 00
	* Every time the button is pressed, the trigger0() function will run
	* and the code inside will be executed once
	*/
	digitalWrite(9, !digitalRead(9)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
	if(digitalRead(9) == HIGH){
		myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
		myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
		
		}else if(digitalRead(9) == LOW){
		myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
		myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
	}
}

and for the nextion code

Nvoltage.val if
sys0=Nvoltage.val*100/350
sys1=Nvoltage.val*100/350
j0.val=120-sys0
n1.val=120-sys0
if(n0.val<730)
{
  j0.pco2=RED
}else
{
  j0.pco2=GREEN
}
if(n0.val>1650)
{
  j0.pco2=GRAY
}

but it doesn't work, there's something I don't quite understand. thank you for the time you spend for me thank you very much

This was your original Nextion code. Why did you change it?

n0.val=Nvoltage.val
sys0=Nvoltage.val*100/5000
sys1=Nvoltage.val*100/400
j0.val=120-sys0
n1.val=120-sys0
if(n0.val<730)
{
j0.pco2=RED
}else
{
 j0.pco2=GREEN
}
if(n0.val>1650)
{
 j0.pco2=GRAY
}

ha sorry I misunderstood. I don't have to change my nextion code, is it good? But it doesn't work :sweat_smile:

You do have to change the Nextion code as I indicated in post #7.

set Nvoltage vscope global

ah ok I understand, I'm going to look like a loser but I'm starting out, how do we do it? sorry and thanks for the help

Did you write the Nextion code using the Nextion Editor?

Capture-Nvoltage

If you did not write the code, where does it come from?

ha yes ok great thank you I just tested email my value changes no more

I copy paste several projects and I try to get what I'm looking for but it's not that simple

I'm trying to learn by myself

I don't understand what you are saying. With the change in the HMI file on the Nextion to change the variable scope for Nvoltage to global, does the program work to show changes on the screen with this line in the program

myNex.writeNum("Nvoltage.val", voltage);

yes with set line of code myNex.writeNum("Nvoltage.val", voltage);

it works but not precise,
maybe the problem comes from part of the code voltage = map(tempData, 0, 1024, 0, 5000); ?
The main problem is the values ​​which do not correspond to the real temperature

Your code is written for something like a TMP36 temperature sensor but originally you mentioned a DS18B20.

Which sensor are you using?

it works but not precise,
The main problem is the values ​​which do not correspond to the real temperature

What temperature values are you seeing and what do you think they should be?

Temperature reading is a totally different issue than the writing of the value to the Nextion. You should be able to work out the Arduino side without the Nextion, and use the Serial monitor to help debug your code.

I use a VMA320 with a probe taken from a KOSO box. I may have found a code that will correspond to my probe, it's not easy when you're starting out

#include <math.h>

double Thermistor(int RawADC) {
 double Temp;
 Temp =log(10000.0/(1024.0/RawADC-1)); // for pull-up configuration
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
 return Temp;
}

void setup() {
 Serial.begin(9600);
}

void loop() {
 Serial.println(int(Thermistor(analogRead(A0))));
 delay(1000);
}

Your code looks like a reasonable version of a Steinhart-Hart equation for reading a thermistor. If the temperature readings agree with an a second thermometer your looking good.