I was about to give up and ask for help, then all the sudden I got it to work.
Then I thought surely I'm not the only noob struggling with this, and I did in fact do alot of forum searching, and I did find alot of stuff, but some of it didn't apply to my situation, and the rest of it was either over my head, or for whatever other reason, I couldn't get it to work.
So while I know this is re-posting old news i'm sure, I wanted to share my results, and if it helps even one person that was about to give up and go do something easier, like competitive eating, I'll feel good about it.
The goal:
Using the 28BYJ-48 stepper motor and ULN2003 stepper motor driver that comes with every basic arduino starter kit above 25$, a breadboard, a 6.5-9v power module, and an Arduino Uno R3, use the absolute BASIC Stepper.h library and a cheap NC-NO limit switch to make a stepper motor move to a specified physical point, stop, back off, and stop again.
Just that, no more. But in the process, learn how this stuff works without having to buy a bunch of expensive hardware.
The setup
The white thing on the far left is the rail and rack setup I want to eventually use for this whole thing. It's nothing fancy, something I whipped up and 3d printed. If you want the files of that I can make them available.
A word on the switch, there's info available with a quick google, but it's not complicated. There are three prongs, A common or 'ground' a NO for Normally Open, and NC for Normally Closed. I have mine connected by a black wire from the common on the switch going to one of the grounds on the arduino. A red wire connects the Normally Open prong to one of the digital inputs on the arduino. I chose 3 but you can do whichever you want, it just has to match to what you declare in the sketch for the 'homeSwitch_A_pin' variable.
The Code:
//For Homing cycle with switch
//On start up motor runs until switch is closed
#include <Stepper.h>
int homeSwtichA_Pin = 3;
int homeSwtichA_State = 0;
const int stepsPerRevolution = 2048;
//int buttonState = 0;
//8 to IN1
//9 to IN2
//10 to IN3
//11 to IN4
Stepper Stepper_A(stepsPerRevolution, 8, 10, 9, 11);
void setup()
{
delay(1000);
Serial.begin(9600);
Stepper_A.setSpeed(5);
pinMode(homeSwtichA_Pin, INPUT_PULLUP); //DECLARE MODE OF SWITCH
homeSwtichA_State = digitalRead(homeSwtichA_Pin); //INITIAL UPDATE FOR STATUS OF SWITCH AND SWITCH VARIABLE
do {
Stepper_A.step(128); //COMPLETE PART OF FULL REVOLUTION DEPENDING ON YOUR SETUP, MINE IS 2048 STEPS FOR A FULL ROTATION
//SO AS 2048 DIVIDED BY 16 = 128 THIS IS 'THEORETICALLY' 1/16TH OF A ROTATION, OR A 22.5 DEGREE SWEEP
homeSwtichA_State = digitalRead(homeSwtichA_Pin); //UPDATE THE STATUS OF THE SWITCH AND SWITCH VARIABLE
Serial.println("HOMING");
} while (homeSwtichA_State == HIGH); //RUNS THE CODE IN THE BRACKETS UNLESS THE SWITCH IS PRESSED, THEN IT STOPS
Stepper_A.step(-128); //BACK OFF THE SWITCH, A WEE BIT.
//stepper can now be considered to be 'homed'
Serial.println("HOMED");
}
void loop()
{
}
Some things I learned:
This has been talked about other places, but I'll mention it here too:
The wiring between the motor driver and the Arduino is kind of goofy. Pick which ever pins you want, but when your setting up the stepper, you have to tell it to go in the order of IN1, IN3, IN2, IN4. Also, in an earlier post mentioned how I struggled getting anything to work until I relised the little chip on the driver board wasn't physically installed correctly, so you have to be on the lookout for stuff like that too.
Don't make the stepper take too big of steps, it won't do anything while it's taking the steps, including checking the state of the switch.
If your code isn't behaving as expected, it could be due to being stuck in a loop somewhere. You can check on this by throwing Serial.Println() codes in there, until you get it lined out.
You should use a variable to hold the information regarding the state of the switch, rather than trying to have the code read the switch itself. This is handled with 'homeSwitchA_State'
You will have to call this variable more than once, and in the right places, or you might get stuck in a loop, or it just won't work.
You need to keep your speed to 15 or less. I used 5 which was plenty. Higher values may or may not do truly terrible things, but the motor probably won't spin at all, making you think you have problems with your code.
Bear in mind that Stepper.h doesn't have a "home position" reference function. This is just to use if you want to test that your setup would theoretically work with a limit switch.
I went down the rabbit hole of AccelStepper for a while, but couldn't ever get me and it on the same page.
I recommend starting here with the basic library, then slowly and saving new files as you go, converting over to a more advance stepper library. That's what I'm going to do, but after I take a break for awhile, I fought with this every spare moment for 2 weeks just because I knew it didn't have to be complicated.
Click the switch, motor stops. That's all I wanted.
So, I hope it bails out somebody else, and good luck, god bless.
