OK, here's some working code. Very few changes, just debouncing the button and adding a run() call.
Let me know if you have questions. I tried to indicate all my changes so you can see what I did. Be sure to put in your control pin numbers!! Also, I don't know what the LimitSwitchAWAY is to be used for. I didn't use it.
-jim
----------------------------------------- Code follows ----------------------------------------------
/*
* Requested action: "All I am trying to do is get the stepper motor to turn a few degrees (calculated 3 steps) with
* each limit switch trip. Once the motor has made it around 360 degree (calculated 133.3 approx number of steps), it
* will turn back to start in the opposite direction and continue as such."
* Modified 9/11/22 to do the requested action. Moves 4 steps (3.6 degrees) per button press. Reverses after one
* rotation (400 steps).
* -jkl
*/
#include <AccelStepper.h>
//Trying defining pins?
/*int stepPin = 13;
int LimitSwitchMOTOR = 11;
int LimitSwitchAWAY = 10;
int dirPin = 12;*/
// These are for my setup. !!!!! Change back to your values !!!!!!!
// note use of const - these values don't change so it's good to use const.
const int stepPin = 5;
const int LimitSwitchMOTOR = 8;
const int LimitSwitchAWAY = 9;
const int dirPin = 4;
AccelStepper myStepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
// put your setup code here, to run once:
//myStepper.setCurrentPosition(0); << not required - AccelStepper always starts at 0.
myStepper.setMaxSpeed(1600.);
myStepper.setMinPulseWidth(20);
//myStepper.setAcceleration(1); << This is really low! If using run(), it should be higher.
myStepper.setAcceleration(1000.);
//pinMode(stepPin, OUTPUT); << Not needed, AccelStepper does this.
//pinMode(dirPin, OUTPUT);
pinMode(LimitSwitchMOTOR, INPUT_PULLUP); // I made these INPUT_PULLUP for my set up. They go low when pressed.
pinMode(LimitSwitchAWAY, INPUT_PULLUP); // !!!!! change these back to just INPUT if your buttons pull high when pressed !!!!!!
}
void loop() {
// put your main code here, to run repeatedly:
static int Counter = 0; // this must be declared static or it will be zeroed each time through the loop.
static int moveDir = 1; // this will control the direction of moves.
//if (LimitSwitchMOTOR == HIGH || LimitSwitchAWAY == HIGH) << What's LimitSwitchAWAY do? Not explained.
////////----------------- This section just checks for a button press and debounces it. ----------------//////////////
//// only uses one limit switch ///////
if (digitalRead(LimitSwitchMOTOR) == LOW) // the switch must be read as a digital input
{ bool flag = true;
while (flag) // waits for buttonto be released.
{ delay(40); // this is to debounce the button.
if (digitalRead(LimitSwitchMOTOR) == HIGH)
{ flag = false;
}
}
/////////////////----------------------- End of button debounce --------------------/////////////////
/* Counter = Counter + 1;
if (Counter > 100) // 100 counts at 4 steps per count is 400 steps - One revolution at 0.9 degrees per step.
{ myStepper.setSpeed(1600);
myStepper.move(-400);
Counter = 0;
}
myStepper.move(3);
myStepper.setSpeed(800);*/
//!!!!!!!!!!!!! here's the replacement for that last bit of code.
if (++Counter >= 100) // 100 counts at 4 steps per count is 400 steps - One revolution at 0.9 degrees per step.
{ moveDir *= -1; //Change the direction of motion and reset Counter
Counter = 0;
}
myStepper.move(moveDir * 4); // relative movement - 4 steps
}
myStepper.run(); // this is needed to actually cause movement
}