Hi!
I would like to program this type of task:
- read serial
- if char is for ex. 'a' call function step(numberOfSteps)
3. function should move stepper for exact number of steps, with constant speed, steps are given in function argument - it's very important for me - motor should stop after movement
- program waits for another input from serial port
I'm using A4988 stepper driver.
I've made a program, but I doesn't work. After calling function step(numberOfSteps) motor moves only one step.
Any comments and suggestions are welcomed.
#include <AccelStepper.h>
// Define a stepper and the pins it will use
#define STEP_PIN 9
#define DIR_PIN 8
#define ENABLE_PIN 7
#define MS1_PIN 3
#define MS2_PIN 4
#define MS3_PIN 5
AccelStepper stepper(1, STEP_PIN, DIR_PIN);
char inChar; // command from user
void setup()
{
Serial.begin(9600);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(ENABLE_PIN,1);
pinMode(MS1_PIN,OUTPUT);
pinMode(MS2_PIN,OUTPUT);
pinMode(MS3_PIN,OUTPUT);
digitalWrite(MS1_PIN,1); // microstepping on
digitalWrite(MS2_PIN,1);
digitalWrite(MS3_PIN,1);
digitalWrite(ENABLE_PIN,1);
stepper.setMaxSpeed(200);
stepper.setSpeed(200);
Serial.println("Device ready");
}
void loop()
{
digitalWrite(ENABLE_PIN,0); // Enable on A4988 chip
if(Serial.available() > 0){
inChar = Serial.read(); //read command
Serial.println(inChar);
switch (inChar){
case 'a': // if 'a' do for ex. 10 steps and stop -> call function step(10)
// What should I type here?
step(10);
break;
}
}
}
void step(int numberOfSteps){
if (stepper.distanceToGo() == 0)
{
stepper.move(numberOfSteps);
stepper.setSpeed(100);
}
stepper.runSpeed();
}