Converting String from Nextion to Intiger to use for math

Hello All,
The below code compiles fine, but does not move my stepper at all and causes the other triggers to not work. However commenting out the three line between the //**********'s namely the lines:

strDist = Nex.readStr("t1.txt");
intDist = strDist.toInt();
Steps = intDist * SPI;

Then all is well, and triggers 0, 2, 3 and 4 work just fine.
Is there something that I'm missing? or is there a better way to go about this?
I realize that this is probably more of a Nextion question, so anyone with knowledge of Nextion's please jump in.
BTW, I'm using the Nextion NX8048P070_011 7" display if that matters.

TIA,
Muggs

#include <EasyNextionLibrary.h>

EasyNex Nex(Serial);

String data_from_display = "";

// Defin pins

int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;      // PUL- pin
int driverDIR = 6;      // DIR- pin

// Variables

int i;                  // Integer for Counter
int pd = 300;           // Pulse Delay period
int SPI = 4072;         // StepsPerInch
String strDist;         // Distance as String from Nextion
int intDist;            // Distance converted to Intiger
long Steps;             // Steps eqal to Dist*SPI
boolean setdirW = LOW;  // Set Direction

// Trigger designations
// trigger0 = Reverse Button
// trigger1 = ENTER
// trigger2 = 0.50" Width
// trigger3 = 1.10" Height
// trigger4 = 0.11"


void setup() {

  Nex.begin(9600);

  pinMode(driverPUL, OUTPUT);
  pinMode(driverDIR, OUTPUT);
  Nex.writeStr("t0.txt", "JFD V0.2");
}

