Back to Parent

//WIP web led test
// single led functions

int LED01 = D2;
int blinkDelay = 1000; //blink delay in millis
int LED_state = LOW;
int blinkCt = 0; 

void setup() {
    
    pinMode(LED01,OUTPUT);
    Particle.function("led",ledControl);
    digitalWrite(LED01, LED_state);

}

void loop() {
    if (blinkCt != 0){
        //Single var for blink mode, var -1 used for stay on 
        //Blink count decrements once every high blink
        ledBlink();
        
    }
}


int ledControl(String command){
    String subcmd=""; 
    if (command == "HIGH"){
        LED_state = HIGH;
        blinkCt = 0;
    }
    else if (command == "LOW"){
        LED_state = LOW;
        blinkCt = 0; 
    }
    else if (command == "BLINK"){
        LED_state = LOW;
        blinkCt = -1; 
    }
    else if (command == "BLINK3"){
        LED_state = LOW;
        blinkCt = 3;
    }
    else if (command.substring(0:6)=="BLINK:"){
        subcmd = command.substring(6);
        if (subcmd.toInt() >= 0){
            blinkCt = subcmd.toInt(); 
            LED_state = LOW;
        }
    }
    else{
        return -1; 
    }
    
    digitalWrite(LED01, LED_state);
    return 1; 
}

void ledBlink(){
    static unsigned long timer;
    if (millis()-timer>blinkDelay){
        if (LED_state == HIGH){
            LED_state = LOW;
            if (blinkCt>0) blinkCt--; 
        }
        else if (LED_state == LOW){
            LED_state = HIGH; 
        }
        digitalWrite(LED01, LED_state);
        timer = millis();

    }
    
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0