Stepper code issues

My project hardware at the moment includes a momentary push button, L298N 25w driver module, Arduino Uno R3 and a 12v 2amp stepper motor. More buttons to be added as the project develops.

Scope of the project is to use this system to power an optical linear screw rail to pre-programmed positions by number of steps. This will eventually include multiple buttons, each set to rotate the stepper motor in a chosen direction and number of steps.

At the moment I only have one button assigned to pin 2. When pressed the motor activates. When I release the button, the rotation stops. What I want to accomplish is to press it once and the motor rotate a certain number of steps, stop and hold until another button is pressed.

EXAMPLE: Press button 1 and the motor turns CW 1000 steps then stops. Press button 2 and the motor turns CCW 1000 steps and stops.
What needs to be changed on my sketch in order to make this happen? And.... annoying as it may be to so many more profficient Arduino programmers, I am definitely new at this and currently beyond my comfort level. I want to understand what I am telling it to do, but have had very little luck finding applicable instruction that fits my hardware needs. Tons of stuff out there for controlling the unipolar kit type steppers with buttons but those lines of code only cause my stepper to growl and jitter.

My code so far:

#include <Stepper.h>
int Distance = 0;
const int stepsPerRevolution = 512;
Stepper myStepper1(stepsPerRevolution, 8, 9, 10, 11);
int Switch = 2;

void setup() {
pinMode(Switch, INPUT_PULLUP);
myStepper1.setSpeed(100);
}

void loop() {
if(digitalRead(Switch) == LOW)
{myStepper1.step(1);
}
}

Suggestions? Hints to where I may find basic instruction to complete my project successfully? If I can get this thing to move a set distance with one button, return with another button, I can take it from there in baby steps. Thanks!

To begin with, you need to look at the StateChangeDetection example sketch in the IDE, to register when the switch becomes pressed, not when it is pressed.

Hi @alisonh
whem post sketch, please use tags </>.

Look this sketch and think about.

RV mineirin

#include <Stepper.h>
int Distance = 0;
const int stepsPerRevolution = 512;
Stepper myStepper1(stepsPerRevolution, 8, 9, 10, 11);
int Switch = 2;
int switch3 = 3;



void setup() {
  pinMode(Switch, INPUT_PULLUP);
  pinMode(Switch3, INPUT_PULLUP);
  myStepper1.setSpeed(100);
}

void loop() {

  while (digitalRead(Switch) == LOW)   // while switch was pressed
  {
    if (digitalRead(Switch) == HIGH)   // If switch was released
    {
      myStepper1.step(100);     // run 100 steps
    }
  }

  while (digitalRead(Switch3) == LOW)   // while switch3 was pressed
  {
    if (digitalRead(Switch3) == HIGH)   // If switch3 was released
    {
      myStepper1.step(200);     // run 200 steps
    }
  }
}

That took me to the greater level! Now I have it running multiple buttons in multiple directions as desired! You rock. I owe you a 6 pack of your favorite flavor!

That code doesn't compile. And there is no debounce compensation.

Running on my setup just fine. Compiled and loaded without errors. Once checked I modified and added the lines I needed. I will add the debounce. This code starts the runs on release. Far less chance of bounce when releasing a spring loaded momentary button.

I doubt that. Maybe just less...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.