Back to Parent

//Declare variable ledPin as an integer
//Set ledPinI to D2 on argon

int ledPin = D2;

// Create a variable to store the brightness of the LED

int ledValue = 0;

//setup is called once every time a program is run 
void setup() {
    
    //Register 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);
    
    //tell argon to use D2 as the output pin
    pinMode(ledPin, OUTPUT);

}

void loop() {

    // nothinIg to do here since the internet connected funtion above takes over

}

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

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

   // Use PWM to set the brightness
   // of the LED
   analogWrite(ledPin, ledValue);

   // 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