Skills Dev I: A Simple Internet Appliance
Made by Shih-Hsueh Wang
Made by Shih-Hsueh Wang
The goal is to understand the basic knowledge of Particle Argon regarding the circuit and codes.
Created: October 28th, 2022
Exercise 1 -1: Make an LED on and off with 3-sec intervals. Video
Exercise 1- 2: Make an LED blink a specific number of times. Video
Exercise 1-3: Make two LEDs in alternate blinks. Video
Exercise 2-1: Make a cloud-based control to blink an LED 3 times. Video
Exercise 2-2: Make a cloud-based control to blink an LED a customized number of times. Video
Exercise 2-3: Make a cloud-based control to blink two LEDs a customized number of times. Video
Based on the instructions on the DioT website, I tried to use the "for" loop to set a specific number of times instead of using multiple HIGH and LOW as the LED output.
I tried multiple times to use the combination of the "for" loop and the "if" condition to set up the program. Yet, it was not successful as the LEDs didn't light up in the way I expected. Fortunately, I figured out the problem, which is the order of the lines of code. Since I shifted the order of digitalWrite() to the lines within each curly bracket under the "if" conditions, I could achieve my intention.
//Exercise 1-3
int led1 = D2;
int led2 = D3;
void setup() {
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
}
void loop() {
for(int i = 0; i <= 4; i++){
if (i <= 4){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
}
}
}
Click to Expand
//Exercise 2-1
int led = D2;
int status = LOW;
void setup() {
pinMode (led, OUTPUT);
Particle.function ("ControlLight", lightControl);
digitalWrite(led, LOW);
}
void loop() {
}
//Cloud-based control
int lightControl (String command) {
if (command.equals ("on")){
for (int i = 0; i <= 5; i++){
if (i % 2 == 0){
status = HIGH;
delay(500);
digitalWrite (led, status);
}else{
status = LOW;
delay(500);
digitalWrite (led, status);
}
}
return 1;
}else if (command.equals("off")){
status = LOW;
digitalWrite (led, status);
return -1;
}else{
return 0;
}
}
Click to Expand
//Exercise 2-2
int led1 = D2;
int status = LOW;
void setup() {
pinMode (led1, OUTPUT);
pinMode(led2, OUTPUT);
Particle.function ("LEDlLight 1", ledControl1);
digitalWrite(led1, LOW);
}
void loop() {
}
//Cloud-based control
int ledControl1 (String command) {
int i = command.toInt();
for (i = 0; i <= command.toInt() * 2 - 1; i++){
if (i % 2 == 0){
status = HIGH;
delay(500);
digitalWrite (led1, status);
}else{
status = LOW;
delay(500);
digitalWrite (led1, status);
}
}
return i / 2;
}
Click to Expand
//Exercise 2-3
int led1 = D2;
int led2 = D3;
int status = LOW;
void setup() {
pinMode (led1, OUTPUT);
pinMode(led2, OUTPUT);
Particle.function ("LEDlLight 1", ledControl1);
Particle.function ("LEDlLight 2", ledControl2);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
void loop() {
}
//Cloud-based control
int ledControl1 (String command) {
int i = command.toInt();
for (i = 0; i <= command.toInt() * 2 - 1; i++){
if (i % 2 == 0){
status = HIGH;
delay(500);
digitalWrite (led1, status);
}else{
status = LOW;
delay(500);
digitalWrite (led1, status);
}
}
return i / 2;
}
int ledControl2 (String command) {
int i = command.toInt();
for (i = 0; i <= command.toInt() * 2 - 1; i++){
if (i % 2 == 0){
status = HIGH;
delay(500);
digitalWrite (led2, status);
}else{
status = LOW;
delay(500);
digitalWrite (led2, status);
}
}
return i / 2;
}
Click to Expand