element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Controlling stepper direction with a button
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 16 replies
  • Subscribers 404 subscribers
  • Views 2443 views
  • Users 0 members are here
Related

Controlling stepper direction with a button

Former Member
Former Member over 12 years ago

I am trying to control a unipolar stepper with two buttons, one on either side of a salvaged typwriter cage. The sketch will run it one one direction but the

button does not reverse the motor. Thanks for advise.

 

#include <Stepper.h>

Stepper s(20,8,9,10,11);

int button1 = 1;

int button2 = 2;

int buttonState1 = 0;

int buttonState2 = 0;

 

 

void setup()

{

pinMode(1,INPUT); //buttons

pinMode(2,INPUT);

 

pinMode(8,OUTPUT);

pinMode(9,OUTPUT);

pinMode(10,OUTPUT);

pinMode(11,OUTPUT);

 

 

s.setSpeed(500);

s.step(1500); // This is well beyond the max range but I assumed that when the carriage hit the button, it would reverse.   

//delay(5000);

}

 

 

void loop()

{

buttonState1 = digitalRead(button1);

buttonState2 = digitalRead(button2);

 

 

if (buttonState1 == HIGH)

{

s.setSpeed(-500);

s.step(1500);//200

//delay(1000);

}

 

 

if (buttonState2 == HIGH)

{

s.setSpeed(500);

s.step(-1500);

delay(1000);

}

 

 

 

 

}

  • Sign in to reply
  • Cancel
