Back to Parent

//LAB 1 - EXERCISE 2

//PROGRAM OBJECTIVE
//Change the program to blink on and off every 3 seconds


//GLOBAL VARIABLES

// Set up global variable for pin so that any configuration changes can easily and quickly be applied throughout the code 
int led = D2;



//PROGRAM

//Setup function configures the initial state of the microcontroller
//In this case, the microcontroller will be set to off when the program first runs. 
void setup() {
    pinMode(led, OUTPUT);
    
    //This code sets the starting LED status to off
    digitalWrite(led, LOW);
    
    //This code makes the functiona accessible via the cloud
    Particle.function("ledControl", ledControl); 
}

//Loop function repeats the actions in it repeatedly until the microcontroller is turned off (removed from power)
void loop() {

}

int ledControl (String command) {
    
    //converts string to integer number
    int value = command.toInt();
    
    if(value > 0) {
                //Using variable i, if i is less than the entered value, increment by 1
        for (int i = 1; i <= value; i++) {
        
        digitalWrite(led, HIGH); 
        delay(500);
        digitalWrite(led, LOW); 
        delay(500); 
        }
        
        //Return led to off state
        digitalWrite(led, LOW); 
    
        //This indicates that the 'on' command was successfully received and exectued
        return 1; 
    }
    
        return 401; //This indicates that there is an error
}
Click to Expand

Content Rating

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

0