Starting and stopping a sketch.

I have a sketch written. Can I start the sketch in any way other than applying power to the board? Also, I need to have the sketch only run once each time that I start it. Dennis Zubek

You could use something like this:

class PushButton
{
  public:
    PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)
      : pin(pin) { // remember the push button pin
      pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
    }

    bool isPressed() // read the button state check if the button has been pressed, debounce the button as well
    {
      bool pressed = false;
      bool state = digitalRead(pin);               // read the button's state
      int8_t stateChange = state - previousState;  // calculate the state change since last time

      if (stateChange == falling) { // If the button is pressed (went from high to low)
        if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
          pressed = true; // the button is pressed
        }
      }
      if (stateChange == rising) { // if the button is released or bounces
        previousBounceTime = millis(); // remember when this happened
      }

      previousState = state; // remember the current state
      return pressed; // return true if the button was pressed and didn't bounce
    }

  private:
    uint8_t pin;
    bool previousState = HIGH;
    unsigned long previousBounceTime = 0;

    const static unsigned long debounceTime = 25;
    const static int8_t rising = HIGH - LOW;
    const static int8_t falling = LOW - HIGH;
};

// -------------------------------------------------------------------------------------------------------------------------------- //

PushButton button = { 2 };  // Create a new PushButton object on pin 2

void setup() {
  // Your setup here
}

void runOnce() {
  // Your code here
}

void loop() {
    if (button.isPressed()) {  // If the button is pressed
        runOnce();
    }
}

Pieter

The Arduino is always running when powered.

If you want to do something only once, put it in setup().

PieterP:

    PushButton(uint8_t pin) // Constructor (executes when a PushButton object is created)

: pin(pin) { // remember the push button pin
      pinMode(pin, INPUT_PULLUP); // enable the internal pull-up resistor
    }

You should not place hardware related setup (like pinMode) in a constructor.
It could be run before the Arduino's init function, so it may not work.

Such initializations belong to a function often called begin() called from setup,
which is guaranteed to be run after init.

Whandall:
You should not place hardware related setup (like pinMode) in a constructor.
It could be run before the Arduino's init function, so it may not work.

Such initializations belong to a function often called begin() called from setup,
which is guaranteed to be run after init.

While that's definitely true for UART and timer related initializations, all pinMode does is writing to the IO registers.
As long as you're not using an Arduino variant that does weird IO initialization before running, it should not be overridden, and it should work perfectly fine.

djzubek:
I have a sketch written. Can I start the sketch in any way other than applying power to the board?

The answer to your question is "no", but you are asking the wrong question.

If you want your sketch to be doing stuff under some conditions (it's been started), and not doing stuff under other conditions (it's been stopped), then you write a sketch that does that. Starting and stopping is not something external to your sketch, it is part of its functionality.

Perhaps an analogy might help. If you are building a car, it's easy to concentrate on only one part - say, the engine. I mean, a car needs to go, right? But if it also needs to change direction, then you don't manage that by turning off the car, hauling the nose of the car around, and then restarting the car. Going and changing direction are both things that a car needs to do, and if you are going to design a car then you need to design parts that do both of those things. Sure, going may be the most essential part and the part that takes the most effort to accomplish, but the other stuff also needs to be done too.

A model is:

boolean i_am_currently_running;

loop() {
  work out if I'm supposed to be started or stopped right now (eg, by looking at a button)

  if i_am_currently_running and !I_should_be_running, then
    do whatever needs to be done to stop (eg, turn motors off)
    i_am_currently_running = false;
  else 
  if  !i_am_currently_running and _should_be_running, then
    do whever needs to be done to start up
    i_am_currently_running = true;
  end if

  if i_am_currently_running, then
    do whatever might be involved in my usual running functions (blink lights, step steppers)
  else 
    do whatever might be involved in my usual stopped function (eg: nothing)
  end if
}

PieterP:
While that's definitely true for UART and timer related initializations, all pinMode does is writing to the IO registers.
As long as you're not using an Arduino variant that does weird IO initialization before running, it should not be overridden, and it should work perfectly fine.

With the multitude of different platforms the Arduino system is running on, I think it is bad practice.