void loop() {
  Nex.NextionListen();
  digitalWrite(driverDIR, setdirW);
  //************
  strDist = Nex.readStr("t1.txt");
  intDist = strDist.toInt();
  Steps = intDist * SPI;
  //************
}
void trigger0() {
  setdirW = !setdirW;
}
void trigger1() {

  for (i = 1; i <= Steps; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger2() {

  for (i = 1; i <= 2036; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger3() {

  for (i = 1; i <= 4479; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger4() {

  for (i = 1; i <= 448; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
  intDist = strDist.toInt();

This line of code puts the integer value of the strDist String into the integer variable intDist

You could do the same with any other String variables that you need as integers

UKHeliBob, Thank you.
Without trying to sound rude, I realize what that line in the code does and have since changed to a float variable. My problem is that the line

strWfromNex = Nex.readStr("t1.txt");

Stops all other triggers from working. Revised code below.
With that line commented out all triggers work fine, but when I un-comment that line, the Arduino refuses to take any other commands from the display.

Thank you again for taking the time to read this. I'm hoping for someone with some knowledge of the EasyNextionLibrary to jump in.

TIA,
Muggs

/*

  Uses DM542T or similar Stepper Driver Unit
  Nextion Intelligent 7.0 inch NX8048P070-011C Capacitive Touch Screen 
  Muggs - 20220216
*/

#include <EasyNextionLibrary.h>

EasyNex Nex(Serial);

// Defin pins

int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;      // PUL- pin
int driverDIR = 6;      // DIR- pin

// Variables

int i;                            // Integer for Counter
int pd = 300;                     // Pulse Delay period
int SPI = 4072;                   // StepsPerInch was caculated to be 4072
String strWfromNex;               // Distance as String from Nextion
String bufferWidth;               // Buffer to remove "."
float Wsteps;                     // Wsteps eqal the Width distance from Display *SPI
boolean setdirW = LOW;            // Set Width Direction

// Trigger designations
// trigger0 = Reverse Direction
// trigger1 = 1.000" - TEMP move test setup
// trigger2 = 0.375" - TEMP move test setup
// trigger3 = 0.250" - TEMP move test setup
// trigger4 = 1.000" - TEMP move test setup


void setup() {

  Nex.begin(9600);

  pinMode(driverPUL, OUTPUT);
  pinMode(driverDIR, OUTPUT);
  Nex.writeStr("t0.txt", "JFD V1.2");
}
void loop() {

  Nex.NextionListen();
  digitalWrite(driverDIR, setdirW);
}
void trigger0() {
  setdirW = !setdirW;
}
void trigger1() {

  for (i = 1; i <= 4072; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger2() {

  for (i = 1; i <= 1527; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger3() {

  for (i = 1; i <= .25 * SPI; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger4() {

  for (i = 1; i <= SPI; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
void trigger5() {
  // strWfromNex = Nex.readStr("t1.txt");
  bufferWidth = parseData(strWfromNex, '.');
  Wsteps = bufferWidth.toFloat()/1000 * SPI;

  for (i = 1; i <= Wsteps; i++) {
    digitalWrite(driverPUL, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL, LOW);
    delayMicroseconds(pd);
  }
}
String parseData(String data, char delimiter) {
  int8_t index = 0, indexPast = 0;
  String resultData;
  index = data.indexOf(delimiter);
  data.remove(index, 1);
  resultData = data;
  return resultData;
}

intDist = strDist.toInt();

I am glad to hear that you know what the line of code does it seemed from the title that you gave this topic that you didn't, hence my reply

Yeah, probably could have titled that better! :wink:

@PerryBebbington can you see what might be wrong ?

@UKHeliBob
Thank you.

@Muggs
I believe you are using @Seithan's Easy Nextion Library, he is the best person to help you with that, not me.

There are 2 ways of dealing with a Nextion that are supported on this forum:
My methods are set out in Using Nextion displays with Arduino, which I will help you with if you use them.
@Seithan has his own methods, which I guess you have found, he helps with those.

Good luck with your project.

@UKHeliBob & @PerryBebbington

OK, I'm almost too embarrassed to say why, but I have the code working perfectly (for now).

I have been concentrating so hard, and focusing on one axis (Width) for now that I completely forgot I put this line in the Nextion HMI:

if(t2.txt!=""&&t1.txt!="") // Don't send unless both Width & Height parameters are present
{
  printh 23 02 54 05
}

Please someone tell me other people have done this!!
Thank you both again.

I can tell you've I've made silly, embarrassing mistakes, many, many of them! Don't worry about it, everyone does it!

@PerryBebbington ,

LOL! Thanks, I'll be reading your rather lengthy post with much interest.

Enjoy. Do understand that my approach is very different to the library you are using.

@PerryBebbington Thank you for pointing me to the "Using Nextion displays with Arduino" post, that was very helpful.
I have a new question that maybe you or someone else can help me with:
I want to use print statements to do a little debugging, but because the Nextion is using the Serial port on the Nano I tried to use the AltSoftSerial lib. but the following doesn't work.

/*

  Uses DM542T or similar Stepper Driver Unit
  Nextion Intelligent 7.0 inch NX8048P070-011C Capacitive Touch Screen 
  Muggs - 20220224
*/

#include <EasyNextionLibrary.h>
#include <AltSoftSerial.h>

EasyNex Nex(Serial);
AltSoftSerial altSerial;

// Define pins

int WidthPUL = 5;   // Width PULSE- pin
int WidthDIR = 4;   // Width DIRECTION- pin
int HeightPUL = 7;  // Height PULSE- pin
int HeightDIR = 6;  // Height DIRECTION- pin

// Variables

int i;                  // Integer for Counter
int pd = 300;           // Pulse Delay period
int SPI = 4072;         // StepsPerInch was caculated to be 4072
String strWfromNex;     // Width Distance as String from Nextion
String strHfromNex;     // Height Distance as String from Nextion
String bufferWidth;     // Buffer to remove "." in Width Field
String bufferHeight;    // Buffer to remove "." in Height Field
float Wsteps;           // Wsteps eqal the Width distance from Display * SPI
float Hsteps;           // Hsteps eqal the Height distance from Display * SPI
boolean setdirW = LOW;  // Set Width Direction
boolean setdirH = LOW;  // Set Width Direction

// Trigger designations
// trigger0 = Reverse Direction
// trigger1 = 1.000" - TEMP move test setup
// trigger2 = 0.375" - TEMP move test setup
// trigger3 = 0.250" - TEMP move test setup
// trigger4 = 1.000" - TEMP move test setup


void setup() {

  Nex.begin(9600);
  altSerial.begin(9600);
  Nex.writeStr("t0.txt", "JFD V1.2.2");
  altSerial.print("Serial Com Test!");

  pinMode(WidthPUL, OUTPUT);
  pinMode(WidthDIR, OUTPUT);
  pinMode(HeightPUL, OUTPUT);
  pinMode(HeightDIR, OUTPUT);
}
void loop() {

  Nex.NextionListen();
  digitalWrite(WidthDIR, setdirW);
  digitalWrite(HeightDIR, setdirH);
}

So, how would I go about printing to the serial monitor for debugging?
TIA,
Muggs
BTW, if this should be started as a new topic, please let me know.

Hi @Muggs

I see you are using @Seithan 's Easy Nextion Library, so it's really him you need help from, not me. His methods are quite different to mine.

However, if you are using a Nano, or any Arduino board that does not have a spare serial port, then whatever you send to the Nextion is also going to the serial monitor, so if you put Serial.print(somedata) in your code it will go to the serial monitor. Unfortunately it might make a mess of what goes to the Nextion as well.

I suggest you buy an Arduino with at least 1 spare serial port and use that for the Nextion. My preferred board is the Nano Every, but there are others. The Mega has 3 serial ports in addition to the serial monitor port plus about a zillion input and output pins.