Back to Parent

// Include Particle Device OS APIs
//#include "Particle.h"
//Adwoa Asare
//10/24/2024
// Let Device OS manage the connection to the Particle Cloud
//SYSTEM_MODE(AUTOMATIC);


int led1 = D1;
int brightness = 0;

// setup() runs once, when the device is first turned on
void setup() {
  
   
    //digitalWrite(led1,LOW);
    Particle.function("led1",ledControl);
    Particle.variable("brightness", brightness);
     pinMode(led1, OUTPUT);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  
    /*blink code
    digitalWrite(led1,HIGH);//LED ON
    delay(1000);//1 sec delay
    digitalWrite(led1,LOW);//LED OFF
    delay(1000);//1 sec delay
    */

}
/*
int ledControl(String command){
    int state = LOW;
    
    //set new state of LED
    if(command=="HIGH"){
        state = HIGH;
    }else if(command=="LOW"){
        state = LOW;
    }else{
        return -1;
    }
    
    //output state to pin for LED
    digitalWrite(led1, state);
    return 1;
    
}
*/
int ledControl(String command){
    //convert command to an int
    brightness = command.toInt();
    
    //bounds checking
    if(brightness > 255 || brightness <0) return -1;
    
    //set brightness of led using PWM
    analogWrite(led1, brightness);
    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