ConnectedLED by Felicia
Made by Felicia Luo
Made by Felicia Luo
Control LED(s) blinking through cloud
Created: October 29th, 2022
Write about the big ideas behind your project? What are the goals? Why did you make it? What are your motivations?
Version 0 - Passing in a "HIGH" string will turn on the orange LED; passing in a "LOW" string will turn off the orange LED. See demo video: https://drive.google.com/file/d/1dqj0dsoVUYIw0fpd4gcAhik4_wF0J-ig/view?usp=share_link
Version 1 - On the basis of Version 0, the LED will blink 3 times before it is turned on/off. See demo video: https://drive.google.com/file/d/1GNsxG6njzVvX49DPNpF38gPeqI2JPiec/view?usp=share_link
Version 2 - Passing in a number, the LED will blink the passed-in number of times. See demo video (passing in 3): https://drive.google.com/file/d/1GSuFfpTnzFkQWIJe60ZTxAa68LWQynvf/view?usp=share_link
Version 3 - On the basis of Version 2, two LEDs will alternate to blink, each blinks the passed-in number of times. See demo video (passing in 5): https://drive.google.com/file/d/1-QLkOcKkAvCAHAop8h1uesgGowwv_v0_/view?usp=share_link
Describe the process you underwent to reach the outcome (problems encountered, how you resolved them, as well as, experiments, hacks, tests, refinements, iterations, failures)
Firstly, I was not familiar with the syntax used in Particle, so I searched "for loop" in Particle Docs and found a syntax reference: https://docs.particle.io/reference/device-os/api/language-syntax/control-structures/#for. This was very helpful as it included all basic syntax like if-else condition, operators, loops, and etc.
When I tested my code for version 1 (blinking 3 times before LED on/off), it only blinked once. Then I realized that I did not put delay(500) at the end of the for loop, so the LED was quickly turned off and back on that human eyes could not notice.
Reflect on the process of making this project. What did you learn? What would you do differently?
// version 0
// Passing in a "HIGH" string will turn on the orange LED; passing in a "LOW" string will turn off the orange LED
int ledPinOr = D2;
void setup() {
//Register our Particle function here
Particle.function("ledControl", ledControl);
// configure the pins to be outputs
pinMode(ledPinOr, OUTPUT);
// initialize both the LEDs to be OFF
digitalWrite(ledPinOr, LOW);
}
void loop() {
}
int ledControl(String command) {
int state = LOW;
// find out the state of the LED
if (command == "HIGH") {
state = HIGH;
} else if (command == "LOW") {
state = LOW;
} else {
return -1;
}
// write to the appropriate pin
digitalWrite(ledPinOr, state);
return 1;
}
Click to Expand
// version 1
// On the basis of Version 0, the LED will blink 3 times before it is turned on/off
int ledPinOr = D2;
void setup() {
//Register our Particle function here
Particle.function("ledControl", ledControl);
// configure the pins to be outputs
pinMode(ledPinOr, OUTPUT);
// initialize both the LEDs to be OFF
digitalWrite(ledPinOr, LOW);
}
void loop() {
}
int ledControl(String command) {
int state = LOW;
// find out the state of the LED
if (command == "HIGH") {
state = HIGH;
} else if (command == "LOW") {
state = LOW;
} else {
return -1;
}
// write to the appropriate pin
digitalWrite(ledPinOr, LOW);
for (int i = 1; i <= 3; i++) {
digitalWrite(ledPinOr, HIGH);
delay(500);
digitalWrite(ledPinOr, LOW);
delay(500);
}
digitalWrite(ledPinOr, state);
return 1;
}
Click to Expand
// version 2
// Passing in a number, the LED will blink the passed-in number of times
int ledPinOr = D2;
void setup() {
//Register our Particle function here
Particle.function("ledControl", ledControl);
// configure the pins to be outputs
pinMode(ledPinOr, OUTPUT);
// initialize both the LEDs to be OFF
digitalWrite(ledPinOr, LOW);
}
void loop() {
}
int ledControl(String command) {
// find out the state of the LED
int value = command.toInt();
if( value > 0 ) {
for (int i = 1; i <= value; i++) {
digitalWrite(ledPinOr, HIGH);
delay(500);
digitalWrite(ledPinOr, LOW);
delay(500);
}
}
else {
return -1;
}
return 1;
}
Click to Expand
// version 3
// On the basis of Version 2, two LEDs will alternate to blink, each blinks the passed-in number of times
int ledPinOr = D2;
int ledPinBl = D4;
void setup() {
//Register our Particle function here
Particle.function("ledControl", ledControl);
// configure the pins to be outputs
pinMode(ledPinOr, OUTPUT);
pinMode(ledPinBl, OUTPUT);
// initialize both the LEDs to be OFF
digitalWrite(ledPinOr, LOW);
digitalWrite(ledPinBl, LOW);
}
void loop() {
}
int ledControl(String command) {
// find out the state of the LED
int value = command.toInt();
if( value > 0 ) {
for (int i = 1; i <= value; i++) {
digitalWrite(ledPinOr, HIGH);
digitalWrite(ledPinBl, LOW);
delay(500);
digitalWrite(ledPinOr, LOW);
digitalWrite(ledPinBl, HIGH);
delay(500);
}
digitalWrite(ledPinBl, LOW);
}
else {
return -1;
}
return 1;
}
Click to Expand