Hello all,
I am very much over my head here. This is my first project that isn't guided by a tutorial and can't seem to get it right. I could be on track for something or I could be way off base.
Attempting to run:
Arduino Uno
A4988 motor driver (only one)
12v 2A nema 17
2x limit switches
1x initiation switch (one key 2pin membrane)
The project is moving an arm to open and close. When the initiation switch is pressed, the stepper needs to continuously run one direction (i.e. clockwise) until it runs into one limit switch and stop. When the initiation switch is pressed again, the stepper needs to continuously run the opposite direction (i,e, counterclockwise) until it runs into the other limit switch and stop.
Through my searches, I have come up with this sketch that I thought would work:
const int dirPin = 2; //to driver
const int stepPin = 3; //to driver
const int enablePin = 4; //to driver
const int motionSwitch = 10; //to expand/retract button
const int expandSwitch = 11; //to opened switch
const int retractSwitch = 12; //to closed switch
const int STEPS_PER_REV = 200;
int motionSwitchState = 0;
int expandSwitchState = 0;
int retractSwtichState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(expandSwitch, INPUT); //signal motor stop
pinMode(retractSwitch, INPUT); //signal motor stop
pinMode(motionSwitch, INPUT); //signal motor start w/ opposite direction
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
digitalWrite(enablePin, LOW);
}
void loop() {
motionSwitchState = digitalRead(motionSwitch); //signals the action
if (motionSwitchState == HIGH){ //reads limit switch states to decide which direction to turn
digitalRead(expandSwitch);
digitalRead(retractSwitch);
if(expandSwitch == 1){ //turn clockwise if expand limit switch is pressed
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
}
if(retractSwitch == 1){ //turns counterclockwise if retract limit switch is pressed
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
}
}
else{ //if initiation switch is not pressed, take no action
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
}
}
Fritzing attempt (again, I am new to all of this):
project1.pdf (777 KB)