I'm new with stepper control and am trying to use AccelStepper instead of the standard Arduino stepper controls. I am using AccelStepper because I have read that it is very efficient especially when controlling multiple steppers - which this project will eventually have. I have read through the AccelStepper documentation but do not have a good understanding yet -still trying to learn.
Project: I have a NEMA 17 stepper with a 20T pulley driving a 1120T pulley. There is a Hall effect sensor that will detect a magnet in the 1120T pulley as it passes over.
When running for the first time, I need to home the 1120T pulley so that the magnet is centered in the middle of the Hall effect sensor range.
I can rotate with standard stepper code (below) so I know the Hall effect digital pin goes High over the expected range which is 25 steps on the stepper motor.
// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing
byte directionPin = 2;
byte stepPin = 3;
int numberOfSteps = 10000;
byte ledPin = 13;
int pulseWidthMicros = 10; // microseconds
int millisbetweenSteps = 10; // milliseconds - or try 1000 for slower steps
#define Hall_Sensor A0 //A0 used with analog output, A5 with digital output
#define Hall_Sensor_D A5
int Val1=0,Val2=0;
void setup() {
Serial.begin(9600);
Serial.println("Starting StepperTest");
digitalWrite(ledPin, LOW);
delay(2000);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(Hall_Sensor_D,INPUT);
digitalWrite(directionPin, HIGH);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
Serial.print(n);
Serial.print(",");
Val1=analogRead(Hall_Sensor); //We read both values and display them raw on the serial monitor
Serial.print(Val1);
Val2=digitalRead(Hall_Sensor_D);
Serial.print(",");
Serial.println(Val2);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(3000);
digitalWrite(directionPin, LOW);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
Serial.print(n);
Serial.print(",");
Val1=analogRead(Hall_Sensor); //We read both values and display them raw on the serial monitor
Serial.print(Val1);
Val2=digitalRead(Hall_Sensor_D);
Serial.print(",");
Serial.println(Val2);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
void loop() {
}
I am using the code above as a reference for AccelStepper to do a similar task but when I run it, the stepper doesn't move. How can I use AccelStepper to increment the steps one at a time and report (serial monitor) the step number and value (0/1) of the Hall sensor? Once this is resolved, I'll need to capture the step number when it goes HIGH the again when I goes LOW and determine the step number in the middle so the stepper can rotate to that position. I'm trying to solve the rotation issue first. Or find a better way of homing with this setup.
/*Example sketch to control a stepper motor with A4988 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>
#include <MultiStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
#define Hall_Sensor_S A0 //A0 used with analog output, A5 with digital output
#define Hall_Sensor_D_S A5
int Val1=0,Val2=0;
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int numberOfSteps = 1145;
void setup() {
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
Serial.begin(9600);
Serial.println("Starting StepperTest");
delay(2000);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(Hall_Sensor_D_S,INPUT);
stepper.move (numberOfSteps) ;
for(int n = 0; n < numberOfSteps; n++) {
Serial.print(n);
Serial.print(",");
Val1=analogRead(Hall_Sensor_S); //We read both values and display them raw on the serial monitor
Serial.print(Val1);
Val2=digitalRead(Hall_Sensor_D_S);
Serial.print(",");
Serial.println(Val2);
}
}
void loop() {
}
Any guidance would be appreciated. I haven't been able to find examples similar to this task yet.
Thank you,
G