I have a related question, which is a knock on from the project. Today I made a little PCB that the Nano plugs into , and the PCB also has a ULN2003 chip on it with pins 2, 3, 4 and 5 hard wired to the ULN2003 IN1, IN2, IN3 and IN4 respectively. It works well, but I have found that no matter what I do I cannot get the stepper to home in the reverse direction
This is the setup
#include "AccelStepper.h"
#define HALFSTEP 8
#define motorPin1 2
#define motorPin2 3
#define motorPin3 4
#define motorPin4 5
int endPoint = 1024; // Move this many steps; 1024 = approx 1/4 turn
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
and this is the homing code
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run();
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
//stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(1000.0); // Set Acceleration of Stepper
}
I thought that I could reverse the pins in the sketch in this section
but no matter what order I put the pins in it always moves in same direction. With separate ULN2003 modules I used for testing, the wires connecting the Nano pins and the ULN2005 input pins could be reversed, and this would reverse the direction of the motor. However my hard wired PCB can't be changed, so I am hoping that I can do this in the sketch in some other way that redefining the motorPin numbers.
Code snippets are always a bad idea. What is 'initial_homing'? How is it defined and set?
And decreasing 'initial_homing' in the while loop is done very very quickly, because stepper.run() will not block until a step is due. So maybe it is beyond zero before run() creates a step.
#include "AccelStepper.h"
#define HALFSTEP 8
#define motorPin1 2
#define motorPin2 3
#define motorPin3 4
#define motorPin4 5
int endPoint = 1024; // Move this many steps; 1024 = approx 1/4 turn
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48
AccelStepper stepper(HALFSTEP, motorPin3, motorPin2, motorPin4, motorPin1);
bool buttonState1, buttonState2;
const int BUTTON_PIN1 = 7; // the number of the pushbutton pin
const int BUTTON_PIN2 = 8; // the number of the pushbutton pin
// Define the Pins used
#define home_switch 12 // Pin 9 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
pinMode(home_switch, INPUT_PULLUP);
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(5);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(1000.0); // Set Acceleration of Stepper
}
void loop() {
buttonState1 = digitalRead(BUTTON_PIN1);
buttonState2 = digitalRead(BUTTON_PIN2);
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500.0);
if (buttonState1 == LOW) // if point switch is in position A move stepper to position defined on the next line
stepper.moveTo(200); // change number in brackets to suit the points throw
stepper.run();
if (buttonState2 == LOW) // if point switch is in position B move stepper to zero position
stepper.moveTo(0); // this is the same position as the microswitch used for homing
}
AccelStepper stepper(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
...
long initial_homing = -4000; // Set to the max steps it may need to reach the reference point
...
// Start Homing procedure of Stepper Motor at startup
stepper.moveTo(initial_homing); // max steps to reach home-switch
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.run(); // Start moving the stepper
}
stepper.setCurrentPosition(0); // Set the current position as zero for now