CNC Shield v3 + nextion

Mod edit:
Moved to jobs and paid consultancy at the request of the OP.
The OP is looking to pay someone to resolve this.

Hey! I'm new here and just getting into Arduino stuff. Currently, I'm working on a CNC pickup winding machine. I've been following this tutorial, but instead of using a custom PCB, I opted for a CNC shield.

[Link to the tutorial: GitHub - sandy9159/DIY-Arduino-based-Guitar-Pickup-Coil-Winder: This is mini Arduino based machine to wind guitar pickup coil.]

I changed the pin numbers according to the CNC shield, but it's not working at all.

I'm confused about setting up the serial communication. I don't know which serial to use and how to configure it. I've tried mySerial, Serial2, different pin numbers, but it just won't work.

Please help!

If your CNC shield uses an Uno or Nano, your only choice of a serial port is a software serial port. The hardware serial is usually used to talk to the controller firmware (Grbl), to uplaod code and for debug. Use any free digital or analog pins for a software serial port.

The software serial port is limited in speed to sround 38400 baud maximum.

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX
int A = 0;
int B = 0;
int C = 0;
int L = 40;
int state = 0;
int EN = 8;  // Change the stepper enable pin to match CNC Shield
String message;
int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;

#define DIR_X 5  // Change these pin numbers to match CNC Shield
#define STEP_X 2
#define DIR_Y 6
#define STEP_Y 3
#define MICROSTEPS 16
#define MOTOR_STEPS 200

int count = 0;
BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
SyncDriver controller(stepperX, stepperY);

void setup() { 
  pinMode(EN, OUTPUT);
  digitalWrite(EN, HIGH);
  numMessages, endBytes = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  stepperX.begin(250, MICROSTEPS);
  stepperY.begin(250, MICROSTEPS);
  delay(500);

  Serial.print("baud=38400");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.end();

  Serial.begin(38400);

}

void loop() {
  data();
  if (A > 0 && B > 0 && C > 0) {
    digitalWrite(EN, LOW);
    if (count <= C) {
      for (int i = 0; i <= L; i++) {
        if (count > C) {
          break;
        }
        controller.rotate(360, 40);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;  
      }

      for (int i = 0; i <= L; i++) {
        if (count > C) {
          break;
        }
        controller.rotate(360, -40);  
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
      }
    }   
  }
}

void data() {
  if (state == 0) {
    if (numMessages == 1) {
      A = QTY;
      Serial.println(A);
      numMessages = 0;
      state = 1;
    }
  }

  if (state == 1) {
    if (numMessages == 1) {
      B = QTY;
      Serial.println(B);
      numMessages = 0;
      state = 2;
    }
  }

  if (state == 2) {
    if (numMessages == 1) {
      C = QTY;
      Serial.println(C);
      numMessages = 0;
      state = 0;
    }
  }

  if (mySerial.available()) {
    inByte = mySerial.read();

    if (inByte > 47 && inByte < 58) {
      message.concat(char(inByte));
    }
    else if (inByte == 255) {
      endBytes = endBytes + 1;
    }

    if (inByte == 255 && endBytes == 3) {
      QTY = message.toInt();
      message = "";
      endBytes = 0;
      numMessages  = numMessages + 1;
    }
  }
}

This is the sketch that I uploaded to the Arduino. I connected the RX cable to the TX pin and the TX to the RX on the CNC shield. Is that the right way? It still isn't working.

Thank you for helping me.

No it is not the right way. The pins on the CNC shield that are labeled RX and TX are tied to the hardware serial pins (Uno pins 0 and 1). While developing, at least, keep the hardware serial port free for program upload, program output and debugging.

To use the software serial you need to assign the SofttwareSerial (SS) RX and TX pins to unused pins on the CNC shield. Currently the SS RX and TX pins are assigned to pins 2 and 3. Those pins are assigned to X step and Y step on the CNC shield and cannot be used for SS.

What pins are you not using? Are you using the limit switch pins? Are you using the Abort, Feed Hold or any of the analog pins. If you are not using the limit switch or analog pins, you could connect the SS port to 2 of those pins. For instance, connect the Nextion TX pin to the Abort pin on the CNC shield (A0) and the Nextion RX pin to the CNC shield Feed Hold pin (A1). Then change the SoftwareSerial constructor to:

