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
  • colecago
    colecago over 12 years ago

    I'm not very familiar with that library, but apparently speed is supposed to be a positive number and just steps should change between pos and neg.  Also, step is a blocking call, it will step until it is done stepping then return to your code.

     

    Keith Gum wrote:

     

    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);

    }

     

     

     

     

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • 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
  • 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

    Mark,

       When I put the meter to the button pin and press, it goes to 6v. I'll try your sketch this morning. Thanks.

     

    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 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
>
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