I used your code in the program. As you can see, I modify the code in removing some part to simplified it. The motos is running well in both direction controlled by the limit switch but the speed is always the one specified in the first part of the program, In this case 500. I tried a lot of different way to program but it's impossible to find the way to reach a different speed. I surely forget something to be able to get the speed 1000 in the second and in the third part of the program.
Here's my code:
// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
const int LimitSwitch_LEFT_Pin = 10;
const int LimitSwitch_RIGHT_Pin = 11;
void setup() {
Serial.begin(9600);
pinMode(LimitSwitch_LEFT_Pin , INPUT);
pinMode(LimitSwitch_RIGHT_Pin , INPUT);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
// Set Dir to Home switch
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
}
void loop() {
int leftSw = digitalRead( LimitSwitch_LEFT_Pin);
int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);
if( (leftSw == HIGH && (digitalRead(dirPin) == HIGH)) ||
(rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
digitalWrite(dirPin,LOW);
delay(2000);
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
digitalWrite(dirPin,HIGH);
delay(2000);
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}}