# Run loop on a button (steper motor) help

**URL:** <https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694>\
**Category:** Programming\
**Created:** [October 6, 2016, 8:56pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694 "2016-10-06T20:56:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uki122](https://avatars.discourse-cdn.com/v4/letter/u/ac8455/32.png) [@Uki122](https://forum.arduino.cc/u/Uki122)\
**Post date:** [October 6, 2016, 8:56pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694/1 "2016-10-06T20:56:10Z")

</div>

I have a issue whit code.  
I want to run a loop on button.  
When i press button steper motor should rotate 200 steps and stop .  
End every time when I press button should to do that.  
But it works only when i hold button.  
I repleace pinout and issue is same.

Please help 🙂

Here's my code

[program\_start\_loop122.ino](https://forum.arduino.cc/uploads/short-url/aCYbyRdYIB8ZLsBNSaMtf6hPzFW.ino) (541 Bytes)

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [October 6, 2016, 9:33pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694/2 "2016-10-06T21:33:28Z")

</div>

I did not download your code but from your description you should look at the state change detection example in the digital examples in the IDE File menu. That way you can know when the button BECOMES pressed not IS pressed.

---

<div class="post-metadata">

**Author:** ![Uki122](https://avatars.discourse-cdn.com/v4/letter/u/ac8455/32.png) [@Uki122](https://forum.arduino.cc/u/Uki122)\
**Post date:** [October 6, 2016, 10:03pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694/3 "2016-10-06T22:03:50Z")

</div>

I solved the problem

here code if need

int stp = 5; //connect pin 11 to step  
int dir = 2; // connect pin 10 to dir  
int buttonPin = 11;  
int a = 0;

void setup()  
{

pinMode(buttonPin, INPUT\_PULLUP);

pinMode(stp, OUTPUT);  
pinMode(dir, OUTPUT);

}  
void loop()  
{  
setup();

if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW  
{  
a=0;  
do  
{  
a++;  
digitalWrite(stp, HIGH);  
delay(10);  
digitalWrite(stp, LOW);  
delay(10);

}while(a\<200);

}  
}

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [October 6, 2016, 10:06pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694/4 "2016-10-06T22:06:44Z")

</div>

Why are you calling setup() in loop()? Very unusual.

And you might want to get in the habit of using autoformat from the tools menu (or ctrl t) to indent your code to make it easier to follow.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:06pm UTC](https://forum.arduino.cc/t/run-loop-on-a-button-steper-motor-help/411694/5 "2021-05-05T22:06:58Z")

</div>

Closed on May 5, 2021, 10:06pm UTC


