AccelStepper Only Executing Final Value Passed Through Serial

Hello all! I have written some Arduino code to turn 2 stepper motors from a serial input.

Right now it works if I enter a comma separated pair of steps into the serial monitor (i.e. 1000,1000).

My issue is that if I enter more than one "pair" of angles (i.e. 1000,1000,2000,2000), it will only execute for the "final pair" (2000,2000 in this example).

My endgame is to use python to send about 2000 pairs of angles stored in a csv through serial, but I'm facing the same problem of only the last pair being used.

My code is below, and there is some more info in my previous two posts (thanks to everyone who commented on those, I truly appreciate the help).
https://forum.arduino.cc/index.php?topic=735638.new#new
https://forum.arduino.cc/index.php?topic=733014.new#new

Thanks for any help and for taking the time to read!

#include "AccelStepper.h"
AccelStepper pitch(1,6,5);
AccelStepper yaw(1,11,10);
long pitchangle;
long yawangle;
int finish = 1;
void setup() {
  Serial.begin(9600);
 
  pitch.setMaxSpeed(500.0);
  pitch.setAcceleration(500.0);
  yaw.setMaxSpeed(500.0);
  yaw.setAcceleration(500.0);
}
void loop() {
  while (Serial.available()>0) {
    finish = 0;
    pitchangle = Serial.parseInt();
    Serial.print(pitchangle);
    Serial.print("Pitch Angle, ");
    yawangle = Serial.parseInt();
    Serial.print(yawangle);
    Serial.print("Yaw Angle, ");
    pitch.move(pitchangle);
    yaw.move(yawangle);
   
    Serial.print("Moving...");
    }
 
    if ((pitch.distanceToGo() != 0) || (yaw.distanceToGo() != 0)){
      pitch.run();
      yaw.run();
    }
    if ((finish == 0) && (pitch.distanceToGo() == 0) && (yaw.distanceToGo() == 0)) {
      Serial.println("Done");
        finish =1;
    }
}

some more info in my previous two posts

It would be good of you to post links so that we can find them easily.

I think that you need some sort of handshaking between whatever the sender is and the Arduino. Then the Arduino can signal the sender when it has finished a move (or is about to) and is ready for more data.

groundFungus:
Can you remove that . It makes the code hard to read and impossible to verify.

I think that you need some sort of handshaking between whatever the sender is and the Arduino. Then the Arduino can signal the sender when it has finished a move (or is about to) and is ready for more data.

Yeah sorry I think I fixed it. Do you have any resources or examples of how to implement handshaking?

My issue is that if I enter more than one "pair" of angles (i.e. 1000,1000,2000,2000), it will only execute for the "final pair" (2000,2000 in this example).

Enter each pair separately, or else change the code to read in only two values from the serial buffer for the pair to send to the stepper. You will need to read fast enough to keep the buffer from overflowing if you continually enter data.

Check out my complete code solution to sending a CSV file from Python to Arduino.
One useful part of the the solution is that the python side is blocked from sending more lines until the Arduino side has processed the last line and is ready to continue.

https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/ComsPair.html#python

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.