reading Nextion Objects IDs and (Eventually) using them to stop a while loop

Hello,

I'm currently working on a project which involves an Arduino Mega, a Nextion Display, and Stepper Motors. I need to "stop" the movement of the stepper motors when I press the corresponding stop button on the Nextion, however, the "movement code" is found inside a while loop, with itself being found inside a Nextion touch push event ("Stop()" Function). I tried using a "dummy" variable in Arduino but it didn't seem to work. The same goes with a variable from the Nextion. Thus, I thought of using the components' IDs as conditions to the loop (?) in order to stop it. But I don't seem to get any reading from both the Serial Monitor (apart from the debug messages which don't even get read by the "Serial.read()") and the serial port in general. If anyone has a better idea, I would more than happy to try it out. Any criticism is appreciated.

This would be the main code :

#include <AccelStepper.h>
AccelStepper stepper (AccelStepper::DRIVER, 4, 5);
#define PUL 4
#define DIR 5 
#define ENA 6 
#define FC_Zero 8 
#define FC_End 9 

#include <Nextion.h>

NexButton GO_PB   = NexButton (0, 8, "GO");
NexButton MAN_PB  = NexButton (0, 9, "MANPb");
NexButton AUTO_PB = NexButton (0, 10, "AUTOPb");
NexButton UP_PB   = NexButton (0, 11, "UPPb");
NexButton DOWN_PB = NexButton (0, 12, "DOWNPb");
NexButton STOP_DOWN_PB = NexButton (0, 14, "STOPDOWNPb");
NexText ACTION = NexText (0, 15, "ACTION");

NexTouch *nex_listen_list[] = { 
  &GO_PB, &MAN_PB, &AUTO_PB, &UP_PB, &DOWN_PB, &STOP_DOWN_PB, NULL
};

int Fase_Zero = 0;
int End_Stroke = 0;
int SMS_Zero = 25;
int SSP_Zero = 25;
int SMS = 100;
int SSP = 100;
int Set = 0;
int Reset = 0;
long Increase = 1; 
long Decrease = 1; 
unsigned long time;
unsigned long t1, dt;
char Action[] = "PRONTO !";

// END declaration

// ----------------------------------------
void setup() {


  Serial.begin(9600);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(PUL, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(FC_Zero, INPUT_PULLUP);
  pinMode(FC_End, INPUT_PULLUP);

  digitalWrite(ENA, LOW); 
  stepper.setCurrentPosition(0);  
  nexInit(); 
  GO_PB.attachPop(Go, &GO_PB);
  MAN_PB.attachPop(Man, &MAN_PB);
  AUTO_PB.attachPop(Auto, &AUTO_PB);
  UP_PB.attachPush(Up, &UP_PB);
  DOWN_PB.attachPush(Down, &DOWN_PB);
  STOP_DOWN_PB.attachPush(Stop, &STOP_DOWN_PB);

} 

// ----------------------------------------
void loop() {
 
  nexLoop(nex_listen_list);
} 

// ----------
void Go(void *ptr) { 
  if (Fase_Zero == 0) {
    digitalWrite(13, HIGH);
    stepper.setMaxSpeed(SMS_Zero);
    stepper.setSpeed(SSP_Zero);
    while (digitalRead(FC_Zero))  {
      char Action[] = " To_Zero";
      ACTION.setText (Action);
      digitalWrite(ENA, HIGH); 
      stepper.moveTo(Decrease); 
      Decrease++;
      stepper.run(); 
    }
    digitalWrite(ENA, LOW); 
    stepper.setCurrentPosition(0); 
    digitalWrite(13, LOW);
    End_Stroke = 0;
  }
  else {

  }
  Fase_Zero = 1;
  Set = 1;
  Serial.print ("vis DONE,1");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print ("vis STOPDOWNPb,0");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  char Action[] = " H O M E  !";
  ACTION.setText (Action);
}

// ----------
void Man(void *ptr) { 
  digitalWrite(13, LOW);
  delay (250);
  digitalWrite(13, HIGH);
  if (Fase_Zero == 1) {
    Serial.print ("vis DONE,1");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    char Action[] = " MANUALE";
    ACTION.setText(Action);

  }
}

// ----------
void Auto(void *ptr) { 
  digitalWrite(13, LOW);
  delay (250);
  digitalWrite(13, HIGH);
  if (Fase_Zero == 1) {
    Serial.print ("vis DONE,1");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    char Action [] = " AUTOMATICO";
    ACTION.setText(Action);

  }
}

// ----------
void Up(void *ptr) { 
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);
  if (End_Stroke == 1) {
    char Action[] = " SU'/ UP";
    ACTION.setText(Action);
    stepper.setMaxSpeed(SMS);
    stepper.setSpeed(SSP);
    while (digitalRead(FC_Zero))  {
      digitalWrite(ENA, HIGH); 
      digitalWrite(11, LOW);
      stepper.moveTo(Decrease);  
      Decrease++;
      stepper.run(); 
    }
  }
  digitalWrite(ENA, LOW); 
  stepper.setCurrentPosition(0);  
  End_Stroke = 0;
  char Action [] = " H O M E  !";
  ACTION.setText (Action);
}

// ----------
void Down(void *ptr) {
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  if (End_Stroke == 0) {
    char Action [] = " GIU'/ DOWN";
    ACTION.setText (Action);
    Serial.print ("vis STOPDOWNPb,1");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    stepper.setMaxSpeed(SMS);
    stepper.setSpeed(SSP);
    do {
      digitalWrite(ENA, HIGH); 
      digitalWrite(12, LOW);
      stepper.moveTo(Increase);  
      Increase--;
      stepper.run();  // Go
    } while (digitalRead(FC_End));
  }
  digitalWrite(ENA, LOW); 
  stepper.setCurrentPosition(0);  
  End_Stroke = 1;
  char Action [] = " E N D  !";
  ACTION.setText(Action);
}
// ----------
void Stop(void *ptr) { 
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  char Action[] = " S T O P  !";
  ACTION.setText (Action);
  digitalWrite(ENA, LOW); 
  stepper.setCurrentPosition(0); 
  End_Stroke = 1;
}

And this would be the code with which I'm experimenting (I also tried using the "serialEvent() function") :

#include <Nextion.h>

NexButton On  = NexButton(0,2, "btOn");
NexButton Off = NexButton(0,3, "btOff");


NexTouch *nex_listen_list[] = {
   &On, &Off, NULL
  };
  
String inputString = "";      // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete

void serialEvent2() {
  while (Serial2.available()) {
    // get the new byte:
    char inChar = (char)Serial2.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void setup() {
 Serial2.begin(9600);
 Serial.begin(9600);
 inputString.reserve(200);
 pinMode(13, OUTPUT);
 nexInit();
 On.attachPush(bOn, &On);
 Off.attachPush(bOff, &Off);
}

void loop() {
  nexLoop(nex_listen_list);
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
      
}
void bOn(void *ptr) {
    digitalWrite(13, HIGH);
  }
  
void bOff(void *ptr) {
    digitalWrite(13, LOW);
  }

Hi Rick,
Welcome.

I can't help you with the Nextion official libraries, other than to say lots of people seem to complain about them.

First off have a read of my Nextion tutorial at the top of the displays' section.

Next, you need to learn to write collaborative, non-blocking code. The mere fact of writing:

...the "movement code" is found inside a while loop

Is enough to tell me this is something you are yet to learn. Blocking code, which include while loops, long for loops and delay are death to any responsive program.

Put your project aside and have a read of:
https://forum.arduino.cc/index.php?topic=223286.0
https://forum.arduino.cc/index.php?topic=503368.0
https://forum.arduino.cc/index.php?topic=711767.0

Then go back and start again using non-blocking code. If you get stuck ask for help.

Good luck.

Thank you so much, Perry, for taking the time to help me,

I really appreciate your advice on using a non-blocking code, and I'll make sure to use it.

However, I'm not the only one working on this, and I'm assigned to the Nextion part of the project.

Still, I'll make sure to use your tutorials in the best way possible.

Again, thank you so much for helping me,

~Rick S

This topic was automatically closed after 120 days. New replies are no longer allowed.