SoftwareSerial mySerial(A0, A1); // RX, TX

The analog input pins are really digital pins with analog input as a special function.

I have changed this line. And connected the
TX - to abort
RX - to Hold

unfortunately the start button still doesn't work

Could there be an issue with the Nextion display configuration?

program.s

//The following code is only run once when power on, and is generally used for global variable definition and power on initialization data
int sys0=0,sys1=0,sys2=0     //At present, the definition of global variable only supports 4-byte signed integer (int), and other types of global quantity declaration are not supported. If you want to use string type, you can use variable control in the page to implement
printh 00 00 00 ff ff ff 88 ff ff ff//Output power on information to serial port
page 0                       //Power on start page 0
bauds=38400

I really know nothing of the Nextion displays. So can't be of much help there.

That should set up the SoftwareSerial port on those pins (A0 & A1). If it does not work, I am at a loss.

What is that? Does the Nextion need 38400 baud? If so change the

mySerial.begin(9600);

to

mySerial.begin(38400);

38400 is about the highest baud rate that SoftwareSerial will reliably run. Use wires as short as possible between the shield and Nextion.

I got it to run for a while, but it was weird. For example, there are three inputs on the display:

  • Wire diameter
  • Coil width
  • Number of turns

Normally, when it runs, it should count down the turns, but it was displaying some random numbers. After a couple of tries, the result ended up running constantly, meaning the countdown zeroed, but the steppers kept going forever.

I unplugged everything, then turned it on again, and it doesn't work anymore.

That is the last code I uploaded to my Arduino Uno (I think it's a replica; does it matter? Maybe I should choose Nano in the IDE?).

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(A0, A1); // RX, TX
int A = 0;
int B = 0;
int C = 0;
int L = 40;
int state = 0;
int EN = 8;  // Change the stepper enable pin to match CNC Shield
String message;
int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;

#define DIR_X 5  // Change these pin numbers to match CNC Shield
#define STEP_X 2
#define DIR_Y 6
#define STEP_Y 3
#define MICROSTEPS 16
#define MOTOR_STEPS 200

int count = 0;
BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
SyncDriver controller(stepperX, stepperY);

void setup() { 
  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);
  numMessages, endBytes = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  stepperX.begin(250, MICROSTEPS);
  stepperY.begin(250, MICROSTEPS);
  delay(500);
}

void loop() {
  data();
  if (A > 0 && B > 0 && C > 0) {
    digitalWrite(EN, LOW);
    if (count <= C) {
      for (int i = 0; i <= L; i++) {
        if (count > C) {
          break;
        }
        controller.rotate(360, 40);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;  
      }

      for (int i = 0; i <= L; i++) {
        if (count > C) {
          break;
        }
        controller.rotate(360, -40);  
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
      }
    }   
  }
}

void data() {
  if (state == 0) {
    if (numMessages == 1) {
      A = QTY;
      Serial.println(A);
      numMessages = 0;
      state = 1;
    }
  }

  if (state == 1) {
    if (numMessages == 1) {
      B = QTY;
      Serial.println(B);
      numMessages = 0;
      state = 2;
    }
  }

  if (state == 2) {
    if (numMessages == 1) {
      C = QTY;
      Serial.println(C);
      numMessages = 0;
      state = 0;
    }
  }

  if (mySerial.available()) {
    inByte = mySerial.read();

    if (inByte > 47 && inByte < 58) {
      message.concat(char(inByte));
    }
    else if (inByte == 255) {
      endBytes = endBytes + 1;
    }

    if (inByte == 255 && endBytes == 3) {
      QTY = message.toInt();
      message = "";
      endBytes = 0;
      numMessages  = numMessages + 1;
    }
  }
}

Any idea?

Can you confirm the baud rate of the Nextion serial port?

You did not answer my question from post #6.

I removed the bauds=38400 from the program.s. The baud rate of the Nextion serial port should be 9600 by default, but I also tried using bauds=9600 inside the program.s, and it didn't work.

What is that? Does the Nextion need 38400 baud? If so, change the

I don't know. When I was setting up for the first time, I found information on the internet that suggested using this baud rate, so I set it that way.

Use wires as short as possible between the shield and Nextion.

The RX-TX cables are about 20 cm long. Is that okay?

