Hey Guys
I am on a project for myselft just4fun.
I am using a Nextion display and an ESP 32 board.
My simple project so far. I have an "UP" button where i can control the brightness of a LED.
Till now (imo) it works quiet well.
The brightness of the LED stand symbolic for the power of a motor. In the future i will try to control a motor with the code
Here is the code:
/******************************************
****************************************/
//Librarys
#include <Nextion.h>
#include <iostream>
using namespace std;
//Fading LED
int freq = 5000;
int ledChannel = 0;
int resolution = 8;
// LED Pins
int Dosierungs_LED = 25;
//Display
HardwareSerial Serial2(2);
void t0PopCallback(void *ptr);
void b1PopCallback(void *ptr);
void b2PopCallback(void *ptr);
NexText t0 = NexText(2, 5, "t0");
NexButton b1 = NexButton(2, 2, "b1");
NexButton b2 = NexButton(2, 3, "b2");
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&b1, //Up
&b2, //Down
&t0,
NULL
};
void t0PopCallback(void *ptr)
{
t0.setText("50");
}
void b1PopCallback(void *ptr)
{
uint16_t len;
double number2;
uint16_t number;
memset(buffer, 0, sizeof(buffer));
t0.getText(buffer, sizeof(buffer));
number = atoi(buffer);
number2 = atof(buffer);
if ( number >= 1 && number <100)
{
number += 1;
memset(buffer, 0, sizeof(buffer));
itoa(number, buffer, 10);
t0.setText(buffer);
}
else if ( number2 >=0.0 && number2 <=0.98)
{
number2 += 0.01;
writeFloatToDisplay(t0, number2);
}
else if (number2 >=0.99 && number2< 1.00)
{
t0.setText("1");
}
}
void setup(void)
{
//Serial COM
Serial.begin(115200);
Serial2.begin(9600);
Serial.println("Monitoring interrupts: ");
// LED - Pins
pinMode(Dosierungs_LED, OUTPUT);
nexInit();
t0.attachPop(t0PopCallback);
b1.attachPop(b1PopCallback);
//PWM für ESP32
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(25, ledChannel);
}
void loop(void)
{
nexLoop(nex_listen_list);
double value = map(pumpLeistung(), 0, 100, 0, 255);
ledcWrite(ledChannel, value);
delay(100);
}
//----------------------------------------------------------------
//------------------------ FUNCTIONS------------------------------
//----------------------------------------------------------------
float writeFloatToDisplay(NexText target, double value)
{ char buffer[10];
dtostrf(value, 5, 2, buffer);
target.setText(buffer);
}
double pumpLeistung()
{
double value;
memset(buffer, 0, sizeof(buffer));
t0.getText(buffer, sizeof(buffer));
value = atof(buffer);
return value;
}
My questions now: 1) Is it necessary to have ledcWrite-function in the loop? I somehow have the feeling it costs me much memorypower of my board. Because from my point of view it now looks every 100ms for a value and sends it to the LED. Is there a more efficiency way?
- Problem. When i upload my code to my esp32. the up button works fine. the value of the field "t0" changes and with the UP-button and so the brightness of the LED. BUT. When i unplug my ESP32 from my computer and replug it back. The button does not work and in the field "t0" on my nextion, nothing is there. To solve it. I must reupload the code to my ESP32. Someone has an idea?
Thank u very much
Best regards