Go Down

Topic: LED SEQUENCE WITHOUT DELAY (Read 1 time) previous topic - next topic

tuxduino

I hope this helps...

Code: [Select]


const byte NUM_STEPS = 3;
byte currStep;

// see blink without delay example
unsigned long prevMillis;

// execute a new step every "interval" milliseconds
unsigned long interval = 1000;    // 1 s to avoid flooding the serial terminal with Serial.println() statements


// perform the expected action(s) for the specified step number
void executeStep(byte stepNumber) {
    switch(stepNumber) {
    case 0:
        // just an example...
        Serial.println("executing step 0");
        break;

    case 1:
        Serial.println("executing step 1");
        break;

    case 2:
        Serial.println("executing step 2");
        break;

    default:
        // this is just to help debugging
        Serial.println("Unexpected step number!");
        break;
    }
}


void setup() {
    Serial.begin(115200);
   
    // start from first step
    currStep = 0;
   
    // execute first step
    executeStep(currStep);
}


void loop() {
    // see blink without delay example...
    // if the time has come to process the next step...
    if (millis() - prevMillis >= interval) {
        prevMillis = millis();

        // switch to next step
        currStep++;
        // restart if needed
        if (currStep >= NUM_STEPS) {
            currStep = 0;
        }
       
        // execute the new step
        executeStep(currStep);
    }
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy