Back to Parent

// name the pins
int ledPin1 = D2;
int ledPin2 = D4;
int ledMax = 255;
int ledMin = 0;

// create a variable to store the
// current brightness of the LED
int ledValue = 0;

void setup()
{
    //Register our Particle function to allow
    // Control of the LED
    Particle.function("led", ledControl);

    // Make the variable 'ledValue' available through
    // the Particle cloud as 'brightness'
    Particle.variable("brightness", ledValue);

    // Set up pin for output
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
}

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

int ledControl(String command)
{
    // Convert the passed variable to an integer
    ledValue = command.toInt();

    // Check it is a valid number
    if( ledValue > ledMax ) return -1;
    if( ledValue < ledMin ) return -1;

    // Use PWM to set the brightness
    // of the LED
    analogWrite(ledPin1, ledValue);
    analogWrite(ledPin2, ledMax - ledValue);
    delay(5000);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    // Return 1 to say completed successfully
    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