What pins are you not using? Are you using the limit switch pins?

I am not using any additional pins. I put a jumper to the EN/GND, have 2 stepper motors and drivers plugged into the CNC shield, and the 12V +- cables. That's all.

When I had it running for a while, perhaps I accidentally touched the micro USB pins together on the power supply. Is it possible that this accident caused the problem? I'm not sure if it happened.

If there is any problem caused by a short, can I measure it with a multimeter somehow, isn't it?
If yes, how?

What is the easiest method to ensure I have serial communication between the CNC shield and the Nextion display?

@yotzee,
Following on from your PM to me.

The default baud rate for a Nextion display is 9600. Until you have things working reliably it does not make sense to change it, you are just adding something else that might go wrong.

Please post a schematic, your photos are better than nothing, but don't help much. From what I can see in the photos you have not connected a ground between the Arduino and the Nextion.

You said in your PM to me that you have read my tutorials, but your Nextion code and the lack of a ground connection, along with not providing a schematic suggests that you should read them again.

Thank you for joining. Yes, I've already reread some of the materials (not all, but I have a better understanding now), and I feel embarrassed about how I initially approached this topic. I'm currently in a phase of my life where I'm unlearning and learning simultaneously. I express gratitude for everything every day and appreciate even the smallest things we have. It's just the way I am—my mind resembles a browser with 40 tabs open, 4 of which are frozen, and I'm still trying to figure things out. So, I apologize once again. I'm doing my best.

I don't have much experience with Arduino. I've built my own CNC router, similar to openbuilds machines, using a CNC shield. I'm eager to build more machines with Arduino, which is why I'm learning. Additionally, I'm working on a guitar project for a friend and using it as an opportunity to learn about the Nextion HMI.

I've created this schematic; I hope it's clear enough and doesn't seem inadequate to your eyes.

The stepper motors are connected correctly, and I double-checked the wiring for A1, B1, A2, and B2 before connecting. They are functioning properly. Establishing the GND connection between the Nextion and the CNC shield was beneficial; now, it's up and running. However, I'm still encountering some issues related to the Arduino, I believe.

I'm still facing two problems: Firstly, the stepper motors don't turn the same amount I input. Secondly, once it's finished and zeroed, I'm unable to start another cycle.

Thank you for offering your help.

Schematic

Last sketch uploaded to arduino

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(A0, A1); // RX, TX
int A = 0;
int B = 0;
int C = 0;
int L = 40; // Adjust the number of rotations as needed
int state = 0;
int EN = 8;  // Change the stepper enable pin to match CNC Shield
String message;
int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;

#define DIR_X 5  // Change these pin numbers to match CNC Shield
#define STEP_X 2
#define DIR_Y 6
#define STEP_Y 3
#define MICROSTEPS 16
#define MOTOR_STEPS 200

int count = 0;
BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
SyncDriver controller(stepperX, stepperY);

void setup() { 
  pinMode(EN, OUTPUT);
  digitalWrite(EN, LOW);
  numMessages = 0;
  endBytes = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  stepperX.begin(250, MICROSTEPS);
  stepperY.begin(250, MICROSTEPS);
  delay(500);
}

void loop() {
  data();
  if (A > 0 && B > 0 && C > 0) {
    digitalWrite(EN, LOW);
    if (count <= C) {
      count = 0; // Reset count before starting a new operation

      for (int i = 0; i < L; i++) { // Iterate for the specified number of rotations
        if (count > C) {
          break;
        }
        controller.rotate(360, 40);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;  
      }

      // Reset values after completing the rotations to enable a new job
      A = 0;
      B = 0;
      C = 0;
    }
  }
}

void data() {
  if (state == 0) {
    if (numMessages == 1) {
      A = QTY;
      Serial.println(A);
      numMessages = 0;
      state = 1;
    }
  }

  if (state == 1) {
    if (numMessages == 1) {
      B = QTY;
      Serial.println(B);
      numMessages = 0;
      state = 2;
    }
  }

  if (state == 2) {
    if (numMessages == 1) {
      C = QTY;
      Serial.println(C);
      numMessages = 0;
      state = 0;
    }
  }

  if (mySerial.available()) {
    inByte = mySerial.read();

    if (inByte > 47 && inByte < 58) {
      message.concat(char(inByte));
    }
    else if (inByte == 255) {
      endBytes = endBytes + 1;
    }

    if (inByte == 255 && endBytes == 3) {
      QTY = message.toInt();
      message = "";
      endBytes = 0;
      numMessages = numMessages + 1;
    }
  }
}

