Hello everybody!
I have been working for a couple of weeks on a project, and I'm at a point where I need some guidance.
My goal is to have a prism be movable along a track remotely.
The motor rotates a screw which pushes the prism down the track, and I wired up two limit switches and plan to place them at either end of the track so that the prism stops in the same place every time. Both limit switches work and will stop the motor when they are pressed, but I ran into a problem. When one of the limit switches is pressed and the user tells the motor to run in the opposite direction to move the cube away, the motor vibrates and doesn't run well. As soon as the limit switch is open again, the motor will run fine. (I drew a picture because that description wasn't too great)
#define switchA 13
#define switchB 11
int delaylength = 4;
byte userInput = 0;
void goForward(){
//here is where the stepper motor code goes
}
void goBack(){
//here is where the stepper motor code goes
}
void setup(){
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(switchA, INPUT_PULLUP);
pinMode(switchB, INPUT_PULLUP);
Serial.begin(9600);
while (! Serial);
Serial.println(" Type 1 to move the cube into position and type 2 to move the cube out of position.");
}
void loop(){
userInput = Serial.read();
//Go forward
if (userInput == '1') {
Serial.println(" moving in...");
while(digitalRead(switchA) != LOW){
goForward();
}
Serial.println(" done");
}
//Go back
if (userInput == '2'){
Serial.println(" moving out...");
while(digitalRead(switchB) != LOW){
goBack();
}
Serial.println(" done");
}
userInput = 0;
}
(My code was a lot worse before, but I got a lot of help from reddit :P)
I'm pretty sure I wired the limit switches correctly as well. I have a motor shield, and I put the wires into digital pins 11 and 13 and one wire in the ground on the digital side and one wire in the ground on the power side. Here's what the wiring is.
Thank you so much for any suggestions you guys have!