Go Down

Topic: Controling Stepper motor with TB6560 and limit switch (Read 2666 times) previous topic - next topic

elpedro57


elpedro57

Hi Robin,
I think I follow all your directives. Everything is include. The motor is running always in the same direction. Can you have a look on my program again. The onlky way to change the speed is with the bottom part where I put 5000. Not sure if I include the (char direction = 'F'; and the unsigned long intervalBetweenSteps;) instructions at the good place in the program. No errors when verify the program.
Thank you

Code: [Select]



// 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';
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';
        delay(2000);
        intervalBetweenSteps = 1000;
    }
    if (rightSw == LOW and direction == 'R') {
        direction = 'L';
        delay(2000);
        intervalBetweenSteps = 2000;
    }
   
    if (stopSw == HIGH) { // assumes HIGH when not pressed
        motorStep();
    }
 
}


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

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

}


Robin2

How is this code like what I suggested?
Code: [Select]
digitalWrite(stepPin,HIGH);
    delayMicroseconds(5000); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(5000);


Maybe there is a good reason why you have not followed my suggestion and if so please explain why.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

elpedro57

Hi Robin,
Your code is ok. Motor is running but the limit switch are non functionnal. When I change the value of 5000, the speed change. This is ok but it seems that the program doesn't go to that part to reverse with the limit switch and change the speed itself with the value for cw and ccw ( L & R )

Thank you,

This is the part I'm talking about:

Code: [Select]


if (leftSw == LOW and direction == 'L') { // assumes LOW when pressed
        direction = 'R';
        delay(2000);
        intervalBetweenSteps = 1000;
    }
    if (rightSw == LOW and direction == 'R') {
        direction = 'L';
        delay(2000);
        intervalBetweenSteps = 2000;
    }
   
    if (stopSw == HIGH) { // assumes HIGH when not pressed
        motorStep();

Robin2

Your code is ok. Motor is running but the limit switch are non functionnal. When I change the value of 5000, the speed change.
You have not explained why you have not used my code in your motorStep() function. It ought to change the speed as well as the direction. If neither is happening then it will help us to identify the problem.

Try adding some Serial.print() statements to print the values of direction and intervalBetweenSteps in the two IF blocks where they are changed.

You can't expect to debug a program without examining what it is doing.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

elpedro57

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:

 
Code: [Select]


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

Robin2

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.
Two or three hours spent thinking and reading documentation solves most programming problems.

elpedro57

Is it this code you mean:

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

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

elpedro57

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

elpedro57

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

Code: [Select]


// 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.

Code: [Select]


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

}



Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

elpedro57

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

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up