:heartpulse:

Hello everyone!

Since then, I've assembled the machine and delved deeper into both Arduino and the Nextion display. I've realized that among the 3 inputs, only the "turns" or the C value affects anything. So, it doesn't matter what I input for wire thickness or coil width. The value of the "L" variable in the code will determine how much the machine moves along the Y-axis, regardless of the values for A and B.

I tried writing the sketch myself and even sought help from our chat GPT friend, but I couldn't make any progress. What I aim for is that the A (wire) and B (coil width) inputs influence the movement along the Y-axis. I've been browsing forums in hopes of finding a solution, but I haven't found one yet. Please, someone, help me out. I would be incredibly grateful. Thank you, and all the best!

Could anyone offer assistance, please? I'm seeking some guidance or direction. I've recently embarked on learning C++ from the ground up and attempted to work with just the AccelStepper library. However, I'm facing difficulties in getting it to function properly.

Hello @yotzee,
I see no evidence in your code that you have read my Nextion tutorial. I can help you with communicating with a Nextion if you follow the ideas I set out in my tutorial. As you have chosen to invent your own way of communicating with a Nextion I cannot help.

My advice is to separate your attempts to communicate with your Nextion from your attempts to communicate with the CNC shield. Understand how to do each one separately, only then try to join the 2 together.

If C++ is completely new to you then I think you are trying to run before you can walk, understand the basics first.

I will monitor this topic, but I will only reply if I see questions related to your attempts to use my Nextion methods.

Good luck with your project.

1 Like

Hello, @PerryBebbington,

Thank you for your response. I've successfully established a connection between my Arduino and the Nextion display, and it appears to be functioning correctly. I've conducted debugging through the serial monitor, and all inputs seem to be working as expected, affirming a good connection.

However, I'm encountering an issue with the machine's functionality. Although it runs and performs countdowns, it's not responding to the A and B variables.

There are three input variables: A (wire), B (coil width), and C (turns). Among these, only the C variable seems to have an effect within the sketch. The others are solely visual; the sketch checks if they're non-zero before functioning.

Remarkably, there's a fourth variable called "L", not inputted from the display, serving as a 'static' variable within the sketch. It controls the motor movement's travel distance, a role that should be fulfilled by the A and B variables, not L.

My goal is to have the Y-axis execute a movement with a maximum distance equal to the B variable, stepping by the value of the A variable. This should continue until the C value reaches zero.

For instance:
A = 0.1
B = 15
C = 200

The X-axis initiates full revolution rotation. After each full revolution, C decrements. Meanwhile, the Y-axis travels the A value at each full X rotation until it reaches the B value. Then, it repeats this process until returning to the start position, looping until C hits zero.

I think the issue doesn't lie in how the Nextion communicates with the Arduino; it might be functioning correctly. It seems the void Data section is fine, and the necessary changes should be made in the loop section. It's possible I overlooked this question initially and got lost in the details. Despite trying numerous approaches, I haven't been able to make it work as intended.

I've gone through your Nextion display tutorial. The cat part scared me, although I have a dog. I've also come across suggestions against connecting the 5V to the Arduino, favoring a micro USB charger. Perhaps this explains the capacitor in your method.

Regarding your tutorial, I noticed the use of 0xa5 instead of 0xff. Could you guide me on changing these values?

I also referred to this site: Instruction Set - Nextion. However, there are aspects that I'm struggling to comprehend. Some sections seem entirely nonsensical to me.

I do possess programming knowledge in Java (developed a 5-axis postprocessor) and C# (hobbyist Unity game dev scripts). I'm learning slowly but steadily, although the process can be quite challenging. I admit, my coding skills are still in the learning phase.

It's nighttime here, and I'll be heading to bed soon. Tomorrow, I plan to reset both my Arduino and Nextion display, and I'll go through your tutorial from start to finish. I'll endeavor to build upon that foundation.

Attached is the last sketch that worked.

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(A0, A1);  // RX, TX
int A = 0;
int B = 0;
int C = 0;
int L = 40;  // Adjust the number of rotations as needed
int state = 0;
int EN = 8;  // Change the stepper enable pin to match CNC Shield
String message;
int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;

