Heartbeat

/*  Heartbeat
 *   Paul Badger 2009
 *   Demonstates how to use an array with millis to achieve irregular rhythms
 *   This function still uses one delay but it never leaves the LEDpin / LED in
 *   the ON state.
 */



long heartBeatArray[] = {
    50, 100, 15, 1200};
int hbeatIndex = 1;   // this initialization is important or it starts on the "wrong foot"
long prevMillis;

int LEDpin = 13;

void setup()                    
{
    Serial.begin(9600);
    pinMode(LEDpin, OUTPUT);
}

void loop()                    
{
    heartBeat(1.0);   // try changing the parameter
}

void heartBeat(float tempo){
    if ((millis() - prevMillis) > (long)(heartBeatArray[hbeatIndex] * tempo)){
        hbeatIndex++;
        if (hbeatIndex > 3) hbeatIndex = 0;

        if ((hbeatIndex % 2) == 0){
            digitalWrite(LEDpin, HIGH);
            delay((int)heartBeatArray[hbeatIndex]) ;  
            digitalWrite(LEDpin, LOW);
        }
        hbeatIndex++;
        // Serial.println(hbeatIndex);
        prevMillis = millis();

    }
}

Heartbeat2

This version gets rid of all the delays, but the function can end up leaving the light on, if you stop calling it.

/* Heartbeat2
 *   Paul Badger 2009
 *   Demonstates how to use an array with millis to achieve irregular rhythms
 *   This function has no delays but it may leave the LEDpin / LED in
 *   the ON state, if the user stops calling it. Hence the "stopHeatbeat()" function
 */


long heartBeatArray[] = {
    50, 100, 15, 1200};
int hbeatIndex = 1;    // this initialization is not important  
long prevMillis;

int LEDpin = 13;

void setup()                    
{
    Serial.begin(9600);
    pinMode(LEDpin, OUTPUT);
}

void loop()                    
{
    heartBeat(1.0);     // try changing parameter
}

void heartBeat(float tempo){
    if ((millis() - prevMillis) > (long)(heartBeatArray[hbeatIndex] * tempo)){
        hbeatIndex++;
        if (hbeatIndex > 3) hbeatIndex = 0;

        if ((hbeatIndex % 2) == 0){     // modulo 2 operator will be true on even counts
            digitalWrite(LEDpin, HIGH);
        }
        else{
            digitalWrite(LEDpin, LOW);
        }
        //  Serial.println(hbeatIndex);
        prevMillis = millis();
    }
}


void stopHeatbeat(){
    digitalWrite(LEDpin, LOW);
}

Share