I am taking a second look at an older video, number 151. I described controlling a TB6600 connected to a stepper motor in that video. I have been asked many questions about this video, and I changed how I do things with the Nextion display. I thought an update was in order. Below are some of the items I used in the video. They are affiliate links. I make a little on every order.
The Newest Video
The Files Used
Here are the files used in the making of the video.
The Original Video(151)
Here is the original video before converting the timer to microseconds, adding command delimiters, and adding feedback from the Nextion display.
Arduino to Nextion
When I started working with the Nextion display, I wanted to eliminate the Nextion.h library. I found it confusing, adding complexity. I decided to send raw RS232 text between the Arduino and the display. This did not go exactly as planned. I discovered that the commands sent to the Nextion had to be exactly as they are in the IDE. It took me a while to figure out the format, but I was off to the races once I got it down. The Arduino to Nextion portion has stayed the same and will remain the same unless Nextion changes. I hope they don’t make any changes.
Nextion to Arduino 1st Attempt
My next step was to make the Arduino interpret the commands from the Nextion. When I first started, I checked that little box “Send Component ID.” I figured that it was a nice automated feature. It did not take long to change my mind. Adding the ending set of 0xFF hex codes and a lack of control over the format caused me headaches.
Nextion to Arduino 2nd Attempt
My next attempt was similar to this article’s earlier video(#151). I would send commands and then interpret the whole command in the Arduino MCU. This worked fine until I started seeing garbage on the serial port. I was not always getting garbage. Sometimes the data would get out of sync, and good data would look like bad data. There was another issue when I sent multiple commands back to back. They would come through as a single command, and both would be ignored. I had to come up with a way to separate commands. This is when I decided to add a delimiter.
Nextion to Arduino 3rd Attempt
I am currently using this method. I want to say it is the final one, but I am sure I will change it. I have tweaked it over time, but it seems robust. I discuss in the video that I should be checking the beginning delimiter better, and probably will in future videos.
Nextion to Arduino Format
I came up with my beginning delimiter based on the Cheap Controls name. I use three characters, “C:C” followed by a command. I follow that with the command. When I start the project, I have to determine the command length. Every event that sends a string will use the same length of command. I usually use a three-character command. A value follows the command. The value can be any length but is usually ASCII text. If a value is sent, I use the COVX function in the Nextion to turn it into text. I then use the “toInt()” function in the Arduino to convert it back. There are ways to send values, but it can get more complicated than converting them. I also like the ability to see the values while troubleshooting.
Results in this Video
Adding the beginning and ending delimiters made this version of the TB6600 control system more reliable. I also left in the code that printed out the command and value on the serial port. This made troubleshooting easier.
microseconds VS milliseconds
The initial video used microseconds to control the delay. I had the stepper motor take a step every time through the delay. This resulted in the motor running slowly because of the total number of steps required for a single rotation. I thought this was a good idea so the video would clearly show the motor turning. This was a mistake. Changing the delay timing to microseconds made the video more entertaining and showed the movement more clearly. This was not a change in how I code or work with the Nextion display. This was a good learning experience that will lead to better videos.
Feedback
Feedback is the most recent change I have made in my programming methods. I want to make sure the Arduino and the Nextion receive the commands. If communication fails, the system that is being built is useless. I am still working on different feedback methods. I don’t believe there will only be one method that will always work. In this example, I use a variable in the Nextion that must be reset by the Arduino before communication can continue.
In some other configurations, I have used button color. I have a button sending a specific command if green and a different command if it is red. The Arduino sets the button’s color based on what it currently thinks the status of operations is. You have to decide the most critical functions in a project and then configure your feedback around those functions.
A machinist once asked about incorporating an emergency stop button on a Nextion display. I showed how to do this video #76(I think it was #76). But, I cautioned against doing it with the display. If the button needed to be pressed, you would have to rely on the display working, sending the data to the Arduino, the Arduino interpreting the command, and the Arduino shutting down the system. An E-switch should be hardwired directly to the equipment you want to shut down.
Conclusion
I am happy I redid this video. I think it is a much-improved version. The changes in the command structure make the data transmission more reliable. The delay timing changes make the motor spin faster. The feedback makes the system more robust by identifying errors in real time.
In video 201 you used 2 variable boxes 1-sendString & 1 -value. I’m not sure what to put in the variable boxes I’m having a problem with this. My stepper only runs in one direction.
sendString box-sta =string, txt= sendString
value box-sta=string, txt=not sure what to put there.
When you send information to the Arduino, it has to be in ASCII format. You need convert any numbers into the text version using the covx command. I store the converted integer in a value variable that is defined as text. I use the sendstring variable to build the string that I will eventually send to the Arduino. You are not allowed to add multple string together in the Nextion. I build them over a few lines of code using the sendstring variable. I start with my beginning delimiter of sendstring = “C:C” then I add my command sendstring += “command”(usually three characters) Then I add my value sendstring += value. Finally I add my ending delimiter sendstring += “?”. To recap the value is used to store the integer that I want to send as a string. The sendstring holds the complete string I send to the arduino. I hope this helps. If you need more help you can submit a request on the help portion of the website. I want you to know that this is the first non-spam comment Cheap Controls has ever gotten.