Go Down

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

elpedro57

Hello,

I'm trying to control my stepper motor with 2 limit switch. One for each direction. It work but I would like to get different speed for each direction. Also, I would like to have a third button to stop the movement. Maybe this has already been advised but I didn't found it. Thank you and sorry for my poor English.

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)) ){
    
        motorStep(1);

    }
    else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
          digitalWrite(dirPin,LOW);
          delay(2000);
    }
    else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
          digitalWrite(dirPin,HIGH);
          delay(2000);
    }
 
}
void motorStep( int MAX){

   for(int x = 0; x < MAX; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }
    
}


Robin2

One for each direction. It work but I would like to get different speed for each direction. Also, I would like to have a third button to stop the movement.
I think this shortened code will do the same as your code but I have not tested it.

I have also added code for a stop button and for varying the interval between steps and I have modified the motorStep() function so it only does one step as that makes the program more responsive than if it were doing, say, 100 steps without a pause.

Code: [Select]
void loop() {

    byte leftSw  = digitalRead( LimitSwitch_LEFT_Pin);
    byte rightSw = digitalRead( LimitSwitch_RIGHT_Pin);
    byte stopSw = digitalRead(stopSwitchPin);
    
    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(10); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(intervalBetweenSteps);

}



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

elpedro57

Thank you Mr. Robin,
I insert your code with mine to complete the instruction but when I check the code I have the error of direction not declared in this scope. I don't know where to insert it. 
Can you help me again,
Thanks

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;

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(10); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(intervalBetweenSteps);

}


Robin2

I insert your code with mine to complete the instruction but when I check the code I have the error of direction not declared in this scope. I don't know where to insert it. 
I had assumed that you would know how to do that as your program already has a series of constants defined at the top of the program.

Just add
Code: [Select]
char direction = 'F';
before setup()

Note that it does not use the const word because direction will change as the program runs

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

elpedro57

#4
Dec 13, 2019, 01:05 am Last Edit: Dec 13, 2019, 02:29 am by elpedro57
Hello Mr Robin,

Thank you very much for your help. I'm not able to complete my code with your instructions and i'm sure they are good. I'm a newbie with Arduino. Is it possible for you to complete my code and also if you can put some value for the speed wich I'll be able to modify later.
When my project will be done I'll send you picture and small video.
Thank you
Pierre

Robin2

Thank you very much for your help. I'm not able to complete my code with your instructions and i'm sure they are good. I'm a newbie with Arduino.
Please post the program that represents your best attempt and I will try to help.

I have been assuming that you wrote the program in your Original Post ?

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

elpedro57

Thank's a lot Mr. Robin. Here's my 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;


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(10); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(intervalBetweenSteps);

}


JCA34F

See if this helps, connect the button and switches between GND and input pins, then Change :
Code: [Select]

  pinMode(LimitSwitch_LEFT_Pin , INPUT);
  pinMode(LimitSwitch_RIGHT_Pin , INPUT);
  pinMode(StopSw_Pin , INPUT);

To:
Code: [Select]

  pinMode(LimitSwitch_LEFT_Pin , INPUT_PULLUP);
  pinMode(LimitSwitch_RIGHT_Pin , INPUT_PULLUP);
  pinMode(StopSw_Pin , INPUT_PULLUP);

Robin2

Thank's a lot Mr. Robin. Here's my program
You seem to have made no attempt to apply my suggestion in Reply #3. I had been hoping you would make some effort.

What I have in mind is this

Code: [Select]
const int StopSw_Pin = 12;
char direction = 'F';  //    <----------------------NEW

void setup() {

 Serial.begin(9600);



For the future please post any error messages that you get when you try to compile a program - as well as the program that gives rise to the error.

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

elpedro57

Mr. Robin,
Sorry for my mistake. I add your command line and now this is what I get. I have the following error message when I verify the program:


Arduino:1.8.8 (Windows 10), Carte : "Arduino/Genuino Uno"

C:\Users\Pierre\Documents\Arduino\TB6560_avec_Arduino_Reference\TB6560_avec_Arduino_Reference.ino: In function 'void loop()':

TB6560_avec_Arduino_Reference:40:9: error: 'intervalBetweenSteps' was not declared in this scope

         intervalBetweenSteps = 1000;

         ^

TB6560_avec_Arduino_Reference:45:9: error: 'intervalBetweenSteps' was not declared in this scope

         intervalBetweenSteps = 2000;

         ^

exit status 1
'intervalBetweenSteps' was not declared in this scope


Now my code with your instruction include:

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

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(10); // short step pulse
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);

}

Robin2

You can solve that problem in much the same way as you solved the earlier problem. But your datatype should be unsigned long as in
Code: [Select]
unsigned long intervalBetweenSteps;


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

elpedro57

Mr. Robin,
As I'm not able to reach what I want to do, I think I should do it from the beginning. My program include is ok for running the motor back and forth with the 2 limit switch with a stop of 2 seconds when I hit the buttons. The only problem is that I can't control the speed for each direction. I need faster in one direction. I'm a bit contraint to ask you again but you're my best reference. When I'll fix this part, I'll try to add myself the other feature I need.
Thank you again

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)) ){
   
        motorStep(1);

    }
    else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
          digitalWrite(dirPin,LOW);
          delay(2000);   
     }
    else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
          digitalWrite(dirPin,HIGH);
          delay(2000);
     }}
     
      void motorStep( int MAX){

   //  for(int x = 0; x < MAX; x++) {
   {   digitalWrite(stepPin,HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin,LOW);
      delayMicroseconds(2000);
     }
     }

Robin2

The only problem is that I can't control the speed for each direction
That is one of the features I included in the code in my Reply #1

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

elpedro57

Mr. Robin,
Is it possible for you to integrate your post #1 to my program.
Thank you again.

Robin2

Mr. Robin,
Is it possible for you to integrate your post #1 to my program.
First off, "Robin" is sufficient - please drop the "Mr."

I gave you the code for two functions in Reply #1 and my idea was (and still is) that you should replace the code for those functions in your program with my functions and add any necessary definitions for variables at the top of your program. I have also given you advice about how to define the variables.

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 do programming for pay.

...R


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

Go Up