I already wrote that I used the Serial.
Right. Then I don't think you're having a serial problem.
I would suggest starting with a simpler program, maybe just one button.
Check return values to make sure each call is successful. For example, you don't check the return value for nexInit().
After looking at loop, I think you need to set page to -1 (or some invalid value) at the end of loop:
int currentPage = -1;
void loop()
{
weight = Get_Weight(); // shouldn't do this every iteration!
nexLoop(nex_listen_list);
if (page != -1)
currentPage = page; // remember what is displayed
if (page == 0)
{
if (pomp_state)
bt00.setValue(pomp_state);
}
if (page == 1)
{
.
.
.
page = -1;
}
Otherwise, loop will perform the "if (page == n)" as fast as the Arduino can do it, over and over. This could be the real problem.
I would also suggest a switch statement instead of separate if statements:
void loop()
{
weight = Get_Weight(); // shouldn't do this every iteration!
nexLoop(nex_listen_list);
if (page != -1)
currentPage = page; // remember what is displayed
switch (page) {
case 0:
if (pomp_state)
bt00.setValue(pomp_state);
break;
case 1:
memset(buffer, 0, sizeof(buffer));
itoa(readTemperature(SensorWater), buffer, 10);
t11.setText(buffer);
.
.
.
break;
default:
break; // do nothing for invalid page numbers, like -1
}
page = -1;
}
And you should call GetWeight only at certain times, not every loop iteration:
uint32_t weighed_time = 500UL; // Take the first weight measurement @ 1.5s after reset
uint32_t temp_time = 0UL; // Take the first temperature @ 5s after reset
void loop()
{
// Check weight once a second
uint32_t current_ms = millis();
if (current_ms - weighed_time > 1000UL) {
weighed_time += 1000UL;
weight = Get_Weight();
// Update the text elements
memset(buffer, 0, sizeof(buffer));
itoa(weight, buffer, 10);
t15.setText(buffer);
t41.setText(buffer);
}
nexLoop(nex_listen_list);
switch (page) {
case 0:
Robin2 suggested this in the very first reply.
Also note that the text elements are updated only when the weight is measured. I assume the Nextion display will redraw them if they are visible. I don't think you need to set them again in loop.
You should use this same approach in loop for all the measurements that are displayed:
- After some interval ("now" - last time), take a measurement
- Remember "now" as the last time
- Take a measurement
- Update the Nextion elements for that measurement
Do the same thing for buttons that change a displayed value:
void b31PopCallback(void *ptr)
{
if (act_speed >= 10) {
act_speed--;
// Update the text elements
memset(buffer, 0, sizeof(buffer));
itoa(act_speed, buffer, 10);
t14.setText(buffer);
t30.setText(buffer);
}
}
...and for the pump state:
void bt00PopCallback(void *ptr)
{
NexDSButton *btn = (NexDSButton *)ptr;
bt00.getValue(&pomp_state);
if(pomp_state)
{
//запуск помпы
analogWrite(ENA, 255);
}
else
{
analogWrite(ENA, 0);
}
}
If those pages are visible, I think the text elements will redraw. I wonder if you can use t14 in both pages, and delete t30?
By the time you make all these changes, to each measurement and value, you may not need a switch statement in loop! All the work can probably be done in the callbacks or in the timed measurements in loop.
Cheers,
/dev