Controling Stepper motor with TB6560 and limit switch

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);
      }}

elpedro57:
I used your code in the program. As you can see, I modify the code in removing some part to simplified it.

I don't think you have simplified it but we can agree to differ on that point.

However I do believe your changes have made it more difficult to find the problem. Just try the code I suggested.

...R

PS ... You are of course free to ignore my advice. All I can say in my defence is that my programs work.

Is it this code you mean:

digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}

Yes Robin,
I also tried it but I always get the same problem with the speed. Impossible to get different speed in the 2 different directions.

elpedro57:
Yes Robin,
I also tried it but I always get the same problem with the speed. Impossible to get different speed in the 2 different directions.

I don't have any time today but I will look at this again tomorrow. In the meantime please post the actual program that you have uploaded to your Arduino.

...R

Thank you Robin,
I'll do somme other try today and I'll send you the last program

Hi Robin,
I worked all day on my project. I'm a bit discourage. I'm sure there isn't something big which made my program not working properly.
I send you 2 different program with each different problems.
I've put some note in the right part of each program.
I'm also surprised of how many peoples read our exchange on that project.
Thank's again

Program 1 wich is in my Arduino. This one doesn't include the pause switch. The 2 limit switch are working well with a delay of 2 sec. when I hit one or the other. Always same speed wich is in the first of the 3 parts of the program

// 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);          //    ALWAYS THIS SPEED IS SELECTED
      digitalWrite(stepPin,LOW);       //    SWITCH ARE WORKING WELL
      delayMicroseconds(500);
         }
         
    if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
         
      digitalWrite(dirPin,LOW);
      delay(2000);  
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(2000);         //       THIS SPEED NOT WORKING
      digitalWrite(stepPin,LOW);
      delayMicroseconds(2000);
      }
      
    if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
         
      digitalWrite(dirPin,HIGH);
      delay(2000);
      digitalWrite(stepPin,HIGH);
      delayMicroseconds(4000);         //       THIS SPEED NOT WORKING
      digitalWrite(stepPin,LOW);
      delayMicroseconds(4000);
      }}}

Now the second program with all your modifications. This one include the stop switch. Neither the 3 switch are working and the speed is always in the same direction and the speed is the one in the last part of the program.

// 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;
const int StopSw_Pin = 12;
char direction = 'F';  //    <----------------------NEW
unsigned long intervalBetweenSteps;

void setup() {

 Serial.begin(9600);

  pinMode(LimitSwitch_LEFT_Pin , INPUT);
  pinMode(LimitSwitch_RIGHT_Pin , INPUT);
  pinMode(StopSw_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() {

    byte leftSw  = digitalRead( LimitSwitch_LEFT_Pin);
    byte rightSw = digitalRead( LimitSwitch_RIGHT_Pin);
    byte stopSw = digitalRead(stopSw);
    
    if (leftSw == LOW and direction == 'L') { // assumes LOW when pressed
        direction = 'R';                      //      THIS SPEED NOT WORKING
        delay(2000);                          //      SWITCH DOESN'T WORK
        intervalBetweenSteps = 3000;
    }
    if (rightSw == LOW and direction == 'R') {
        direction = 'L';                       //       THIS SPEED NOT WORKING
        delay(2000);                           //       SWITCH DOESN'T WORK
        intervalBetweenSteps = 5000;
    }
    
    if (stopSw == HIGH) { // assumes HIGH when not pressed       SWITCH DOESN'T WORKING
        motorStep();
    }
 
}


void motorStep(){
    
    if (direction == 'R') {
        digitalWrite(dirPin, HIGH);
    }
    else {
        digitalWrite(dirPin, LOW);
    }

    digitalWrite(stepPin,HIGH);
    delayMicroseconds(5000); // short step pulse     ALWAYS THIS SPEED IS SELECTED
    digitalWrite(stepPin,LOW);               //      SWITCH DOESN'T WORK
    delayMicroseconds(5000);

}

elpedro57:
Now the second program with all your modifications. This one include the stop switch. Neither the 3 switch are working and the speed is always in the same direction and the speed is the one in the last part of the program.

I'm only looking at this version. Let's get that working before thinking of other options.

This code does NOT have all my code. The last 4 lines of the motorStep() function is still your code.

You don't have any Serial.print() statements in your code to show you if the buttons are being detected correctly. You are not using INPUT_PULLUP and your buttons pins may be giving spurious responses. Note that when you use INPUT_PULLUP your buttons should be wired to pull the pin to GND when pressed and digitalRead() will return LOW when pressed. The alternative is external pull-down resistors.

Temporarily put a delay(2000); as the last thing in loop() and add some code to print the values of the buttons. Don't worry about the motor for the moment.

...R

I'm not understand all your directives. Is it possible for you to help me integrate those in my program.
Thank's

elpedro57:
I'm not understand all your directives. Is it possible for you to help me integrate those in my program.

It seems to me that you just want me to write the whole program for you.

I will help you learn (assuming you want to learn) but I'm not going to write the program.

It would be perfectly respectable if you have no interest in learning Arduino programming and if you just want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay. I don't myself participate in that section.

...R

Robin,
That's not want I want. I worked on that program for multiple hours and multiple days and I want to learn. I did a lot of try. If I would leave in a town where I could have lesson on programming Arduino I'd like to get those course. But I'm leaving 800 Km of Montreal and there is no resource in my town.
Sorry

Robin,
The last 4 lines are exactly yours, on post #1 and post # 17

elpedro57:
The last 4 lines are exactly yours, on post #1 and post # 17

I am losing interest here very very quickly.

Reply #17 is irrelevant. Your latest code is in Reply #26 and these are your last 4 lines of motorStep()

    digitalWrite(stepPin,HIGH);
    delayMicroseconds(5000); // short step pulse     ALWAYS THIS SPEED IS SELECTED
    digitalWrite(stepPin,LOW);               //      SWITCH DOESN'T WORK
    delayMicroseconds(5000);

These are the last 4 lines in my proposed code in Reply #1.

digitalWrite(stepPin,HIGH);
    delayMicroseconds(10); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(intervalBetweenSteps);

How come we are still discussing something as trivial as this after 25 Replies?

...R

Sorry. I'll try this another time.

I did it and nothing work. I'll try the Gigs and Collaborations section.

Sorry for your time.

Thank's

elpedro57:
I did it and nothing work.

You have not posted the latest version of the program.

If you want to learn then don't expect instant solutions. Programming is not like that. I was not expecting it to work straight off. But I have been waiting since Reply #1 for you to test it properly and describe in detail what happens.

...R