Single button to start loop for once

Hello friends,

I’m very new to arduino, and cannot find a solution for my problem. I have made a turntable with Leonardo for photography, when I push a button to start loop, it turns 90 degrees - stop - wait for 1 second -send a keystroke to pc, and repeat this 4 times for full 360 degrees and finish, but when I push the start button again, it doesn’t start and I have to reset arduino each time. Please can you give me some advice to solve this problem? Sorry for my bad English.

Here is my sketch

#include <Keyboard.h>

int pinButton = 8;
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin

void setup(){

const byte buttonPin = 8;
pinMode(buttonPin, INPUT_PULLUP);
while (digitalRead(buttonPin) == HIGH)
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
Serial.begin(9600);
}

void loop(){

digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/Slowly turns the motor 1600 steps/
for (int x = 0; x < 400; x++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(1200);
digitalWrite(smStepPin, LOW);
delayMicroseconds(1200);

}

delay(600);
Keyboard.begin();
Keyboard.write(‘a’);
Keyboard.end();
delay(600);

digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/Slowly turns the motor 1600 steps/
for (int x = 0; x < 400; x++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(1200);
digitalWrite(smStepPin, LOW);
delayMicroseconds(1200);
}

delay(600);
Keyboard.begin();
Keyboard.write(‘a’);
Keyboard.end();
delay(600);

digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/Slowly turns the motor 1600 steps/
for (int x = 0; x < 400; x++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(1200);
digitalWrite(smStepPin, LOW);
delayMicroseconds(1200);
}

delay(600); //
Keyboard.begin();
Keyboard.write(‘a’);
Keyboard.end();
delay(600);

digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/Slowly turns the motor 1600 steps/
for (int x = 0; x < 400; x++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(1200);
digitalWrite(smStepPin, LOW);
delayMicroseconds(1200);
}

delay(600); //
Keyboard.begin();
Keyboard.write(‘a’);
Keyboard.end();
delay(600);

while(1) {
}

}

Don't attach your code. Post it in code-tags. Many are using this forum on a mobile, which doesn't have the ability to open *.ino files.

The

 while(1) {
  }

at the end of loop() means "do nothing forever". Remove that.

Move the " while (digitalRead(buttonPin) == HIGH)" to the top of loop() and add a ';' or "{}" at the end of the line to give it some 'nothing' to do until the button is pressed. Now when your loop() ends it will go back to waiting for the button to be pressed.

It worked, thank you very much.

while (digitalRead(buttonPin) == HIGH)
  pinMode(smDirectionPin, OUTPUT);

This hardly makes sense.

If you want to do nothing until the switch is pressed, include a body with a do nothing (or just nothing) in it.

while (digitalRead(buttonPin) == HIGH)
{
   ; // Do nothing
}

Setting the mode of the pin over and over is useless.

Thanks very much for the answers, my problem is solved with your help.