Hi eveyrbody.
Could you please help me fix my program.
I use Arduino UNO R3 & module TB6560 to control Step motor SUMTOR 3A.
The key point is when I input "Turn R 100" to command line of Serial monitor, this motor rotates continuously. I can not stop it.
My program as below (or attached file):
code:
int _step = 0, Distance = 0, count; // Record the number of steps we've taken void setup()
int new_loop, old_loop;
String new_dir, old_dir, command = "", turning, cycle; // Data received from the serial port
char character;
int CW = 8;
int CLK = 9;
void setup() {
pinMode(CW, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
Serial.begin(9600);
}
void loop() {
// If data is available to read,
while (Serial.available()) {
character = Serial.read();
command.concat(character);
}
turning = command.substring(0, 4); // Split "Turn" order
new_dir = command.substring(5, 6); // Split "R" order
cycle = command.substring(7);
new_loop = cycle.toInt();
// if ((new_dir != old_dir)|| (new_loop!= old_loop) )
if (turning == "Turn")
{
old_loop = new_loop;
old_dir = new_dir;
_step = old_loop * 3200 / 100;
if (old_dir == "L")
{
digitalWrite (8, HIGH);
}
else if (old_dir == "R")
{
digitalWrite(8, LOW);
}
} else {
_step = 0;
}
count = 0;
for (int x = 0; x < _step; x++)
{
digitalWrite(9, HIGH);
delayMicroseconds(200);
digitalWrite(9, LOW);
delayMicroseconds(200);
Distance++; // record this step // Check to see if we are at the end of our move
}
if (Distance == _step) {
_step = 0;
}
Serial.println(command);
delay (500);
}
For short programs please include them in your post using the code button </> like this
int _step = 0, Distance = 0, count; // Record the number of steps we've taken void setup()
int new_loop, old_loop;
String new_dir, old_dir, command = "", turning, cycle; // Data received from the serial port
char character;
int CW = 8;
int CLK = 9;
void setup() {
pinMode(CW, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
Serial.begin(9600);
}
void loop() {
// If data is available to read,
while (Serial.available()) {
character = Serial.read();
command.concat(character);
}
turning = command.substring(0, 4); // Split "Turn" order
new_dir = command.substring(5, 6); // Split "R" order
cycle = command.substring(7);
new_loop = cycle.toInt();
// if ((new_dir != old_dir)|| (new_loop!= old_loop) )
if (turning == "Turn")
{
old_loop = new_loop;
old_dir = new_dir;
_step = old_loop * 3200 / 100;
if (old_dir == "L")
{
digitalWrite (8, HIGH);
}
else if (old_dir == "R")
{
digitalWrite(8, LOW);
}
} else {
_step = 0;
}
count = 0;
for (int x = 0; x < _step; x++)
{
digitalWrite(9, HIGH);
delayMicroseconds(200);
digitalWrite(9, LOW);
delayMicroseconds(200);
Distance++; // record this step // Check to see if we are at the end of our move
}
if (Distance == _step) {
_step = 0;
}
Serial.println(command);
delay (500);
}
My guess is that the motor keeps running because loop() keeps repeating the movement.
If you only want the motor to make a single movement then you need a variable to manage that. For example when a new message arrives set motorMayRun = true and when the first movement is finished set it to false. And (obviously) only make the motor run if (motorMayRun == true)
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data
Hi Robin2.
Thank you very much for your time.
I tried to find out this button </> when I wrote this topic but I did't found it. Sorry.
Actually I want to send full command " Turn R 100" to tell motor turn right 1 round.
To do that I split this command to 3 String: Turn, R & 100.
And use condition: "turning = turn" to tell motor turns.
I also use variable "distance" to know when motor finish then reset Step to zero.
Hi Robin2.
I'm beginner in programing field so sorry if my question is stupid.
I already studied Serial Input Basic topic as your suggestion.
My understanding is: Arduino will receive a String from Serial monitor by reading each character. So if I write: "command = Serial.readString()", I will mess Arduino up because it doesn't know when it need to finish reading. So it can not get exactly what we want to transfer.
Is my understanding correct?
And one more question. Could you please explain to me why do we need this command?
Hi Robin2.
Maybe you misunderstand what I mean.
I understand your example & of course there's no readString() in your examples.
I did as same as your example & used strcmp() command then my problem is solved.
I just want to confirm that in the code at first post, I used readString(), So it mess Arduino up caused Arduino board doesn't know when it need to finish reading? Is it right?
Otherwise, please teach me why do we need this command in your example:
lodolfo:
Otherwise, please teach me why do we need this command in your example:
if (ndx >= numChars) {
ndx = numChars - 1;
}
That is just there to prevent an unexpected long message from writing past the end of the receivedChars[] array and screwing up some other part of memory.