Dear all,
I am reading serial port to move stepper motor in one direction and a specified distance.
My problem is to type direction and distance together to work but I want to send first direction and then distance to move. Please guide me where am I wrong?
Note: to move right 5mm, I have to send r5 not r then 5 separately
/****************************************************/
if (Serial.available() > 0) {
// read the incoming char:
incomingChar = Serial.read();
/****************************************************/
if (incomingChar=='r'){ // Move right
dist = Serial.parseFloat(); // Enter distance in mm
pos = 1000dist;
}
/************************************************/
The small fragment of code you show looks OK. Perhaps the problem is in the rest of your code somewhere. Can you write a complete sketch that shows the problem?
Remember that the default timeout for .parseFloat() is one second. If you send 'r' and don't follow it within one second with a number, you will get a zero.
float pos;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available() > 0)
{
// read the incoming char:
char incomingChar = Serial.read();
if (incomingChar == 'r') // Move right
{
float dist = Serial.parseFloat(); // Enter distance in mm
pos = 1000.0 * dist;
Serial.print("Move right ");
Serial.println(pos);
}
}
}
This works on my Arduino UNO if I send the number within one second of sending the 'r'.
Move right 0.00 (Here I was not fast enough.)
Move right 5000.00 (Here I send 'r' and '5' as two separate lines but within one second.)
What you should probably be doing is save the input stream to a char array and then parse the array. If r5 is right 5mm, is it safe to assume l5 is left 5mm? This would be the equivalent of clockwise and counter-clockwise.
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this
johnwasser:
Remember that the default timeout for .parseFloat() is one second. If you send 'r' and don't follow it within one second with a number, you will get a zero.
float pos;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available() > 0)
{
// read the incoming char:
char incomingChar = Serial.read();
if (incomingChar == 'r') // Move right
{
float dist = Serial.parseFloat(); // Enter distance in mm
pos = 1000.0 * dist;
Serial.print("Move right ");
Serial.println(pos);
}
}
}
This works on my Arduino UNO if I send the number within one second of sending the 'r'.
Move right 0.00 (Here I was not fast enough.)
Move right 5000.00 (Here I send 'r' and '5' as two separate lines but within one second.)
Thanks yes you are right. When I send two commands within one second it works. and i I delay more than one second I get 0.00.
Like:
Enter Distance with direction To Move: e.g., r1, l1 etc.
r0.00
r2.00
r3.00
l5.00
l2.00
r3.00
r0.00
Thanks for the help.
yes its right. When I send two commands within one second it works. and if I delay more than one second then I receive 0.00 on the serial monitor.
Like:
Enter Distance with direction To Move: e.g., r1, l1 etc.
r0.00 //delay more than one second
r2.00 //two commands send within second
r3.00
l5.00
l2.00
r3.00
r0.00