How to make a stepper motor run with one button click

Hi everybody,

For my project i have to run a stepper motor for x amount of time. To activate the stepper motor i used a button for input. The problem is that the stepper motor only runs when i hold the button. The perfect situation would be if i pushed the button once and then rotated for x amount of time. What do i have to add to make this work?

My arduino code is shown below, i hope somebody can help me!

int bluepin = 2;
int pinkpin = 3; 
int yellowpin = 4; 
int orangepin = 5;
int currentStep = 0;

int buttonpin = 8;
int buttonState = 0;

void setup() {
// put your setup code here, to run once:
pinMode (bluepin,OUTPUT);
pinMode (pinkpin,OUTPUT);
pinMode (yellowpin,OUTPUT);
pinMode (orangepin,OUTPUT);
pinMode(buttonpin,INPUT);
digitalWrite(bluepin,LOW);
digitalWrite(pinkpin,LOW);
digitalWrite(yellowpin,LOW);
digitalWrite(orangepin,LOW);

}
void loop() {
buttonState = digitalRead(buttonpin);
if (buttonState == HIGH) {
// turn Motor on:
switch(currentStep) {
case 0:
digitalWrite(bluepin,HIGH);
digitalWrite(pinkpin,LOW);
digitalWrite(yellowpin,LOW);
digitalWrite(orangepin,LOW); 
break;
case 1:
digitalWrite(bluepin,LOW);
digitalWrite(pinkpin,HIGH);
digitalWrite(yellowpin,LOW);
digitalWrite(orangepin,LOW); 
break;
case 2:
digitalWrite(bluepin,LOW);
digitalWrite(pinkpin,LOW);
digitalWrite(yellowpin,HIGH);
digitalWrite(orangepin,LOW); 
break;
case 3:
digitalWrite(bluepin,LOW);
digitalWrite(pinkpin,LOW);
digitalWrite(yellowpin,LOW);
digitalWrite(orangepin,HIGH);
break; 
}
currentStep = (++currentStep < 4) ? currentStep : 0;
delay(3);
} else {
// turn motor off:
digitalWrite(currentStep, LOW );

Please read this:-
How to use this forum
Because your post is breaking the rules about posting code.

Look at the state change detection example code in the IDE to do what you want.

Use millis() for a timeout.

So in your loop() something like this.

if (digitalRead(buttonpin) startTime = millis()
if (millis() < startTime + timeout) {
  run motor
}

You need to have a variable (let's call is motorRun) which can be set to true when you press the button. Then the code for your motor will make it do N steps if motorRun == true. When the N steps have been completed it should set motorRun to false.

...R
Stepper Motor Basics
Simple Stepper Code

Thx for the reactions guys. I understand the feedback but i have trouble to process it into my arduino code. I'm quite new to arduino so do you guys have any tips or advice left for me how i can build up my code?

Thanks for the code tags.

I understand the feedback but i have trouble to process it into my arduino code.

So did you try the state change example code?

You need to keep tabs on the last button state so that you do this

if (buttonState == HIGH && lastButtonState == LOW){ running = !running }

running is a boolean variable and replaces
if (buttonState == HIGH) {
with
if (running) {