Skill Dev1: Jenny

Made by Yanling Zhang

The first iot practice.

Created: November 2nd, 2021

0

Exercise 1

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

Hint: You only need to make two changes. Read the comments to see where.

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
iot_ex1-1
zhang yanling - https://youtu.be/u8QJiTAXHck
0

Exercise 2

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

Hint: An easy way to do this is to copy and paste. A better way to do this is with a for loop!

0
int ledPin = D2;

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

void loop() {
    
    for (int e = 0; e < 5; e++) {        
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
    
    delay(3000);

}
Click to Expand
0
iot_ex1-2
zhang yanling - https://youtu.be/YNv6HdxgM3Q
0

Exercise 3

Go back to the original program. Now 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
int led1Pin = D2;
int led2Pin = D10;

void setup() {
    pinMode(led1Pin, OUTPUT);
    pinMode(led2Pin, OUTPUT);
    
}

void loop() {
        digitalWrite(led1Pin, LOW);
        digitalWrite(led2Pin, HIGH);
        delay(500);
        digitalWrite(led1Pin, LOW);
        digitalWrite(led2Pin, HIGH);
        delay(500);
        
}
Click to Expand
0
iot_ex1-3
zhang yanling - https://youtu.be/Jqom5683yME
0

Making a Connected LED: Exercise 1

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

0
int ledPin = D2;

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

void loop()
{
   // Nothing to do here
}


int ledControl(String command){
    //int state = LOW;

    if(command == "BLINK3TIMES"){
	    for (int e = 0; e < 3; e++){        
            digitalWrite(ledPin, HIGH);
            delay(500);
            digitalWrite(ledPin, LOW);
            delay(500);
	    }
    }
    else{
	    return -1;
	}
	    
    digitalWrite(ledPin, LOW);
    return 1;
}
Click to Expand
0

Making a Connected LED: Exercise 2

Modify the cloud function as follows: Instead of passing a HIGH Or LOW string 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
int ledPin = D2;

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

void loop()
{
   // Nothing to do here
}


int ledControl(String command){
    int number = atoi(command);
    if (number != 0){
        for(int e=0; e<number; e++){
            digitalWrite(ledPin, HIGH);
            delay(500);
            digitalWrite(ledPin, LOW);
            delay(500);
        }
        return 1;
    }
    else{
        return -1;
    }
    
}
Click to Expand
0

Making a Connected LED: Example 3

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.

1
int led1Pin = D2;
int led2Pin = D10;

void setup() {
    pinMode(led1Pin, OUTPUT);
    digitalWrite(led1Pin, LOW);
    pinMode(led2Pin, OUTPUT);
    digitalWrite(led2Pin, LOW);
    Particle.function("led1", led1Control);
    Particle.function("led2", led2Control);
    
}

void loop()
{
   // Nothing to do here
}


int led1Control(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(led1Pin, state);
   return 1;
}
    
    
int led2Control(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(led2Pin, state);
   return 1;
}
Click to Expand
0
iot_ex2-3
zhang yanling - https://youtu.be/aGD-znZGTQY
0

Reflection

In the first three exercises, I got familiar with the circuit, and the particle platform. I learned how to distinguish while loop and for loop with the help of Jaki.

In the last three exercises, I explored the Particle.function in this exercise, and also learned "int number = atoi(command);" to convert string to int.

When editing this page, I failed to upload the videos. It all turned into white blocks which I can't delete. I finally figure out this problem by uploading the video via Youtube:)

x