Back to Parent

//LAB 1 - EXERCISE 3

//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 redLed = D2;
int greenLed = D3; 

//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(redLed, OUTPUT);
    pinMode(greenLed, OUTPUT); 
    
    //This code sets the starting LED status to off
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, 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(redLed, HIGH); 
        delay(500);
                
        digitalWrite(redLed, LOW); 
        delay(500); 
    
        digitalWrite(greenLed, HIGH); 
        delay(500); 


        digitalWrite(greenLed, LOW); 
        delay(500); 
        }
        
        //Return led to off state
        digitalWrite(redLed, LOW); 
        digitalWrite(greenLed, 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