Hello everyone I'm working on a project to basically build a pick and place system. At the end of each axes I have a limit switch to stop the pick and place moving when hit. I have wired up the limit switches correctly and I know they work correctly. The code should be pretty simple but for some reason it's not working to stop the motors when the switches are pressed. I am also interfacing Arduino with Matlab so I'm not sure if that would be causing any problems.
There's too much code to post the whole thing so I'll just show what I'm working with:
AccelStepper stm1(1,11,10); // stepper motor 1 declared
void setup() {
pinMode(6,INPUT); // limit switch 2
pinMode(5,INPUT); // limitswitch 1
}
if (stm==1) { // if stepper motor 1 is called from matlab
if ((digitalRead(5) ==1) && (digitalRead(6) ==1)){ //if switches not pressed motor runs where it has to as told my Matlab
stm1.moveTo(val2dir90); //Changed here
if ((stm1.distanceToGo() != 0)) {
stm1.runToNewPosition(val2*90);
stm1.run();
}
}
else if ((digitalRead(5) ==0) || (digitalRead(6) ==0)) { //if either switch is pressed
stm1.setSpeed(0);
}
}
So basically I'm telling the motors to move from Matlab which works fine but the code for the limit switches doesn't work.
Limit switches are hooked up to pins 5 and 6 so while its not pressed the motor should go where it has to go but if either one is pressed it should stop the motor.
I've tried many different things along these lines but all that happens is the motors just run and when I press the button nothing happens.
If anyone would be able to help me in any way I'd appreciate it so much thank you!