Skill Dev1 - Yang Bai
Made by yangb
Made by yangb
This is the first skill develope assignment of Design of IoT.
Created: November 21st, 2020
int ledPin = D2;
int ledPin2 = D3;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // blink the led1 once
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin2, HIGH); // blink the led2 once
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
}
Click to Expand
ledblinking.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// name the pins
int ledPin = D2;
void setup()
{
//Register our Particle function here
Particle.function("led", ledControl);
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
}
void loop()
{
// Nothing to do here
}
int ledControl(String command)
{
int state = LOW;
// find out the state of the led
if(command == "HIGH"){
for(int i = 0; i < 3; i++){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}else if(command == "LOW"){
state = LOW;
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
Ready.Last Event: particle/device/updates/pending = false Yang_Argon v1.5.2
Click to Expand
// name the pins
int ledPin = D2;
void setup()
{
//Register our Particle function here
Particle.function("led", ledControl);
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
}
void loop()
{
// Nothing to do here
}
int ledControl(String command)
{
int state = LOW;
int times = command.toInt();
// find out the state of the led
if (times > 0){
while(times > 0) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
times--;
}
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
Click to Expand
// name the pins
int ledPin = D2;
int ledPin2 = D3;
void setup()
{
//Register our Particle function here
Particle.function("led1", ledControl1);
Particle.function("led2", ledControl2);
// Configure the pins to be outputs
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize both the LEDs to be OFF
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
}
void loop()
{
// Nothing to do here
}
int ledControl1(String command)
{
int state = LOW;
int times = command.toInt();
// find out the state of the led
if (times > 0){
while(times > 0) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
times--;
}
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin, state);
return 1;
}
int ledControl2(String command)
{
int state = LOW;
int times = command.toInt();
// find out the state of the led
if (times > 0){
while(times > 0) {
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
times--;
}
}else{
return -1;
}
// write to the appropriate pin
digitalWrite(ledPin2, state);
return 1;
}
Click to Expand
I think one method I find it really useful is .toInt(). At first, I don't know how to transform a string to an int. It took me some time to figure it out.
Besides, for the exercise 2-3, I want to reuse the function ledControl. But I don't know how to import the ledPin as a parameter:( I wish I can figure it out soon.