Pages: 1 [2]   Go Down
Author Topic: LED SEQUENCE WITHOUT DELAY  (Read 894 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope this helps...

Code:

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);
    }
}

Logged

Pages: 1 [2]   Go Up
Jump to: