Allowing the button to do it's function even after its no longer held down

Hello,

I would like my code to run at the press of the button. Currently, the stepper will only move when the button is actively pressed. I'd like for one press to cause the stepper to do two revolutions.

Any ideas?

Here is my code:
''#include <Stepper.h>
const int stepsPerRevolution = 150; // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int buttonPin = 2;
int buttonState = 0;

int buttonPushCounter = 0; // counter for the number of button presses
int lastButtonState = 0; // previous state of the button

void setup() {
myStepper.setSpeed(120);
pinMode(buttonPin, INPUT_PULLUP); //high off and low on
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:

buttonState = digitalRead(buttonPin);

if (buttonState == LOW) {
delay (100);
(buttonState = LOW);
delay (100);
Serial.println("on");
Spreader();
delay(500);
}
}
void Spreader() {
// read the pushbutton input pin:
myStepper.step(stepsPerRevolution * 2);
}
"

Please. put your code in code snippets like this

hi

this is done by putting ``` at the start and end of your code

Okay, I will. Can you assist me though.

i will try but no guarantee

Look at state change detection. When you detect the button going down, do your thing.

HTH

a7

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