Skill Dev1 - Yang Bai

Made by yangb

This is the first skill develope assignment of Design of IoT.

Created: November 21st, 2020

0

Making an LED Blink:
Exercise 1

0

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

0
int ledPin = D2;

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

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(3000);
    digitalWrite(ledPin, LOW);
    delay(3000);
}
Click to Expand
0
0

Exercise 2

0

  blink on and off 5 times then stop for 3 sec  

0
int ledPin = D2;

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

void loop() {
    int i = 5;
    while ( i > 0 ){
        digitalWrite(ledPin, HIGH); // blink the led for 5 times
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
        i = i - 1;
    }
    delay(2500); // stop for 3 seconds
}
Click to Expand
0
0

Exercise 3

0

  add a second LED to the circuit and alternate blinks  

0
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
0
0

Making a Connected LED:
Exercise 1

0

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

0
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
0
0

Exercise 2

0

  Instead of passing HIGH or LOW, pass the number of times you 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
// 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
0
0

Exercise 3

0

Go back to the original program. Now add a second LED to the circuit.
Change the program and cloud function to allow you to control both LEDs remotely    

0
// 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
0
0

Process & Reflection

0

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. 

x
Share this Project

Courses

About

This is the first skill develope assignment of Design of IoT.