I'm trying to replace the noisy DC gear motor that rewinds my clock with a stepper and two Hall effect limit switches. I can make the stepper run with a Sketch like this for the Uno:
The Hall effect sensors are listed here but play no role in the rest of it, yet.
#include <ezButton.h>
#include <AccelStepper.h>
// Motor interface type must be set to 1 when using a driver
#define DIR 2
#define STEP 3
#define motorInterfaceType 1
#define HALL_SENSOR_A A0
#define HALL_SENSOR_B A2
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, STEP, DIR);
void setup()
{
// Setup the stepper controller pins as Outputs
pinMode(DIR,OUTPUT);
pinMode(STEP,OUTPUT);
// Setup the Hall Effect switches as Inputs
pinMode(HALL_SENSOR_A, INPUT);
pinMode(HALL_SENSOR_B, INPUT);
stepper.setMaxSpeed(1000.0); // set the maximum speed
stepper.setAcceleration(100.0); // set acceleration
stepper.setSpeed(200); // set initial speed
}
void loop(){
stepper.runSpeed();
}
The single limit switch in the following Sketch will turn on the motor and it will run for a certain number of revolutions.
#include <ezButton.h>
#include <AccelStepper.h>
// Motor interface type must be set to 1 when using a driver
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
#define MAX_POSITION 5000 // 0x7FFFFFFF // maximum of position we can set (long type)
ezButton limitSwitch_1(A0); // create ezButton object that attach to pin A0;
ezButton limitSwitch_2(A2);
bool isStopped = true; //false;
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50); // set debounce time to 50 milliseconds
limitSwitch_2.setDebounceTime(50); // set debounce time to 50 milliseconds
stepper.setMaxSpeed(500.0); // set the maximum speed
stepper.setAcceleration(100.0); // set acceleration
stepper.setSpeed(400); // set initial speed
stepper.setCurrentPosition(0); // set position
//stepper.moveTo(MAX_POSITION);
}
void loop() {
limitSwitch_1.loop(); // MUST call the loop() function first
if (limitSwitch_1.isPressed())
{
stepper.moveTo(MAX_POSITION);
Serial.println(F("The limit switch: TOUCHED"));
isStopped = false; //true;
//stepper.run();
//delay(5000);
}
if (isStopped == false)
{
stepper.run();
if (stepper.distanceToGo() == 0) // if motor moved to the target position
{
stepper.setCurrentPosition(0); //reset position to 0
//stepper.moveTo(MAX_POSITION); // move the motor to maximum position again
isStopped = true;
Serial.println(F("The stepper motor is STOPPED"));
}
}
}
This Sketch actually would work for my needs, except I'd have to tune the Max Position value for the motor to lift the weight the correct distance. Doable, but not as straight forward as having one switch to turn it on and a second to turn it off.
Anyone here with enough experience on how to use one switch to turn on the motor, like the second Sketch does, but then turn it off with a second switch? I've driven myself nuts looking at YouTube and Arduino related videos and documents. You would think someone would specifically address this situation, but I haven't found it. Any help is most appreciated.
John