Push Button to Move Stepper CW, CCW then stop

My arduino experience to this point has been programming the steps/rev and speeds etc. for my small cnc machines which are controlled by the conputer with a grbl sending program. I haven't tried to write a program internal to the arduino.

I've been on the Dronebotworkshop site trying to figure out how to do this. he has 2 great videos that i'm trying to combine into one. obviously, his code works independently but when i try to combine the parts i think are necessary, well it doesn't even build.

He has a video which shows how to set the motor to do a cw then ccw then cw cycle but it seems it's run off the reset button or power on.
He also has a video for using a speed pot to control the motor.

I'm trying to combine these videos to do something very simple. When a button is pressed, the motor does X revolutions cw then the same number of revolutions CCW and stops. when the button is pressed again, it goes through this cycle again.

bipolar stepper
L298N driver

seems incredibly simple on the surface but apparently i'm incapable of combining code.

any help is greatly appreciated.

What have you tried so far? Without posting your code (use code tags, please) nobody knows what you have tried. If you want to react to a button, look at the State Change Detection example in the IDE (File->Examples->02.Digital->StateChangeDetection) since you want to do something when a button is pressed.

How is your button wired up? The typical method is to connect one side to ground and one side to an arduino pin. You then declare that pin as INPUT_PULLUP. This makes a button press read LOW (connected to ground) and when not pressed, it reads HIGH.

After that code works, add in some of the code to get your motor to step N times one direction. When that works, add code to make it go the other way after that.

Sorry, i haven't posted here before and didn't know how to add code

i checked out the example, it shows how to power an LED. with the stepper having 4 inputs compared to the LED 1, do i just replace the "13" with the 4 motor pins?

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

here's the code i'm using for the motor.
first problem is it bypasses the speed pot completely.
second problem is it doesn't reverse direction. this code is from a unipolar motor and i'm sure i have to switch something for it to convert into ccw for the bipolar

   // read the sensor value:
  int sensorReading = analogRead(SPEED_CONTROL);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(STEPS_PER_REV / 100);
  }
    // Slow - 4-step CW sequence to observe lights on driver board
  stepper_NEMA17.setSpeed(100);    
  StepsRequired  =  400;
  stepper_NEMA17.step(StepsRequired);
  delay(100);
 
   // Rotate CW 1/2 turn slowly
  StepsRequired  =  STEPS_PER_REV / 2; 
  stepper_NEMA17.setSpeed(100);   
  stepper_NEMA17.step(StepsRequired);
  delay(100);
  
  // Rotate CCW 1/2 turn quickly
  StepsRequired  =  - STEPS_PER_REV / 2;   
  stepper_NEMA17.setSpeed(700);  
  stepper_NEMA17.step(StepsRequired);
  delay(100);
}

I guess your knowledge about programming has a lot of room for improving it.
Do you know what

void setup()

and

void loop()

are for?
if yes write a few sentences how you would explain it.

best regards Stefan

i don't. i've no knowledge of programming at all.
i've just put what's in there in another program trying to keep them similar.

i was just copying out the sections that are relevant. so i tried putting my 8 input pins in the 'ledPIN' variable and it had an error. if that's sending power to turn something on, i don't know how to integrate that into the motor power.

here's the whole code, i guess.

/*
  Stepper Motor Demonstration 3
  Stepper-Demo3.ino
  Demonstrates NEMA 17 Bipolar Stepper with L298N Driver
  Uses Potentiometer on Analog Input A0
  Uses Arduino Stepper Library
 
  DroneBot Workshop 2018 
  https://dronebotworkshop.com
*/
 
// Include the Arduino Stepper Library
#include <Stepper.h>
 
// Define Constants
// Number of Steps Required
int StepsRequired;
// Number of steps per output rotation
const int STEPS_PER_REV = 200;
const int SPEED_CONTROL = A0;
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 8;       // the pin that the led is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11 
// Connected to L298N Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-2-3-4 for proper step sequencing
 
Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);
 
