Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: elpedro57 on Dec 12, 2019, 06:04 pm

Title: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 12, 2019, 06:04 pm
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);
      }
    
}

Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 12, 2019, 07:35 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 12, 2019, 09:36 pm
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);

}

Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 12, 2019, 10:14 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 13, 2019, 01:05 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 13, 2019, 10:39 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 13, 2019, 02:33 pm
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);

}

Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: JCA34F on Dec 13, 2019, 03:00 pm
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);
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 13, 2019, 03:28 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 13, 2019, 03:43 pm
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);

}
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 13, 2019, 06:41 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 13, 2019, 08:18 pm
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);
     }
     }
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 14, 2019, 12:05 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 14, 2019, 03:23 am
Mr. Robin,
Is it possible for you to integrate your post #1 to my program.
Thank you again.
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 14, 2019, 09:21 am
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


Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 14, 2019, 02:43 pm
OK Robin,
I'll try again.
Thank you
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 14, 2019, 10:04 pm
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);

}

Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 15, 2019, 11:02 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 15, 2019, 04:12 pm
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();
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 15, 2019, 07:13 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 15, 2019, 09:42 pm
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);
      }}
     
 
     
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 15, 2019, 09:48 pm
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.
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 15, 2019, 09:59 pm
Is it this code you mean:

digitalWrite(stepPin,HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin,LOW);
      delayMicroseconds(500);
         }
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 15, 2019, 10:05 pm
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.
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 16, 2019, 08:59 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 16, 2019, 03:23 pm
Thank you Robin,
I'll do somme other try today and I'll send you the last program
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 16, 2019, 10:13 pm
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);

}


Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 17, 2019, 10:57 am
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 17, 2019, 02:35 pm
I'm not understand all your directives. Is it possible for you to help me integrate those in my program.
Thank's
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 17, 2019, 02:54 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 17, 2019, 06:30 pm
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 17, 2019, 06:57 pm
Robin,
The last 4 lines are exactly yours, on post #1 and post # 17
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 17, 2019, 07:13 pm
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()
Code: [Select]
    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.
Code: [Select]
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
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 17, 2019, 07:43 pm
Sorry. I'll try this another time.
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: elpedro57 on Dec 17, 2019, 09:53 pm
I did it and nothing work. I'll try the Gigs and Collaborations section.

Sorry for your time.

Thank's
Title: Re: Controling Stepper motor with TB6560 and limit switch
Post by: Robin2 on Dec 17, 2019, 10:52 pm
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