Pages: [1]   Go Down
Author Topic: I am attempting to program a bipolar stepper motor with direction switch.  (Read 634 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone.
I am attempting to program a standa stage which uses a bipolar stepper motor. The motor must change direction once it reaches the end of the stage, the end is indicated by a push button. However it doesn't change direction. Could you please verify whether or not my code is correct?
Code:

#include <Stepper.h>
#define BUTTON1 4
#define BUTTON2 5
#define BUZZER 1
#define LED 7
 

 
 int moveleft = 0;
 int moveright = 1;

 const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
                          // for your motor
 int maxsteps = 250000;
 
 // initialize the stepper library on the motor shield
 Stepper myStepper(stepsPerRevolution, 12,13);     
 
 // give the motor control pins names:
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
 
 int x = 0;
void setup() {
 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins  // can be used to control the motor:
 pinMode(BUTTON1, INPUT);
 pinMode(BUTTON2, INPUT);
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 pinMode(LED, OUTPUT);
 
 // initialize the serial port:
 Serial.begin(9600);
 
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(200);
 }

 
 void loop() {
   
   myStepper.step(48);
   delay(20);
   if (digitalRead(BUTTON1)){
     digitalWrite(LED, HIGH);
   }
 
   
     while (moveleft, HIGH){
       myStepper.step(48);
       delay(20);
       if (digitalRead(BUTTON1) && !digitalRead(BUTTON2)){
     moveleft = HIGH;
     moveright = LOW;
   }
       }
       while (moveright, HIGH){
         myStepper.step(-48);
         delay(20);
         if (digitalRead(BUTTON2) && !digitalRead(BUTTON2)){
     moveright = HIGH;
     moveleft = LOW;
   }

         
   }
 }
 
 

note that the buzzer isn't currently in use. The only other thing that could explain this problem is that the buttons are broken.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The only other thing that could explain this problem is that the buttons are broken.
Or incorrectly wired. I'm leaning towards incorrectly wired. How are they wired?

You are not using the internal pullup resistor. Why not?

Not using it means that you need to have an external pullup or pulldown resistor. Do you?

Code:
int moveleft = 0;
 int moveright = 1;
This is silly. You can only move left or right. It is meaningless to be able to move both directions at the same time. That being the case, you should have a boolean variable, movingLeft that is valued true (when moving left) or false (when moving right), not two variables.

Code:
int maxsteps = 250000;
Perhaps you should take a peak at the values that fit in an int. Hint: This one doesn't.

Code:
     while (moveleft, HIGH){
No clue what you are trying to do here, but this isn't right. The comma operator is distinctly different from the == operator.

Code:
       if (digitalRead(BUTTON1) && !digitalRead(BUTTON2)){
     moveleft = HIGH;
     moveright = LOW;
   }
The digitalRead() function does not return true or false. You should use explicit, not implicit comparisons here. Primarily because when you wire the switches the easy way, LOW (0) means pressed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Firstly thank you for replying. Secondly I forgot that the max steps was in there. It is not being used and I just chose a random value for it. I don't know whether the motor needs a pullup or pulldown resistor and I am currently leaning towards the switches being broken too. I don't think that they ever switch off. And I like the new programming ideas that you have given me. So with this comes an overall Thank you.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 626
Posts: 34168
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is not the motor that needs a pull up resistor but the switch.
Just write a simple sketch that lights an LED when it detects the switch and push it by hand.
Asking again, how is your switch wired. It should be between the digital input and ground, with a resistor between the digital input and +5V, or enable the internal pull up resistors.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there are 2 switches with a common GND/Vin. so there are 3 wires. I have written a script for an LED but the light is constantly on, which is why I think that the switch may be broken. I will attempt your wiring suggestions.
Logged

Pages: [1]   Go Up
Jump to: