# Skills Dev I: A Simple Internet Appliance by Everett

Learn to make LEDs blink and simple connected internet appliance.

Created: November 3rd, 2022

0

## Intention

### First Sketch

Exercise 1.

Modify the program to Blink on and off every 3 seconds.

0
``````// Exercise 1_1

int led = D2;

void setup() {
pinMode( led, OUTPUT );
}

void loop() {
diditalWrite(led, HIGH);
delay(3000);
diditalWrite(led, LOW);
delay(3000);
}``````
Click to Expand
0

Exercise 2

Change the program to blink on and off 5 times then stop for 3 seconds. Each blink should be 0.5s.

0
``````// Exercise 1_2

int led = D2;

void setup() {
pinMode( led, OUTPUT );
}

void loop() {
for(int i = 0; i < 5; i++){
digitalWrite(led, HIGH);
delay(500);

digitalWrite(led, LOW);
delay(500);
}
digitalWrite(led, LOW);
delay(3000);
}``````
Click to Expand
0

Exercise 3

Add a second LED to the circuit. Program the LED’s to alternate blinks i.e. when LED 1 turns on, LED 2 turns off, then when LED 2 turns on, LED 1 turns off.

0
0
``````// Exercise 1_3

int led1 = D2;
int led2 = D3;

void setup() {
pinMode( led1, OUTPUT );
pinMode( led2, OUTPUT );
}

void loop() {

digitalWrite(led2, HIGH);

for(int i = 0; i< 5; i++){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(500);

digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
}

delay(3000);
}``````
Click to Expand
0

### Connected LED

Exercise 1

Modify the cloud function to blink the LED 3 times after it is called.

0
``````//Exercise 2_1

int ledPin = D2;

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Particle.function("ledControl", myLedControl);
}

void loop() {

}

int myLedControl(String command) {
int state = LOW;
if (command == "ON") {
for(int i = 0; i <= 3; i++) {
if(i % 2 == 0) {
state = HIGH;
digitalWrite(ledPin, state);
delay(500);
} else {
state = LOW;
digitalWrite(ledPin, state);
delay(500);
}
}
return 1;
} else if(command == "OFF") {
state = LOW;
digitalWrite(ledPin, state);
return -1;
}
return 0;
}``````
Click to Expand
0

Exercise 2

Pass the number of times user would like it to blink.
Set the function to blink that number of times.
Finally once it has completed all of the blinking it should turn the LED off.

0
``````//Exercise 2_2

int ledPin = D2;

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Particle.function("ledControlByTimes", myLedControl);
}

void loop() {

}

int myLedControl(String command) {
int times = command.toInt();
int state = LOW;
for(int i = 1; i <= times * 2; i++) {
if(i % 2 == 0) {
state = HIGH;
digitalWrite(ledPin, state);
delay(500);
} else {
state = LOW;
digitalWrite(ledPin, state);
delay(500);
}
}
digitalWrite(ledPin, LOW);
return 1;
}``````
Click to Expand
0

Exercise 3

Add a second LED to the circuit.
Change the program and cloud function to allow you to control both LEDs remotely.

0
``````//Exercise 2_3

int led1 = D2;
int led2 = D3;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Particle.function("ledControlByTimes1", myLedControl1);
Particle.function("ledControlByTimes2", myLedControl2);
}

void loop() {

}

int myLedControl1(String command) {
int times = command.toInt();
int state = LOW;
for(int i = 1; i <= times * 2; i++) {
if(i % 2 == 0) {
state = HIGH;
digitalWrite(led1, state);
delay(500);
} else {
state = LOW;
digitalWrite(led1, state);
delay(500);
}
}
digitalWrite(led1, LOW);
return 1;
}

int myLedControl2(String command) {
int times = command.toInt();
int state = LOW;
for(int i = 1; i <= times * 2; i++) {
if(i % 2 == 0) {
state = HIGH;
digitalWrite(led2, state);
delay(500);
} else {
state = LOW;
digitalWrite(led2, state);
delay(500);
}
}
digitalWrite(led2, LOW);
return 1;
}``````
Click to Expand
0
Exercise 3
Everett Jiaxi Gu
0

## Process

I completed several parts of the exercise by referring to the instructions listed on the DIoT website, trying to adjust the details and order of parts of the sample code along the way to see if there were different results. In addition to the parts described in the guide, I spent more time practicing connecting and organizing the circuit, checking the code for syntax errors such as the placement of parentheses and spaces, and reading the programming syntax files.

0

## Reflection / Next Steps

1. I have questions about controlling two LEDs separately in Exercises 2-3. The LED blinks as expected when the two functions are executed separately. If I enter the number of times and execute both functions at the same time, there will be an error message, I think there may still be problems with some operations.

2. Need to spend time to read the programming guide document.

3. Some hardware still need to be familiar with, such as switching network, dealing with network and synchronization latency issues.