#define DIR_X 5  // Change these pin numbers to match CNC Shield
#define STEP_X 2
#define DIR_Y 6
#define STEP_Y 3
#define MICROSTEPS 16
#define MOTOR_STEPS 400

int count = 0;
BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
SyncDriver controller(stepperX, stepperY);

void setup() {
  pinMode(EN, OUTPUT);
  digitalWrite(EN, HIGH);
  numMessages, endBytes = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  stepperX.begin(1000, MICROSTEPS);
  stepperY.begin(1000, MICROSTEPS);
  delay(500);
  Serial.println("Setup complete.");  // Debugging: Check if setup completes
}

void loop() {
  data();
  if (A > 0 && B > 0 && C > 0) {
    digitalWrite(EN, LOW);
    while (count < C) { // Change to a while loop to ensure it runs till count reaches C
      for (int i = 0; i < L && count < C; i++) { // Ensure count is still less than C before rotating
        controller.rotate(360, 40);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
        delay(0); // Introduce a delay to give time for the motor to rotate
      }

      for (int i = 0; i <= L && count < C; i++) { // Ensure count is still less than C before rotating
        controller.rotate(360, -40);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
        delay(0); // Introduce a delay to give time for the motor to rotate
      }
    }
  


    if (count >= C) {
      // Reset job parameters after completion
      A = 0;
      B = 0;
      C = 0;
      count = 0;
      digitalWrite(EN, HIGH); // Disable the stepper motor
      // Add any other necessary resets here
    }
  }
}


void data() {
  if (state == 0) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      A = QTY;
      Serial.println(A);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 1;
    }
  }

  if (state == 1) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      B = QTY;
      Serial.println(B);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 2;
    }
  }

  if (state == 2) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      C = QTY;
      Serial.println(C);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 0;
    }
  }


  if (mySerial.available()) {  //Is data coming through the serial from the Nextion?
    inByte = mySerial.read();

    // Serial.println(inByte); //See the data as it comes in

    if (inByte > 47 && inByte < 58) {  //Is it data that we want to use?
      message.concat(char(inByte));    //Cast the decimal number to a character and add it to the message
    } else if (inByte == 255) {        //Is it an end byte?
      endBytes = endBytes + 1;         //If so, count it as an end byte.
    }

    if (inByte == 255 && endBytes == 3) {  //Is it the 3rd (aka last) end byte?
      QTY = message.toInt();               //Because we have now received the whole message, we can save it in a variable.
      message = "";                        //We received the whole message, so now we can clear the variable to avoid getting mixed messages.
      endBytes = 0;                        //We received the whole message, we need to clear the variable so that we can identify the next message's end
      numMessages = numMessages + 1;       //We received the whole message, therefore we increment the number of messages received.

      //Now lets test if it worked by playing around with the variable.
    }
  }
}

OK, good! I can't help you with the CNC shield, I know nothing about them, sorry.

I don't understand why you are using single letters for your variables, use names that mean something, maybe wire, coilWidth and turns, which would make your code a lot easier to read. Same thing for 'L' what is 'L'? Give it a descriptive name.

Well, a reverse connected capacitor exploding would scare your dog too, but I have a cat. The capacitor is recommended, it is not mandatory. If you don't want to add one don't worry about it.

As it says in the tutorial, I found that a Nextion display generates a lot of electrical noise, the capacitor reduces that.

It uses 0xa5 for sending from the Nextion to my code, other than what is in my code I can't tell you any more unless you ask a specific question. From my code to the Nextion it uses 0xff as per Nextion guidelines.

I don't think there's anything more I can tell you. Hopefully someone better able than me to deal with your code can help.

I don't understand why you are using single letters for your variables, use names that mean something, maybe wire, coilWidth and turns, which would make your code a lot easier to read. Same thing for 'L' what is 'L'? Give it a descriptive name.

I changed the abbreviations to words. 'L' represents the 'number of rotations.' Frankly, I don't understand the code that came with the tutorial from that person named Sandy at github. It simply doesn't make sense to me how he created this project. Seems rather pointless.

I've attempted various programming approaches to achieve my desired outcome, but unfortunately, it's still not functioning as intended. I'm feeling a bit fatigued with this project. Do you happen to know where I could find professional assistance for a fee?

