Welcome. You can do whatever is needed ![]()
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
- The blink-without-delay example in the IDE
- Using millis() for timing. A beginners guide
- Demonstration code for several things at the same time
- Flashing multiple LEDs at the same time
- 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);
}