Parents
  • Former Member
    Former Member over 12 years ago

    I tried another approach but the carriage will only move as far as "stepsPerRevolution" will permit. The mechanism must move freely from one button to the next. The far left blue pressure button can be seen in this photo. http://www.flickr.com/photos/50454200@N06/8508893686/

     

     

     

    #include <Stepper.h>

     

     

    int button1 = 1;

    int button2 = 2;

    int buttonState1 = 0;

    int buttonState2 = 0;

     

     

    const int stepsPerRevolution = 100; 

     

     

    Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

     

     

    void setup() {

      pinMode (button1,INPUT);

      pinMode (button2,INPUT);

      // set the speed at 60 rpm:

      myStepper.setSpeed(80);

      myStepper.step(stepsPerRevolution);

      //delay(500);

      

    }

    void loop() {

      buttonState1 = digitalRead (button1);

      buttonState2 = digitalRead (button2);

      if(buttonState2 == HIGH)

      {

      // step one revolution  in one direction:

      myStepper.step(stepsPerRevolution);

      //delay(500);

      }

     

      if (buttonState1 == HIGH)

      {

       // step one revolution in the other direction:

      myStepper.step(-stepsPerRevolution);

      //delay(500);

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Former Member
    Former Member over 12 years ago

    I tried another approach but the carriage will only move as far as "stepsPerRevolution" will permit. The mechanism must move freely from one button to the next. The far left blue pressure button can be seen in this photo. http://www.flickr.com/photos/50454200@N06/8508893686/

     

     

     

    #include <Stepper.h>

     

     

    int button1 = 1;

    int button2 = 2;

    int buttonState1 = 0;

    int buttonState2 = 0;

     

     

    const int stepsPerRevolution = 100; 

     

     

    Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

     

     

    void setup() {

      pinMode (button1,INPUT);

      pinMode (button2,INPUT);

      // set the speed at 60 rpm:

      myStepper.setSpeed(80);

      myStepper.step(stepsPerRevolution);

      //delay(500);

      

    }

    void loop() {

      buttonState1 = digitalRead (button1);

      buttonState2 = digitalRead (button2);

      if(buttonState2 == HIGH)

      {

      // step one revolution  in one direction:

      myStepper.step(stepsPerRevolution);

      //delay(500);

      }

     

      if (buttonState1 == HIGH)

      {

       // step one revolution in the other direction:

      myStepper.step(-stepsPerRevolution);

      //delay(500);

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • colecago
    colecago over 12 years ago in reply to Former Member

    Are you trying to get this to run automatically from side to side using those blue buttons as limits?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to colecago

    Keith

    If I'm not mistaken you have a button1 on Pin 1 which is the Tx.

     

    While its possible to use this pin, you should try to leave it available for debugging.

     

    Your photo is not a lot of help to see how you have wired the buttons.

     

    Are you switching 5v onto the pin, or pulling the pin to ground.?

    If you are pulling the pin to ground you need to have the pullups activated by adding

    digitalWrite (button1, HIGH);

    digitalWrite (button2, HIGH);

    or use external pullup resistors.

     

    I would suggest shifting the button pins, and adding some debugging output

     

    #include <Stepper.h>

     

    int button1 = 1;

    int button2 = 2;

    int buttonState1 = 0;

    int buttonState2 = 0;

     

    const int stepsPerRevolution = 100; 

     

    Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

     

    void setup() {

      Serial.begin(9600);

      pinMode (button2,INPUT);               //shift to pin 2

      pinMode (button3,INPUT);               //shift to pin 3

      // set the speed at 60 rpm:

      myStepper.setSpeed(80);

      myStepper.step(stepsPerRevolution);

      //delay(500);

      

    }

    void loop() {

      buttonState1 = digitalRead (button1);

      buttonState2 = digitalRead (button2);

      if(buttonState2 == HIGH)

      {

          Serial.println("button2 detected");               //add this to see if the button is detected

      // step one revolution  in one direction:

      myStepper.step(stepsPerRevolution);

      //delay(500);

      }

     

      if (buttonState1 == HIGH)

      {

          Serial.println("button1 detected");            //add this to see if the button is detected

       // step one revolution in the other direction:

      myStepper.step(-stepsPerRevolution);

      //delay(500);

      }

    }

     

     

     

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to mcb1

    Mark,

       The buttons will stop the motor, but not reverse it.

     

    Keith

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to mcb1

    Mark,

         The button is detected until it is pressed, then stops until released.

     

    Keith

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to mcb1

    The buttons are working if continually press, but I need a momentary switch

    to reverse the motor until it hits the opposite switch, etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Blacksheep32
    Blacksheep32 over 12 years ago in reply to Former Member

    Or you need a relay that activates/deactivates every time a button is pressed?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • colecago
    colecago over 12 years ago in reply to Former Member

    So the buttons are high when not pressed? 

     

    try these changes.  Be aware that button1 and button2 might be backwards

     

    #include <Stepper.h>

     

    int button1 = 1;

    int button2 = 2;

    int buttonState1 = 0;

    int buttonState2 = 0;

    int direction = 0;

     

    const int stepsPerRevolution = 100;

     

    Stepper myStepper(stepsPerRevolution, 8,9,10,11);          

     

    void setup() {

      Serial.begin(9600);

      pinMode (button2,INPUT);               //shift to pin 2

      pinMode (button3,INPUT);               //shift to pin 3

      // set the speed at 60 rpm:

      myStepper.setSpeed(80);

      myStepper.step(stepsPerRevolution);

      //delay(500);

     

    }

    void loop() {

      buttonState1 = digitalRead (button1);

      buttonState2 = digitalRead (button2);

       if (direction == 0){

         if (buttonState2 == LOW)

            direction = 1;

         else

             myStepper.step(stepsPerRevolution);    

    }

    else{

        if (buttonState1 == LOW)

           direction = 0;

         else

    myStepper.step(-stepsPerRevolution);    

    }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Blacksheep32

        This sketch "works" sometimes, but then has a mind of its own! The button input is not responding quickly and is a little sluggish.

     

    #include <Stepper.h>

     

     

      int button1 = 2;

      int button2 = 3;

      int stepCount=0;

      int buttonState1 = 0;

      int buttonState2 = 0;

      const int stepsPerRevolution = 100;

     

     

      Stepper myStepper(stepsPerRevolution, 8,9,10,11);          

     

     

    void setup() {

     

     

      Serial.begin(9600);

      pinMode (button1,INPUT);               //shift to pin 2

      pinMode (button2,INPUT);               //shift to pin 3

      myStepper.setSpeed(30);

     

     

      myStepper.step(1);

      //delay(500);

      digitalWrite (button1, LOW);

      digitalWrite (button2, LOW);

    }

     

     

    void loop() {

      buttonState1 = digitalRead (button1);

      buttonState2 = digitalRead (button2);

     

     

      if (buttonState1 == HIGH)

      {

      stepCount ++;

      }

      if (stepCount >1)

      {               

      myStepper.step(stepsPerRevolution);//stepsPerRevolution

      }

      if (buttonState2 == HIGH)

      {

        stepCount --;

      }

      if (stepCount  < 1)

      {          

      myStepper.step(-stepsPerRevolution);

      }

      Serial.println(stepCount);

    }

     

     


    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to mcb1

    Mark,

        This sketch "works" sometimes, but then has a mind of its own! The

    button input is not responding quickly and is a little sluggish.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 12 years ago in reply to Former Member

    Sorry Keith

     

    I've been tied up with something else, that had to get done.

     

    Apologies the sketch I posted, I forgot to change the 'int button' to the new pins, but I see above you've corrected that.

     

    If I understand correctly the stepper library will go away and do its thing, and come back when its finished.

     

    Imagine sending 'someone' down the road to the corner.

    If you want to tell them to stop, they won't resond until they are there at the corner.

     

    If however you told them to take ten steps and wait for further instructions....then they would respond a lot quicker.

     

    This is what your sketch is effectively doing

     

     

    However the buutons are troubling me...

    I understand that when you press a button, the pin goes high (+6v)  .... should be 5v

    If the button is a momentary button it should go to zero when released ...

    (You have not mentioned if you have external resistors that pull it down ...if not add a 220, 1K or 10K between pin2 and ground, and pin3 and ground.)

     

    In this case, your sketch would make it go one revolution only, not all the way to the other end.

     

    So I think we need to sort out your buttons first.

    Can you add a LED (WITH A SERIES RESISTOR!!) between Pin 4 and ground and Pin 5 and ground.

    and alter your sketch to add :-

     

    in void setup()


      int LedPin1 = 4;

      int LedPin2 = 5;

      pinMode(LedPin1, OUTPUT);

      pinMode(ledPin2, OUTPUT);

     

     

    then in void loop() ....

     

      if (buttonState1 == HIGH)

      {

      DigitalWrite(Ledpin1, HIGH);

      stepCount =2;               //This resets to a known state rather than continuing to count while the button is HIGH

      }

      else

      {

           DigitalWrite(Ledpin1, LOW);

      }

     

      if (stepCount >1)

      {               

          myStepper.step(stepsPerRevolution);     //stepsPerRevolution

      }

      if (buttonState2 == HIGH)

      {

         DigitalWrite(Ledpin2, HIGH);

         stepCount =-2;      //This resets to a known state rather than subtracting while the button is HIGH

      }

      else

      {

         DigitalWrite(Ledpin2, LOW);

      }

      if (stepCount  < 1)

      {          

         myStepper.step(-stepsPerRevolution);

      }

      Serial.println(stepCount);

    }

     

     

    Now the led should light when the button is pressed.

    I suspect your problems are due to the input pin 'floating', but the resistor and led should tell you this quickly.

    (You don't need to hook the stepper up for this test....)

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube