IO program control

Welcome. You can do whatever is needed :wink:

I don't have any resources for you except for the arduino reference and the web in general.

To read a switch, you can read up on digitalRead() - Arduino Reference.

In it's simplest form (loop() only, switch wired between pin and GND)

void loop()
{
  if (digitalRead(somePin) == LOW)
  {
    runPattern();
  }
}

void runPattern()
{
  ...
  ...
}

If you did not set up your code correctly from the beginning, you probably will sooner than later find out that if your pattern is running and takes a lot of time, your code will not react immediately on the switch because your code waits for the runPattern() to be finished before it returns to loop(). This is called blocking; you need to stay away from for-loops, while-loops etc. if they contain delays.

There are plenty of examples how to work around that. E.g. on the forum

  1. The blink-without-delay example in the IDE
  2. Using millis() for timing. A beginners guide
  3. Demonstration code for several things at the same time
  4. Flashing multiple LEDs at the same time
  5. State machines, a short tutorial

Because you asked about blocking the output, below demonstrates a way to do that.

const uint8_t switchPin = 2;
const uint8_t ledPin = LED_BUILTIN;

void setup()
{
  Serial.begin(115200);


  pinMode(switchPin, INPUT_PULLUP); // switch connected between pin and GND
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  // simulate pattern
  uint8_t ledState = random(0, 2);
  
  // debug
  Serial.print("ledState if switch is pressed: ");
  Serial.println(ledState);
  // if switch is closed
  if (digitalRead(switchPin) == LOW)
  {
    Serial.println("Setting led");
    // set led state
    digitalWrite(ledPin, ledState);
  }

  // slow serial output down
  delay(2000);
}