Last sketch that i uploaded:

#include <Stepper.h>
#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(A0, A1);  // RX, TX
int wire = 0;
int coilWidth = 0;
int turns = 0;
int L = 40;  // Adjust the number of rotations as needed
int state = 0;
int EN = 8;  // Change the stepper enable pin to match CNC Shield
String message;
int QTY, numMessages, endBytes;
byte inByte;
int flag = 0;


#define DIR_X 5  // Change these pin numbers to match CNC Shield
#define STEP_X 2
#define DIR_Y 6
#define STEP_Y 3
#define MICROSTEPSX 1
#define MICROSTEPSY 16
#define MOTOR_STEPS 400


int count = 0;
BasicStepperDriver stepperX(MOTOR_STEPS, DIR_X, STEP_X);
BasicStepperDriver stepperY(MOTOR_STEPS, DIR_Y, STEP_Y);
SyncDriver controller(stepperX, stepperY);

void setup() {
  pinMode(EN, OUTPUT);
  digitalWrite(EN, HIGH);
  numMessages, endBytes = 0;
  Serial.begin(9600);
  mySerial.begin(9600);
  stepperX.begin(200, MICROSTEPSX);
  stepperY.begin(200, MICROSTEPSY);
  delay(500);
  Serial.println("Setup complete.");  // Debugging: Check if setup completes
}

void loop() {
  data();
  int stepsRequired = (wire / 8) * MOTOR_STEPS * MICROSTEPSY;
  if (wire > 0 && coilWidth > 0 && turns > 0) {
    digitalWrite(EN, LOW);
    while (count < turns) { // Change to a while loop to ensure it runs till count reaches C
      for (int i = 0; i < L && count < turns; i++) { // Ensure count is still less than C before rotating
        controller.rotate(360, stepsRequired);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
        delay(0); // Introduce a delay to give time for the motor to rotate
      }

      for (int i = 0; i <= L && count < turns; i++) { // Ensure count is still less than C before rotating
        controller.rotate(360, -stepsRequired);
        mySerial.print("n2.val=");
        mySerial.print(count);
        mySerial.write(0xff);
        mySerial.write(0xff);
        mySerial.write(0xff);
        count++;
        delay(0); // Introduce a delay to give time for the motor to rotate
      }
    }
  


    if (count >= turns) {
      // Reset job parameters after completion
      wire = 0;
      coilWidth = 0;
      turns = 0;
      count = 0;
      digitalWrite(EN, HIGH); // Disable the stepper motor
      // Add any other necessary resets here
    }
  }
}


void data() {
  if (state == 0) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      wire = QTY;
      Serial.println(wire);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 1;
    }
  }

  if (state == 1) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      coilWidth = QTY;
      Serial.println(coilWidth);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 2;
    }
  }

  if (state == 2) {
    if (numMessages == 1) {  //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
      turns = QTY;
      Serial.println(turns);  //See what the important message is that the Arduino receives from the Nextion
      numMessages = 0;    //Now that the entire set of data is received, reset the number of messages received
      state = 0;
    }
  }


  if (mySerial.available()) {  //Is data coming through the serial from the Nextion?
    inByte = mySerial.read();

    // Serial.println(inByte); //See the data as it comes in

    if (inByte > 47 && inByte < 58) {  //Is it data that we want to use?
      message.concat(char(inByte));    //Cast the decimal number to a character and add it to the message
    } else if (inByte == 255) {        //Is it an end byte?
      endBytes = endBytes + 1;         //If so, count it as an end byte.
    }

    if (inByte == 255 && endBytes == 3) {  //Is it the 3rd (aka last) end byte?
      QTY = message.toInt();               //Because we have now received the whole message, we can save it in a variable.
      message = "";                        //We received the whole message, so now we can clear the variable to avoid getting mixed messages.
      endBytes = 0;                        //We received the whole message, we need to clear the variable so that we can identify the next message's end
      numMessages = numMessages + 1;       //We received the whole message, therefore we increment the number of messages received.

      //Now lets test if it worked by playing around with the variable.
    }
  }
}

Yes, there is a forum category for that, do you want me to move this topic to it?

1 Like

Yes, please. I would be grateful. Thank you.