I have a simple code that runs CW and then CCW but how can incorporate 2 limit switches in the code so code moves CW once it hits one switch it then goes CCW and if then runs into the other switch now goes CW and so on back and forth between the switches. Any suggestions would be greatly appreciated!
const int stepPin = 7;
const int dirPin = 6;
const int enPin = 5;
int T=100; // Motor speed
void setup() {
Serial.begin(9600);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
}
digitalWrite(enPin,HIGH); //when HIGH powers motor on
Serial.write(T);
digitalWrite(dirPin,LOW); // //Changes the rotations direction CW
for(int x = 0; x < 1000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(T);
digitalWrite(stepPin,LOW);
delayMicroseconds(T);
}
digitalWrite(dirPin,HIGH); //Changes the rotations direction CCW
for(int x = 0; x < 1000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(T);
digitalWrite(stepPin,LOW);
delayMicroseconds(T);
}
}
You need a variable to keep track of the direction. When a limit switch is triggered the variable should be changed.
Rather than use a FOR loop for the steps just allow the loop() function to look after the repetition. Between each step check the state of the limit switches and update the direction variable if appropriate.
Could you possibly give a code scenario of your idea and then I can for sure understand better and continue to finish it. Thanks for your advice/knowledge as well!
void loop() {
leftSwitchState = digitalRead(leftSwitchPin);
rightSwitchState = digitalRead(rightSwitchPin);
if (leftSwitchState == LOW) { // assumes LOW when pressed
direction = 'R';
}
if (rightSwitchState == LOW) {
direction = 'L';
}
if (direction == 'R') {
set stepper driver direction to move right
}
else {
set driver to move left
}
code to move one step
}
I tried to follow in your steps but I was not getting any detection from the limit switches.(Not sure why the logic makes sense.) I made some changes I now can get the motor to spin with switch but only when hit and only CW how can I have it rotate until it hits a switch and then reverse. I tried to mess with the logic a bit but got nowhere. Any ideas to any changes?
// defines pins numbers
# define stepPin 5
# define dirPin 4
# define enPin 3
#define leftSwitchPin 10
#define rightSwitchPin 11
int T=200; // Motor speed
int leftSwitchState;
int rightSwitchState;
void rotateCW();
void rotateCCW();
void setup() {
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
pinMode(leftSwitchPin,INPUT);
pinMode(rightSwitchPin,INPUT);
leftSwitchState = digitalRead(leftSwitchPin);
}
void loop() {
rightSwitchState = digitalRead(leftSwitchPin);
if (leftSwitchState != rightSwitchState ){
if (digitalRead(rightSwitchPin) != leftSwitchState){
rotateCW();
}
else {
rotateCCW();
}
}}
void rotateCW(){
digitalWrite(enPin,HIGH);
digitalWrite(dirPin,LOW); // HIGH Enables the motor to move in a particular direction CW
for(int x = 0; x < 1000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(T);
digitalWrite(stepPin,LOW);
delayMicroseconds(T);
}
}
void rotateCCW(){
digitalWrite(enPin,HIGH);
digitalWrite(dirPin,HIGH); //Changes the rotations direction CCW
for(int x = 0; x < 1000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(T);
digitalWrite(stepPin,LOW);
delayMicroseconds(T);
}
}
Do you intend for the motor to start rotation immediately upon power up? or do one of the switches need to detect something before the motor begins to move?
@SandFarmer ---- I would like for the motor to run CW up until a switch is detected and then once first switch is detected rotate opposite of what it was and so on between the 2 switches. If you have any ideas or advice on corrections on my code, I would appreciate it. Thanks
Maybe the right question is what happens if your motor is turning in one direction, comes up against an nonfunctioning limit switch - i.e. the motor keeps trying to drive in that direction. Does what the motor is turning come up against a hard stop?
No matter - I was also wondering how your motor is going to be wired up. you have a dirPin and an enPin. What did you intend those to do? are they triggering a relay of some sort to reverse direction?