How to constantly read a position of a motor

Hello,
I am currently using the Arduino Mega and have my motor set up to receive and send data through serial port 1.

Here is a description of general commands that I can send to an Accelnet Panel ADP that is used to communicate with the motor:
ASCII Command Sent Command Description Example Response
g r0x32 Get motor position (counts) v -12598
s r0xca 0 Set motor position (counts) ok
s r0x24 11 Enable Programmed Velocity Mode ok
s r0x2f 20000 Set velocity to 2000 count/sec ok
s r0x2f -20000 Set velocity to -2000 count/sec ok
s r0x2f 0 Set velocity to 0 count/sec ok

My issue is that when I want to be constantly reading the position of the motor, but I get several “ok” responses into the Serial 1 Port from the Accelnet mixed in with the actual position count that I care about.

My code so far:
int value = 0;

void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}

void loop() {
Serial1.println(“s r0x24 11”); // Enable Programmed Velocity Mode
Serial1.println(“s r0x2f 0”); // Set velocity to 0 count/sec, i.e. STOP
Serial1.println(“s r0x2f 50000”); // Set velocity to 5000 count/sec
Serial1.println(“g r0x32”); // Get the motor position (counts)
if (Serial1.available() > 0) { // Checks to see if at least one character is available.
// read the incoming byte:
char incomingByte = Serial1.read();
Serial.println(incomingByte); // Outputs the response character value from Accelnet
if(incomingByte >= ‘0’ && incomingByte <= ‘9’) // is this an ASCII digit between 0 and 9?
{
int value = (value*10) + (incomingByte - ‘0’); // converts to int
Serial.println(value); // prints the value
}
else if (incomingByte == 10)
{
value = 0; // reset value to 0 to ready for the next sequence of digits.
}
}
delay(5000); // Allow the motor to continue moving at 5000 count/sec for 5 seconds.
Serial1.println(“s r0x2f 0”); // Set the velocity to 0 count/sec, i.e. STOP
delay(1000); // Wait one second.
Serial1.println(“s r0x2f -50000”); // Reverse the direction of the motor with a velocity of 5000 count/sec.
delay(5000); // Allow the motor to continuing moving at 5000 count/sec for 5 seconds.
Serial1.println(“s r0x2f 0”); // Set the velocity to 0 count/sec, i.e. STOP
delay(1000); // Wait one second.
}

Right now my serial monitor reads this for a loop, and each line takes 12 seconds, so I’m getting a very delayed position reading:
o
k
o
k
o
k
v

3
3
9
9
8
8
2
2
5
5
9
9
o
k
o
k
o
k

If anyone has any ideas for how I can rapidly read off the position of the motor, I would greatly appreciate it.

Thanks in advance for any help!

With all those delays why are you supprised it is slow. Get rid of them.

    Serial1.println("g r0x32"); // Get the motor position (counts)
    if (Serial1.available() > 0) { // Checks to see if at least one character is available

The first statement is like posting a question on this forum.
The second is like reading the answer. How long did you wait for a response? Did you really expect one that fast?

Are you really expecting a one character reply? That is all you are reading.

You need to have the loop() function on the arduino running as fast as possible,
and only doing actual stuff when it needs to.

It should not have delays in it.

I think you need to go away and read some fundamentals about how to program
this sort of realtime operation.

Quite apart from the communication issues that you have, it isn't clear from your
program what you are actually trying to do.

When you send a request to the motor for the position, you should not try to do
anything else until you have got a response. You certainly should not send the motor
a whole lot of other unneccessarey bullshit command, until it has finished responding
to you.

You need to develop some basic concept of timing. There are things that take seconds to
happen, things which take tenths of seconds, and some which take hundredth of seconds.
You need to appreciate this, and organise your logic accordingly.

michinyon,

Thank you for your response. The reasoning behind the delays is that the
Serial1.println("s r0x2f 50000"); // Set velocity to 5000 count/sec
command tells the motor to start moving whereas the
Serial1.println("s r0x2f 0"); // Set velocity to 0 count/sec, i.e. STOP
command tells the motor to stop. If the delays weren't there, the motor would start and then stop right away, but I want the motor to be turning for 5 seconds while I am taking position measurements from the
Serial1.println("g r0x32"); // Get the motor position (counts)
command.

Is there a way to run two different loops simultaneously? This would solve my problem if I could loop the
Serial1.println("g r0x32"); // Get the motor position (counts)
command without delays while simultaneously running the set velocity commands to allow my motor to cycle back and forth for five seconds.

Thanks.

Is there a way to run two different loops simultaneously?

No. You can do different things at nearly the same time in one loop, though.

This would solve my problem

No, it wouldn't. Reading, understanding, and embracing the blink without delay example, and forgetting that delay() even exists, would.