void setup() {
// initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop() {
    // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
    // read the sensor value:
  int sensorReading = analogRead(SPEED_CONTROL);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(STEPS_PER_REV / 100);
  }
    // Slow - 4-step CW sequence to observe lights on driver board
  stepper_NEMA17.setSpeed(100);    
  StepsRequired  =  400;
  stepper_NEMA17.step(StepsRequired);
  delay(100);
 
   // Rotate CW 1/2 turn slowly
  StepsRequired  =  STEPS_PER_REV / 2; 
  stepper_NEMA17.setSpeed(100);   
  stepper_NEMA17.step(StepsRequired);
  delay(100);
  
  // Rotate CCW 1/2 turn quickly
  StepsRequired  =  - STEPS_PER_REV / 2;   
  stepper_NEMA17.setSpeed(700);  
  stepper_NEMA17.step(StepsRequired);
  delay(100);
}

Few questions:

  1. Do you want to use the analog to control the motor speed?
  2. If not how fast do you want the motor to turn?
  3. How do you want to specify the number of rotations?
  4. Do you want to be able to cancel the operation with the button?

Hi Skant153,

I try to write an allday analogon about how programming works:
(OK in some parts not allaydays example)

see post below your last post

i was going to use the pot for testing and then try and program a speed so it cannot be changed without editing the program. so i guess the answer to your first two questions is no, i don't want a pot in the final product but i don't know the speed i wish to go yet but probably 20-30 rpm.

as for number of rotations, i don't know the different ways to specify it so i also can't answer that.

4 - that's a really good idea. yeah, i'd like to incorporate that.

imagine an extraterrestrial energy being leaving his blackhole-pure-energy-area down to earth and watching
a carpenter using a cordless screwdriver.

The extraterrestrial energy being's first conclusion: OK the handhold-shaped thing is able to connect these flat things by holding it close to it and pull the trigger.
Imagine this extraterrestrial energy being finds a hand-gun. Concluding Oh fine now I have my own flat things connector
hold it close to the flat thing and pull the trigger.... hm seems to be a different kind of handhold-shape thing very noisy and makes the flat thing jump away instead of connecting.

So somebody is epxlaining it has to be this kind of "handhold-shaped thing" a cordless screwdriver energy-beeing ah now I know OK give it to me I want to try it. OK makes the same kind of sound as yours but the flat things just don't connect !? why?

carpenter you need to use screws. energy being "ah OK I understand where do I find screws.... oh here !OK hold screw press cordless scredriver on top sssst ey whats wrong now? it does not work!

carpenter you have to clamp a bit that fits into the screw and you have to use a screw with thread that is suitable for wood
if you want to use small boards and they don't shall splinter you have to drill a whole first and use screws with a self lowering countersunk head and adjust the torque-limiter to the right value.

OK this analogon is exaggerating but there is some truth in it. You try to write a c++program from a too low knowledge-level to make it even compile.

So it is the same with programming. You have to learn the basics.
I recommend working through this tutorial
Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

As long as you put some own effort into learning it and demonstrate your learning here in the forum you can ask as many questions as you like you will get help and support to drive up the learing-curve.

best regards Stefan

skant153:
i don't. i've no knowledge of programming at all.
i've just put what's in there in another program trying to keep them similar.

i was just copying out the sections that are relevant.

I reckon you will have to grow your knowledge of programming if you want to get this to work. Copying chunks of code without understanding how things work is a recipe for frustration.

...R

you mean if i want to learn how to do a thing i should go read about it instead of asking for help on a forum intended to provide help? wow, you guys are the best.

don't ever let anyone tell you you're not the best, you're too good for it.

skant153:
you mean if i want to learn how to do a thing i should go read about it instead of asking for help on a forum intended to provide help? wow, you guys are the best.

That is most certainly NOT what I meant - but we occasionally get people here who enjoy a grievance more than they enjoy being helped. I hope you are not one of those.

If you want help with learning about programming that is different from help with a specific project, although the project could be used as a basis for the learning.

...R

skant153:
you mean if i want to learn how to do a thing i should go read about it instead of asking for help on a forum intended to provide help? wow, you guys are the best.

don't ever let anyone tell you you're not the best, you're too good for it.

