//Using "Blink without Delay" as a template to avoid blocking code
//changing TOTLEDS to a larger number will not work you need to change the code
//to accomodate
#define TOTLEDS 12
#define BUTTONPIN 12
uint8_t ledPinArray[TOTLEDS] = { 19, 18, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
uint8_t scriptPosition = 0;
bool winnerLitUp = 0;
static uint8_t arrayState = 0;
unsigned long previousMillisScript = 0;
unsigned long intervalScript = 50;
unsigned long previousMillisBlink = 0;
unsigned long intervalBlink = 50;
void setup()
{
Serial.begin(9600);
for (uint8_t counter = 0; counter < TOTLEDS; counter++)
{
pinMode(ledPinArray[counter], OUTPUT);
}
pinMode(BUTTONPIN, INPUT_PULLUP);
Serial.println("Press the button.");
}
void loop()
{
// button, doesn't really need debounce in this case.
// you can also read less frequently and use a third 1 ms timer.
if (!digitalRead(BUTTONPIN))
{
if (scriptPosition == 0)
{
randomSeed(millis());
scriptPosition = 1;
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillisBlink >= intervalBlink)
{
previousMillisBlink = currentMillis;
//code
uint16_t rndNum;
static bool allDark = 0;
static bool winnerChosen = 0;
static uint8_t winnerLamp = 0;
switch (arrayState)
{
case 0:
// you can keep an array of states and compare
// but i'm keeping it simple
if (!allDark)
{
for (int counter = 0; counter < TOTLEDS; counter++)
{
digitalWrite(ledPinArray[counter], false);
}
winnerChosen = 0;
}
allDark = 1;
break;
case 1:
allDark = 0;
rndNum = random(0, 0xFFFF); // using hex notation for readability
for (int counter = 0; counter < TOTLEDS; counter++)
{
//using bitread to kep it readable for you
digitalWrite(ledPinArray[counter], bitRead(rndNum, counter));
}
break;
case 2:
if (!winnerChosen)
{
winnerLamp = random(0, TOTLEDS-1);
// left to right.
Serial.print("Winner:");
Serial.println(TOTLEDS - winnerLamp);
winnerChosen = true;
winnerLitUp = false;
}
rndNum = random(0, 0xFFFF);
for (int counter = 0; counter < TOTLEDS; counter++)
{
bool winningLamp;
if (winnerLamp == counter)
{
winningLamp = true;
}
else
{
winningLamp = bitRead(rndNum, counter);
}
digitalWrite(ledPinArray[counter], winningLamp);
}
break;
case 3:
if (!winnerLitUp)
{
for (int counter = 0; counter < TOTLEDS; counter++)
{
bool winningLamp;
if (winnerLamp == counter)
{
winningLamp = true;
}
else
{
winningLamp = false;
}
digitalWrite(ledPinArray[counter], winningLamp);
}
winnerLitUp = true;
}
break;
}
}
if (currentMillis - previousMillisScript >= intervalScript)
{
previousMillisScript = currentMillis;
//code
switch (scriptPosition)
{
case 0:
arrayState = 0;
break;
case 1:
Serial.println("Starting Blink on all.");
arrayState = 1;
intervalScript = 3000; // mseconds of blinking (all random)
scriptPosition = 2;
break;
case 2:
Serial.println("Holding one.");
arrayState = 2;
intervalScript = 3000; // mseconds of staying on the winning number with others blinking
scriptPosition = 3;
break;
case 3:
Serial.println("Turning all others off.");
arrayState = 3;
intervalScript = 3000; // mseconds of staying on the winning number without others blinking
scriptPosition = 4;
break;
case 4:
Serial.println("All off. Press the button again.");
Serial.println("");
arrayState = 0;
intervalScript = 50; // mseconds of staying on the winning number
scriptPosition = 0;
break;
}
}
}