Adding button press to control stepper

Thread Starter

Merlin5150III

Joined May 12, 2017
2
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.
 

shteii01

Joined Feb 19, 2010
4,644
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.
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.
 

shteii01

Joined Feb 19, 2010
4,644
Hi Shitel
I just asked about button control options that's all.....
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.
 

be80be

Joined Jul 5, 2008
2,051
You maybe need to study this one its a example of how accelstepper works
Code:
// Blocking.pde
// -*- mode: C++ -*-
//
// Shows how to use the blocking call runToNewPosition
// Which sets a new target position and then waits until the stepper has
// achieved it.
//
// Copyright (C) 2009 Mike McCauley
// $Id: Blocking.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{
    stepper.setMaxSpeed(200.0);
    stepper.setAcceleration(100.0);
}

void loop()
{
    stepper.runToNewPosition(0);
    stepper.runToNewPosition(500);
    stepper.runToNewPosition(100);
    stepper.runToNewPosition(120);
}
If button = 0 stepper.runToNewPosition(500);
elseif button = 1 stepper.runToNewPosition(0);
 
Last edited:
Top