A tutorial might to you a bit of good as it presents the foundation concepts in an organized fashion. How do you think a class in a school would be like if the teacher just started the lesson with "Today we will be talking about programming. Does anyone have any questions?" We are the best and you will see that if you take a moment to read a few of the responses already made relevant to your problem.

i'm not interested in learning how to program. i'm too old and stupid to learn all about how to program. all i want to do is get this done so i can carry on with what i need to do.

i've gotten the motor to turn cw and ccw. i'm not clear on how to have a button start this process.

void loop() {
  // read the sensor value:
//  int sensorReading = analogRead(SPEED_CONTROL);
  // map it to a range from 0 to 100:
  int motorSpeed = 60; //map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(STEPS_PER_REV);
    delay(500);
        stepper_NEMA17.setSpeed(motorSpeed);
     //step 1/100 of a revolution:
    stepper_NEMA17.step(-STEPS_PER_REV);
    delay (2000);
  }

skant153:
i'm not interested in learning how to program.

Head on over to the Gigs and Collaborations and post what work you would like someone to do for you and the value you give it.

thanks preewhatever, if you ever decide to add anything to a conversation, let us know. you've been exactly the opposite of helpful. you're just the type of person who thinks he's better than everyone else just because you know slightly more than others about a topic. maybe take a course in leadership and you'll realize you've got a ways to go.

anywho, without the addition of anything on this forum, i've been able to mingle code together to get this to work, which is exactly what i was told not to do. thanks for the lack of assistance.

code in case anyone looks it up in the future.

/*
  Stepper Motor Demonstration 3
  Stepper-Demo3.ino
  Demonstrates NEMA 17 Bipolar Stepper with L298N Driver
  Uses Potentiometer on Analog Input A0
  Uses Arduino Stepper Library

  DroneBot Workshop 2018 
  https://dronebotworkshop.com
*/

// Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants

// Number of steps per output rotation
const int STEPS_PER_REV = 200;
const int SPEED_CONTROL = A0;
const int button1Pin = 3;
int button1State = 0;
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11 
// Connected to L298N Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-2-3-4 for proper step sequencing

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() {
    pinMode(button1Pin, INPUT_PULLUP);
  // nothing to do inside the setup
}

void loop() {
      button1State = digitalRead(button1Pin);
  // read the sensor value:
//  int sensorReading = analogRead(SPEED_CONTROL);
  // map it to a range from 0 to 100:
  int motorSpeed = 60; //map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (button1State == LOW) {
    stepper_NEMA17.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    stepper_NEMA17.step(STEPS_PER_REV);
    delay(500);
        stepper_NEMA17.setSpeed(motorSpeed);
     //step 1/100 of a revolution:
    stepper_NEMA17.step(-STEPS_PER_REV);
    delay (2000);
  }
}

Here is your code cleaned up:

#include <Stepper.h>

// Define Constants

// Number of steps per output rotation
const int STEPS_PER_REV = 200;
const int SPEED_CONTROL = A0;
const int button1Pin = 3;
const int motorSpeed = 60;
int button1State = LOW;

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() 
{
    pinMode(button1Pin, INPUT_PULLUP);
    stepper_NEMA17.setSpeed(motorSpeed);
}

void loop() 
{
  button1State = digitalRead(button1Pin);
  if (button1State == LOW) 
  {
    stepper_NEMA17.step(STEPS_PER_REV);
    delay(500);
    stepper_NEMA17.step(-STEPS_PER_REV);
    delay (2000);
  }
}

If you remove the delay(2000) you can respond to subsequent button presses faster. I would also recommend looking for a button change rather than a button state. If you want to be able to abort the operation using another button press then you will have to move the stepper a step at a time while looking for a button change.

skant153:
thanks preewhatever, if you ever decide to add anything to a conversation, let us know. you've been exactly the opposite of helpful.

That's a very unfriendly attitude from a person who is requesting volunteers to give up their time to provide advice.

Earlier you said "i'm not interested in learning how to program." which is perfectly reasonable - I suspect you are among 80% or 90% of the world population. And the usual course for people who don't want to learn a skill (for example as a plumber or a car mechanic) is to hire an expert. That is what @Perehama was suggesting to you. I just can't understand why you would take offence at that.

...R