Run loop once with a push button

Hello for everyone. I’m new with Arduino and I’m stuck here. So here is the deal. I have stepper motor and I want to make loop run only once when push button is pressed and released. I will be so grateful if someone will check the code and give me some ideas what could possibly be wrong.

int buttonPin;
const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;

void setup()
{
  buttonPin = 7; 
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
}

void loop()
{
  //check button pressed, if so enter program condition (inside if statement)
  if(digitalRead(buttonPin) == LOW) //functions based off of button pulling input pin LOW
  {
    digitalWrite(dirPin,HIGH); // Komanda suktis į vieną pusę
  // Makes 800 pulses for making one full cycle rotation, for funkcijoje esantis skaičius nurodo kokį kelią turi padaryti steperis
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); // speed of stepper
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay



  digitalWrite(dirPin,LOW); //Komanda suktis į kitą pusę
  // Makes 800 pulses for making two full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  
   digitalWrite(dirPin,HIGH); // Komanda suktis į vieną pusę
  // Makes 800 pulses for making one full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay

    digitalWrite(dirPin,HIGH); // Komanda suktis į vieną pusę
  // Makes 800 pulses for making one full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay


  
     //put code you want to run on button press here.
   }
}

The code runs as long as the button pin is low.
Have a look at the state change example to make the code only run when the button pin becomes low.

buttonPin = 7;

All the other pins have constants.

What is special about buttonPin?

It would be useful if you used the IDE’s auto format tool on your code, to give a better picture of how it is structured.

You have a lot of repeated code - some more functions would help.

Put a check in your code. Something like int doStuff = 0;

And when your code reads a button push, set doStuff = 1;

Then all the stuff that happens when a button is pushed can be in an if statement like

if(doStuff == 1){
//do the stuff.
//After stuff is done reset doStuff to 0
doStuff = 0;
}

That's basically how I would do it anyway. Might be better ways to do it though.

Hey fellas, I’m so glad I’ve made it and I really want to say thank you for your help. It looks like so simple. I’m making mojito machine. So, idea is to push momentary button and make specific movement of stepper and servos once per button is pressed. So here is code :slight_smile:

const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;
const int pin = 6;
int buttonState = 0;


void setup() {


  pinMode(pin, INPUT);
  digitalRead(pin); // not sure if I need it 
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
  
}

void loop() {
  if (digitalRead(pin) == HIGH) { //if the moment button is pressed once it makes command below once
    action();
  }
}
void action() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 800 pulses for making one full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay

  digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 800 pulses for making two full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
}
action() {

Didn’t the compiler complain just a little bit there?

You have comments indicating 200 and 400 steps, but code that suggests 800.

TheMemberFormerlyKnownAsAWOL:

action() {

Didn't the compiler complain just a little bit there?

You have comments indicating 200 and 400 steps, but code that suggests 800.

Yup yup, sorry for that . I've just translated some notes to English and deleted "void" . My mistake.

Algis95:
Hey fellas, I’m so glad I’ve made it and I really want to say thank you for your help. It looks like so simple. I’m making mojito machine. So, idea is to push momentary button and make specific movement of stepper and servos once per button is pressed. So here is code :slight_smile:

const int stepPin = 5; 

const int dirPin = 2;
const int enPin = 8;
const int pin = 6;
int buttonState = 0;

void setup() {

pinMode(pin, INPUT);
  digitalRead(pin); // not sure if I need it
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
 
}

void loop() {
  if (digitalRead(pin) == HIGH) { //if the moment button is pressed once it makes command below once
    action();
  }
}
action() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay

digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 800; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
}

It looks to me like a pretty good start. Of course, they’ll be a few along any time to talk to you about those delays. They’re fine if you don’t plan on ever needing something else to happen during those times.

Don’t be afraid to use descriptive names for functions and variables. As examples: redLedPin or runStepperOne(). It can help make it easier for others to help you debug an issue you may have in the future. Just an observation I’ve seen with other posts I’ve seen here.

I’m pretty new to Arduino myself. So, take what I say with a grain of salt. LoL