Program to run once and stop

I have a project which needs to be run one pass thru the program each time a button is pushed, & then stopped, or at least running in a loop till the button is pushed again. Before I tie all my subroutines together for nothing, can this be done or am I trying to do something impossible? THANK YOU for any help you can give!

Welcome

Of course it's possible :slight_smile:

Here is an example t1023908.ino - Wokwi Arduino and ESP32 Simulator

Here is an example using the state change detection method to run a function or a section of code each time a button is pressed. See the lines marked with *******. The code will run one time when the button switch changes state from HIGH to LOW.

// by C Goulding aka groundFungus

const byte  buttonPin = 2;    // the pin to which the pushbutton is attached
const byte ledPin = 13;       // the pin to which the LED is attached

bool buttonState = 0;         // current state of the button
bool lastButtonState = 0;     // previous state of the button

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(115200);
   Serial.println("Function to run once per button press");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:

            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
            // *********** put the code that you want to run once 
            // each button press here ***************
            Serial.println("this will run one time per button press");
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}
1 Like

THANK YOU! THANK YOU! I'm new at the Arduino altho I worked on IBM computers
for years & wrote some programs in Fortran & SPS. I think I can work something like this
into my sketch & you gave me a way to do it! Thanks again-- The old Whizgeezer

Have a look at state machines. The concept is that an input changes a variable (state) and an if statement becomes true now that the state has changed. The code in the if statement runs and increments the state again so that next time through loop it will not run as it is no longer true. Instead the next state will run which can be the original state or a completely new one.

A useful way to get your head around it is to draw a finite state machine diagram. Decide how many states your code has. Put each in a circle and draw a line between any state that can progress into another. Then mark the input or condition that increments between each state. Eg;

consider

// button driven action

const byte butPin = A1;
byte butState;

// -----------------------------------------------------------------------------
void
doSomething (void)
{
    for (int n = 0; n < 5; n++)  {
        Serial.print   ("doSomething: ");
        Serial.println (n);
        delay (1000);
    }
}

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            doSomething ();
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

Thank you! This is an intriguing approach so I'll keep it in mind; I got 3 others to choose from
so I have some good choices. WhizGeezer

THANK YOU! Yours is the simplest & the most promising of the 4 replies I received so
I think I can use this approach.
WhizGeezer

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