Nano Every with Nextion

Hello,
I'm driving a linear actuator with a Nano Every and a Nerxtion HMI. My issue is that it only runs through the code once, i.e. one extend and one retract. It uses the parameters I choose on the Nextion, so I know it's communicating. This code worked fine with an Uno R4 (with different pins of course). What is wrong with my code?
Thanks!

#include "EasyNextionLibrary.h"  // Include EasyNextionLibrary

int RPWM = 9;  //connect Arduino nano every pin 9 to IBT-2 pin RPWM, this is the inhale pin
int LPWM = 10;  //connect Arduino nano everey pin 10 to IBT-2 pin LPWM, this is the exhale pin
byte Speed = 0;
int pwm1;
bool myend;

EasyNex myNex(Serial1);  // Create an object of EasyNex class with the name < myNex >
                        // Set as parameter the Hardware Serial you are going to use

void setup() {
pinMode(9, OUTPUT); // Configure pin 9 as an Output
pinMode(10, OUTPUT); // Configure pin 10 as an Output
myNex.begin(9600);  // Begin the object with a baud rate of 9600
Speed = 50; // Set the speed low for safety
analogWrite(LPWM, 0); //disable exhale pin
analogWrite(RPWM, Speed); //Pull the actuator back in if it's out at a low speed
}


void loop() {
myNex.NextionListen(); // This function must be called repeatedly to respond to touch events
                       // from Nextion touch panel.
}

void trigger1() { //all math is done on the nextion, based on an actuator speed of 50mm/sec
  float myintimer = myNex.readNumber("n0.val"); //this is the length of time for inspiration
  float myouttimer = myNex.readNumber("n1.val"); //this is the length of time for expiration
  byte speedin = myNex.readNumber("va9.val"); //this is the speed for inspiration, 1-255
  byte speedout = myNex.readNumber("va10.val"); //this is the speed for expiration, 1-255
  speedin = speedin/2; //this is for the faster actuator, delete if using the slower one
  speedout = speedout/2; //this is for the faster actuator, delete if using the slower one
  myend = false;
  while (myend == false){
  myNex.NextionListen();
   
  pwm1 = speedout; //Expiration
  analogWrite(RPWM, 0); //inhale pin disabled
  analogWrite(LPWM, pwm1); //speed of exhale
  delay(myouttimer); //time of exhale

  pwm1 = speedin; //Inspiration
  analogWrite(LPWM, 0); //exhale pin disabled
  analogWrite(RPWM, pwm1); //speed of inhale
  delay(myintimer); //time of inhale
  delay(1250); //Extra timer to make sure the actuator is home
  } 
}

void trigger2()  { //Finish
  myend = true;
  }

If you comment out the trigger2 function, does it repeat the moves?

What event is calling trigger1() and trigger2().
Button presses and releases?
Is trigger2 a release event on the button for trigger1 or is it a separate button?

This code worked fine with an Uno R4

Do you still have this unit, and confirm that the code indeed still works as expected? The difference for the Nano Every does not make sense to me.

If I comment out the trigger2 function it still only does one cycle. It is button presses and releases calling trigger1() and trigger2(). They are separate buttons on the Nextion. I will check with the Uno this afternoon. Thanks!

I think this leaves out any Nextion issues.

You might be better able to debug what you have with just the Nano Every.

I tried it with the Uno, and it worked fine. I did discover that if I reset the nano, I can drive another cycle. Also if I wait long enough, it does cycle again, but it takes about 15 minutes. I think it must be the way I'm using delay().

  float myintimer = myNex.readNumber("n0.val"); //this is the length of time for inspiration
  float myouttimer = myNex.readNumber("n1.val"); //this is the length of time for expiration
  byte speedin = myNex.readNumber("va9.val"); //this is the speed for inspiration, 1-255
  byte speedout = myNex.readNumber("va10.val"); //this is the speed for expiration, 1-255
delay(myouttimer); //time of exhale
delay(myintimer); //time of inhale

You need to print out what you are reading from the Nextion. The default value for a bad reading is 777777 which will look like 13 minutes with delay(777777);

Thanks so much for the tip, it was reading a bad number. However I discovered that whatever the number is, it's reading a bad number if it's the first one it reads. If I read the first number twice:

myintimer = myNex.readNumber("n0.val");
myintimer = myNex.readNumber("n0.val");

it works fine. Why would this be?

it was reading a bad number. However I discovered that whatever the number is, it's reading a bad number if it's the first one it reads.

Good progress.

  float myintimer = myNex.readNumber("n0.val"); //this is the length of time for inspiration
  float myouttimer = myNex.readNumber("n1.val"); //this is the length of time for expiration
  byte speedin = myNex.readNumber("va9.val"); //this is the speed for inspiration, 1-255
  byte speedout = myNex.readNumber("va10.val");

myintimer and myouttimer should be unsigned long integers as they are used as delay values.

Does the bad first reading problem occur independent of what is read first?

If you run the EasyNextion Library example ReadAndWriteNumberCode,ino and the associated .HMI file do you see the same issue, or do you get more reliable reads?

You may need to put some error checking in your code, and test for reasonable values. What are the reasonable limits for your delays? If you can't get any Serial output into your code for error checking, you may need to have an error indicator on the Nextion screen which you write to after a read.

