Hi i am making a plotting machine and i want to interpret G-code to run some stepper motors. They need to run at the same time and at different speeds so i utilised the modified Accelstepper library for the stepper motors i am using(28byj-48). I always seem to get 1.00 as my stepper speed when i read it even though i read the variable i used to set the speed and it is not 1.00. This is preventing my motors from moving at different speeds. I have changed the speed in this way because the motors must finish the G-code command at the same time.
Here is my code.
#include <AccelStepper.h>
int xspd;
int yspd;
const int spd=60;
int n;
int x=10;
int y=10;
int l;
AccelStepper stepperY(5, 2, 3, 4, 5);
AccelStepper stepperX(5, 6, 7, 8, 9);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
stepperX.setCurrentPosition(0);
stepperY.setCurrentPosition(0);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
if(Serial.peek()=='G'){
Serial.read();
n=Serial.parseInt();
Serial.println(n);
if(Serial.peek()=='X'){
Serial.read();
x=Serial.parseInt();
}
if(Serial.peek()=='Y'){
Serial.read();
y=Serial.parseInt();
}
switch(n){
case 0:
if(stepperY.distanceToGo()== 0){
if(y<x){
yspd=(spd*y/x);
Serial.println("yspd is ");
Serial.println(yspd);
stepperX.moveTo(x);
stepperY.moveTo(y);
stepperY.setSpeed(yspd);
stepperX.setSpeed(spd);
// stepperY.setSpeed(spd);
// stepperX.setSpeed(spd);
Serial.println("y speed is ");
Serial.println(stepperY.speed());
Serial.println("x speed is ");
Serial.println( stepperX.speed());
}else if(x<y){
xspd=(spd*x/y);
Serial.println("xspd is ");
Serial.println(xspd);
stepperX.moveTo(x);
stepperY.moveTo(y);
stepperX.setSpeed(xspd);
stepperY.setSpeed(spd);
Serial.println("y speed is ");
Serial.println(stepperY.speed());
Serial.println("x speed is ");
Serial.println(stepperX.speed());
// stepperY.setSpeed(spd);
// stepperX.setSpeed(spd);
}
}
while(stepperY.distanceToGo()> 0){
stepperX.runSpeed ();
stepperY.runSpeed();
Serial.print("x=");
Serial.println(stepperX.distanceToGo());
Serial.print("y=");
Serial.println(stepperY.distanceToGo());
}
break;
}
}
}
x=0;
y=0;
}
I hope someone can help.
Thank you.