//LAB 1 - EXERCISE 1
//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;
//Set up status variable to check the state of hte power - is it on or off?
bool status = LOW;
//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) {
if(command.equals("on")) {
status = HIGH;
////Version 1 - the easy approach
////Blink on and off every 3 seconds
//digitalWrite(led, HIGH);
//delay(500);
//digitalWrite(led, LOW);
//delay(500);
//digitalWrite(led, HIGH);
//delay(500);
//digitalWrite(led, LOW);
//delay(500);
//digitalWrite(led, HIGH);
//delay(500);
//digitalWrite(led, LOW);
//delay(500);
//return 1;
//This indicates that the 'on' command was successfully received and exectued
////Version 2 - the harder approach
////Blink on and off every 3 seconds using loop
//Using variable i, if i is less than 3, increment by 1
for (int i = 1; i <=3; 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;
}
if(command.equals("off")) {
status = LOW;
return -1; // This indicates that the 'off' command was successfully received and exectued
}
return 401; //This indicates that there is an error
}
//If the command "on" is received, the LED is turned on and blinks 3x
//If the command "off" is received, the LED is turned off
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .