Adding button press to control stepper

Discussion in 'Programmer's Corner' started by Merlin5150III, Jun 12, 2017.

  1. Merlin5150III

    Thread Starter New Member

    May 12, 2017
    2
    0
    Hi,
    I've been trying to adapt this AccelStepper example code to work with one button to control the back and forth movement of the stepper, meaning one press to go forward, one press brings it back......
    I'm not sure and overwhelmed with all the ways it can be done.

    Here's what I'm trying to work with.......

    #include <AccelStepper.h>

    AccelStepper stepper(1, 3, 4);

    int st = 1;
    int targ = 500;

    void setup()
    {
    pinMode(2, INPUT);
    stepper.setMaxSpeed(200.0);
    stepper.setAcceleration(200.0);
    }

    void loop()
    {

    if (st == 0 && digitalRead(2) == LOW)
    {
    st = 1;
    targ = -targ; stepper.moveTo(targ);
    }
    else if (stepper.distanceToGo() == 0)
    st = 0;
    }
    }
    stepper.run();
    }
    I'm confused counting the button presses, using Boolean ect.....
    Any guidance would be appreciated.
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,805
    577
    Yeah. When you can not hack someone else's code, you write your own from scratch. That way you know what is going on and can actually troubleshoot the code because you have a clue what is going on because you wrote it in the first place.
     
    spinnaker likes this.
  3. Merlin5150III

    Thread Starter New Member

    May 12, 2017
    2
    0
    Hi Shitel
    I just asked about button control options that's all.....
     
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,805
    577
    Your code has no comments that identify the functions of various pieces of the code.
    You provide no detailed description of the program.

    Your post can be summarized following way:
    -I want a button to do X.
    -I have this code.
    -It is not working.

    I am not going to waste my time trying to read your mind. You want help? You want GOOD help? Then spend your time presenting information in clear and organized manner.
     
  5. be80be

    Senior Member

    Jul 5, 2008
    476
    62
    You maybe need to study this one its a example of how accelstepper works
    Code (Text):
    1. // Blocking.pde
    2. // -*- mode: C++ -*-
    3. //
    4. // Shows how to use the blocking call runToNewPosition
    5. // Which sets a new target position and then waits until the stepper has
    6. // achieved it.
    7. //
    8. // Copyright (C) 2009 Mike McCauley
    9. // $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
    10.  
    11. #include <AccelStepper.h>
    12.  
    13. // Define a stepper and the pins it will use
    14. AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
    15.  
    16. void setup()
    17. {
    18.     stepper.setMaxSpeed(200.0);
    19.     stepper.setAcceleration(100.0);
    20. }
    21.  
    22. void loop()
    23. {
    24.     stepper.runToNewPosition(0);
    25.     stepper.runToNewPosition(500);
    26.     stepper.runToNewPosition(100);
    27.     stepper.runToNewPosition(120);
    28. }
    If button = 0 stepper.runToNewPosition(500);
    elseif button = 1 stepper.runToNewPosition(0);
     
    Last edited: Jun 17, 2017
Loading...