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:
// 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);
}
}
elpedro57:
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.
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);
}
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
// 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);
}
elpedro57:
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
char direction = 'F';
before setup()
Note that it does not use the const word because direction will change as the program runs
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
elpedro57:
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 ?
// 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);
}
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
// 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);
}
}
elpedro57:
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.
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
// 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);
}
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:
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();
elpedro57:
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.