[closed] Steppermotor (w/ L928N driver) two buttons code

I'm using this coding so that my stepper motor can rotate clockwise with one button and counterclockwise with the other. Does this code work?

`#include <Stepper.h>

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
// set 7the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}

void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(0);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(0);
delay(500);
}
//second button

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
// step one revolution in one direction:
Serial.println("counterclockwise");
myStepper.step(stepsPerRevolution);
delay(0);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
// step one revolution in the other direction:
Serial.println("clockwise");
myStepper.step(0);
delay(500);
}
}
`

Did you test it?

Please do not post i "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

Your attempt at code tags did not quite work out; instead of single backticks you need to use triple backticks (```); please edit your post and fix it.

1 Like

Looks like the else clauses for each button would move the opposite of what you want.

This code does not work as intended. It reports "clockwise, counterclockwise" with those motors on and repeats on its own without button control.

1 Like

I recommend replacing the L298N with a unit that has MOSFET outputs. The L298 drops about 3V which is just burnt as heat.

how do you think I should rewrite my code as?

i also use a 7.5v battery for the L928N if that helps.

please help me out ive had a lot of struggle with it and i want to see how it could start to work.

No idea but you are now supplying the motor with 4.5V which is about a 40% power loss.

Really? I do say the code worked perfectly well with only one button my issue is just the second button doesn't seem to want to work.

i do apologize for that this is my first time posting and first time here. :frowning:

Both buttonPin1 and buttonPin2 do the same thing. Try using a negative sign in front of stepsPerRevolution ... and figure out why the motors drive forward all the time, and only reverse when a button is pressed. Once a button is pressed, the forward motor state is stuck. You need to reset the state.

Break your sketch down to:

  1. Begin at a stop.
  2. Move forward with one button (only when the button is pressed)
  3. Move backward with the second button (only when the button is pressed)

[edit]

I see it is adapted from the example...

... look to debounce your buttons and it will probably work as you say it did.
https://www.arduino.cc/en/Tutorial/Debounce

1 Like

thank you so much and where would i put the negative sign would it be in the second if else statment? If you can show me an example using my coding that would be amazing im really new at the whole coding industry or well in general.

We all start somewhere... at the beginning.

And, see the example link in the previous post.

You are not reading. (but, it is just one "-" sign... easy to miss)

[edit]

I was messing with your code.... and the most important thing is the button debounce. I think that will solve most of any problems. The "negative sign" will give you reverse... but figure out the button debounce first.

I'd indent it for and post it according to the forum rules for easy viewing and cutting & pasting, and also add something to the output to differentiate between the messages caused by the different buttons:

#include <Stepper.h>

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);
  // set 7the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // step one revolution in one direction:
    Serial.println("clockwise1");
    myStepper.step(stepsPerRevolution);
    delay(0);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    // step one revolution in the other direction:
    Serial.println("counterclockwise1");
    myStepper.step(0);
    delay(500);
  }
  //second button

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // step one revolution in one direction:
    Serial.println("counterclockwise2");
    myStepper.step(stepsPerRevolution);
    delay(0);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    // step one revolution in the other direction:
    Serial.println("clockwise2");
    myStepper.step(0);
    delay(500);
  }
}

This gives this output with no buttons attached:

counterclockwise1
clockwise2
counterclockwise1
clockwise2
counterclockwise1
clockwise2
counterclockwise1
clockwise2
counterclockwise1
clockwise2

I'd would also show how the components are connected with a schematic, because different things could happen depending on the connections.

Where did you get this code? and do they have any guidance?

1 Like

Is there a pulldown resistor at the input pin? Please show a schematic how everything is wired.

Because of the delays and the blocking stepper movement there is no debouncing needed in this sketch. The problem is more that the delays slow down reaction on button presses So you have to press the button longer to be recognized.

That's because of the print statement in the else block. The only thing that should be done in the else block is turning the led off ( and shorten the delay for faster reaction to the button presses )

@milkshakee:
I would recommend connecting the buttons between pin and Gnd (without any resistor) and use INPUT_PULLUP. Try this slightly adapted sketch:

#include <Stepper.h>

// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT_PULLUP);
  // set 7the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(115200);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed. If it is, the buttonState is LOW:
  if (buttonState1 == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // step one revolution in one direction:
    Serial.println("clockwise1");
    myStepper.step(stepsPerRevolution);
    delay(0);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    // step one revolution in the other direction:
   // Serial.println("counterclockwise1");
    //myStepper.step(0);
    delay(20);
  }
  //second button

  // check if the pushbutton is pressed. If it is, the buttonState is LOW:
  if (buttonState2 == LOW) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    // step one revolution in one direction:
    Serial.println("counterclockwise2");
    myStepper.step(-stepsPerRevolution);
    delay(0);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    // step one revolution in the other direction:
   // Serial.println("clockwise2");
    //myStepper.step(0);
    delay(20);
  }
}

[Edit] Corrected the button comments: state is now LOW when pressed.

1 Like

Might help if we knew which of the thousands of different stepper motors you have.

Hi, @milkshakee

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

hi sorry yes! once I'm back at my crib i gotcha lad :smile:

the buttons do both work now. But my only issue now is that when none are pressed it still turns on and rotates every 180 degrees.