Does the code on the Uno R4, have the same issue?
What model Nextion are you using?

I think it will be helpful at this point if I could see your HMI file. Can you please make a .zip of the file and attach it to your post.

I caught that and changed them yesterday.

Yes.

No.

NX8048P070-011C-Y

I'll zip the HMI file and post it this afternoon.
Thanks!

There have been two postings in the past year on the Arduino forum about serial output issues with intelligent series displays. My conclusion is that there is some sort of hardware issue with that model.

In a report on GitHub, one person solved the issue by modifying the time out in the library

I managed to fix the issue by changing one of the source files, EasyNextionLibrary.cpp. In NextionListen function I changed the waiting time from 100UL to 10UL.

EDIT: Is the UnoR4 and 3.3v device, and the Intelligent Series at 5v Serial? How is the R4 connect to the Nextion? The Nano Every should be a good fit.

Linear actuator snorebuds w revised math 2 032525.zip (791.9 KB)

And here is the code that works:

#include "EasyNextionLibrary.h"  // Include EasyNextionLibrary

int RPWM = 9;  //connect Arduino nano every pin 9 to IBT-2 pin RPWM, this is the inhale pin
int LPWM = 10;  //connect Arduino nano everey pin 10 to IBT-2 pin LPWM, this is the exhale pin
byte Speed = 0;
int pwm1;
int pwm2;
bool myend;
unsigned long myintimer;
unsigned long myouttimer;

EasyNex myNex(Serial1);  // Create an object of EasyNex class with the name < myNex >
                        // Set as parameter the Hardware Serial you are going to use

void setup() {
pinMode(9, OUTPUT); // Configure pin 9 as an Output
pinMode(10, OUTPUT); // Configure pin 10 as an Output
myNex.begin(9600);  // Begin the object with a baud rate of 9600
Speed = 50; // Set the speed low for safety
analogWrite(LPWM, 0); //disable exhale pin
analogWrite(RPWM, Speed); //Pull the actuator back in if it's out at a low speed
}


void loop() {
myNex.NextionListen(); // This function must be called repeatedly to respond to touch events
                       // from Nextion touch panel.
}

void trigger1() { //all math is done on the nextion, based on an actuator speed of 50mm/sec
  
  myintimer = myNex.readNumber("n0.val"); //this is the length of time for inspiration
  myouttimer = myNex.readNumber("n1.val"); //this is the length of time for expiration
  myintimer = myNex.readNumber("n0.val"); //this is the length of time for inspiration
  byte speedin = myNex.readNumber("va9.val"); //this is the speed for inspiration, 1-255
  byte speedout = myNex.readNumber("va10.val"); //this is the speed for expiration, 1-255
  speedin = speedin/2; //this is for the faster actuator, delete if using the slower one
  speedout = speedout/2; //this is for the faster actuator, delete if using the slower one
  myend = false;
  while (myend == false){
   myNex.NextionListen();
   
  pwm1 = speedout; //Expiration
  analogWrite(RPWM, 0); //inhale pin disabled
  analogWrite(LPWM, pwm1); //speed of exhale
  delay(myouttimer); //time of exhale

  pwm2 = speedin; //Inspiration
  analogWrite(LPWM, 0); //exhale pin disabled
  analogWrite(RPWM, pwm2); //speed of inhale
  delay(myintimer); //time of inhale
  delay(2000); //Extra timer to make sure the actuator is home

  } 
}

void trigger2()  { //Finish
  myend = true;
  }

I can't see anything which would explain what you are seeing with a review of the HMI file.

That said, if you don't have a trigger0() function I would remove that call from the start and stop button press events.

I am at a loss explain why you need the double reading with the Nano Every but not the Uno R4.

I am still somewhat suspicious of the Intelligent Series Nextion.

I have used a Nano Every with a Discovery series Nextion is many projects, and I have never encountered this double reading issue.

I wonder what happens if you put a delay(50) as the first line of the trigger1 function does the code still need the dual read?

Did you try the example program where the reading is not in a trigger function. Does it require the double reading?

If you write a simple test program without the sounds and sliders but just try and read a single number with a trigger function do you need the double reading?

I wrote a simple program with one button and two number boxes and read the first into the second. I did not need the double read. I tried commenting out the bitmap and the wave on the original HMI program, but it still required the double read. Interesting.

Yes.

Great. Now time to start putting stuff back in and see what breaks the single reading.

@vlambert2

Hi
I had a quick look at this in Nextion Editor Debug, connected to an UnoR3 running your sketch.
Here's what I found...


Notice something missing there?
Over to you.

Edit: And I don't rate the EasyNextion library either, poor handling of this kind of error.

I'm not certain about the button images and the 04 FF FF FF sent as a press event. I don't know what the EasyNextion Library does with this.

When I make the start and stop standard Nextion editor buttons without images, I don't see this 04 FF FF FF and can get clean trigger function code being sent with the release event.

As you start putting stuff back to eliminate the double reading, I would see what you get with your full current program but without the image buttons.

That was it! Something about the way I was changing the button images. When I stopped doing that, it runs fine. Thanks for all your help!

Do you have any thoughts as to why the code with the changing button image and the extraneous 04 FF FF FF was working properly on the Uno R4?