Back to Parent

// name the pins
int ledPin1 = D2, ledPin2 = D4, ledPin;

void setup()
{
    // Configure the pins to be outputs
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);

    // Initialize both the LEDs to be OFF
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    
    //Register our Particle function here
    Particle.function("led", ledControl);
    Particle.function("ledsel", ledSelect);
    Particle.variable("ledID", ledPin);
}

void loop()
{
    // Nothing to do here
}

int ledSelect(String ledPinInput)
{
    if(ledPinInput == "D2"){
        ledPin = ledPin1;
        return 1;
    }else if(ledPinInput == "D4"){
        ledPin = ledPin2;
        return 1;
    }
    else{
        return -1;
    }
}

int ledControl(String command)
{
    int state = LOW;

    // find out the state of the led
    if(command == "HIGH"){
	    state = HIGH;
    }else if(command == "LOW"){ 
	    state = LOW;
    }else{
	    return -1;
    }

    // write to the appropriate pin
    digitalWrite(ledPin, state);
    return 1;
}
Click to Expand

Content Rating

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

0