The first thing you should learn is using array's. Then you can code many things more efficient.
Write a separate functions for the blinking patterns without delays
Here (quite) some code snippets that should get you started, some are not super efficient but I think the application is not time critical ![]()
int leds[10] = {3,4,5,6,7,8,9,10,11,12}; // array of the led numbers used
int state = 0;
int lastState = 0;
setup()
{
for (int i=0; i<10; i++)
{
pinMode(led[i], INPUT_PULLUP);
}
...
}
void loop()
{
reading = digitalRead(buttonPin);
if (reading == LOW && reading != lastButtonState)
{
state++;
if (state >= 9) state = 0;
}
lastButtonState = reading;
switch(state)
{
case 0: Pattern0(); break;
case 1: Pattern1(); break;
case 2: Pattern2(); break;
...
}
}
////////////////////////////////////////////////////////
//
// PATTERN FUNCTIONS
//
void Pattern0()
{
for (int i=0; i< 10; i++) digitalWrite(Leds[i], LOW);
}
void Pattern1()
{
for (int i=0; i< 10; i++) digitalWrite(Leds[i], HIGH);
}
void Pattern2()
{
static int interval = 100; // each pattern
static unsigned long lastTime;
if (state != lastState) // first call to this pattern?
{
lastState = state;
// setup the initial state of the leds
for (int i=0; i< 10; i+= 2) // NOTE STEPSIZE 2
{
digitalWrite(Leds[i], HIGH);
digitalWrite(Leds[i+1], LOW);
}
lastTime= millis();
}
// generic blink if interval has passed since last call
if (millis() - lastTime >= interval)
{
for (int i=0; i< 10; i++) digitalWrite(Leds[i], !digitalRead(Leds[i]); // blink;
lastTime= millis();
}
}
void Pattern3()
{
static int interval = 50; // each pattern
static unsigned long lastTime;
if (state != lastState) // first call to this pattern?
{
lastState = state;
// setup the initial state of the leds
for (int i=0; i< 10; i++) digitalWrite(Leds[i+1], LOW);
digitalWrite(Leds[0], HIGH);
digitalWrite(Leds[5], HIGH);
lastTime= millis();
}
// generic shift if interval has passed since last call
if (millis() - lastTime >= interval)
{
int temp = digitalRead(0);
for (int i=0; i< 9; i++) digitalWrite(Leds[i], digitalRead(Leds[ i+1]);
digitalWrite(leds[9], temp);
lastTime= millis();
}
}
You see the repeating pattern of patterns? (code not tested)