Skill Dev I - Wei Liang

Made by Wei Liang

Created: October 30th, 2020

0

Outcome and Process

This is our first lab in DIoT class. I am able to create a connected LED internet appliance under the guidance and tutorials.

0

First Sketch: Making an LED Blink

Exercise 1

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

This is a warmup - the only thing we need to change is the delay time. The outcome is shown in the video.

0
int ledPin = D2;

void setup() {
  // We want to tell the Argon that we'll use
  // D2 as an output pin.
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // First... On
  digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
  delay(3000);               // Wait for 3000mS = 3 second

  // Now... Off
  digitalWrite(ledPin, LOW);   // Turn OFF the LED pins
  delay(3000);               // Wait for 3000mS = 3 second
  // rinse + repeat

}
Click to Expand
0
Exercise 1
Wei Liang - https://youtu.be/A0JzUt0V8Fk
0

Exercise 2

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

Things got a little bit trickier here - I use a for loop to achieve the 5 times blink.

I create an integer variable cnt and assign it to 5. By using it as a global variable and call it in the loop, I can easily change the times of blink I need without digging through the for loop to change the number. This method will extend to the cloud variable later on.

It should be noted that I put the delay outside the loop (after the loop) as 2500 ms since we already have a 500 ms delay in the last round of the loop. The outcome is shown in the video.

0
int ledPin = D2;
int cnt = 5;

void setup() {
  // We want to tell the Argon that we'll use
  // D2 as an output pin.
    pinMode(ledPin, OUTPUT);
}

void loop() {
    for(int i = 0; i < cnt; i++){
        // First... On
        digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
        delay(500);  
        // Now... Off
        digitalWrite(ledPin, LOW);   // Turn OFF the LED pins
        delay(500);               // Wait for 500mS = .5 second
    }
    delay(2500);
}
Click to Expand
0
Exercise 2
Wei Liang - https://youtu.be/ClBC1mN47pQ
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.

In the beginning, I was about to use separate digitalWrite functions for the two LEDs. Then I think it is more efficient to just use one pair of digitalWrite for two LED pins, and swap after each loop.

I use a temporary variable to finish the swap at the end of the loop function (Bubble swap). The outcome is shown in the video.

0
int ledPin1 = D2;
int ledPin2 = D4;
int currLit, currOff, temp;

void setup() {
    // We want to tell the Argon that we'll use
    // D2 and D4 as output pins.
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    currLit = ledPin1;
    currOff = ledPin2;
}

void loop() {
    // First... On
    digitalWrite(currLit, HIGH);   // Turn ON the LED pins

    // Now... Off
    digitalWrite(currOff, LOW);   // Turn OFF the LED pins
    delay(1000);               // Wait for 1000mS = 1 second
    // rinse + repeat
    
    temp = currLit;
    currLit = currOff;
    currOff = temp;

}
Click to Expand
0
Two LEDs Blink
Wei Liang - https://youtu.be/Pjtifm8p5_Y
0

Making a Connected LED

Here is an example of how I use API to call the cloud function as a comparison to do so on the console.

0
Connected LED - Using API
Wei Liang - https://youtu.be/y79ASEuhAFo
0

Exercise 1

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

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.

I am only showing one code chunk and video here because the code of Exercise can be used to achieve the outcome in Exercise 1. 

The way I convert the String command input to an integer variable is using atoi() function, from the C string library. Later on, I notice that I can use the attribute .toInt().

I added one section to return -1 if the command input is less than zero. It indicates the program encounters an error and the error code is -1.

0
// name the pins
int ledPin = D2;

void setup()
{
    // Configure the pins to be outputs
    pinMode(ledPin, OUTPUT);

    // Initialize both the LEDs to be OFF
    digitalWrite(ledPin, LOW);
    
    //Register our Particle function here
    Particle.function("ledb", ledBlink);
}

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

int ledBlink(String cntInput)
{
    int cnt = atoi(cntInput);
    if (cnt <= 0){
        return -1;
    }
    for(int i = 0; i < cnt; i++){
        // First... On
        digitalWrite(ledPin, HIGH);   // Turn ON the LED pins
        delay(1000);  
        // Now... Off
        digitalWrite(ledPin, LOW);   // Turn OFF the LED pins
        delay(1000);               // Wait for 500mS = .5 second
    }
    // Make sure the LEDs to be OFF
    digitalWrite(ledPin, LOW);
    return 1;
}
Click to Expand
0
LED Blink
Wei Liang - https://youtu.be/L98FXXH8jsI
0

Exercise 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.

I use two cloud function here - 

  1. The cloud function to select which LED we want to control by typing in the pin number;
  2. The cloud function of LED control, the same as the above exercises.

I also use a cloud variable to show on the console what the current LED selected is. The outcome is shown in the video.

0
// name the pins
int ledPin1 = D2, ledPin2 = D4, ledPin;

void setup()
{
    // Configure the pins to be outputs
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);

    // Initialize both the LEDs to be OFF
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    
    //Register our Particle function here
    Particle.function("led", ledControl);
    Particle.function("ledsel", ledSelect);
    Particle.variable("ledID", ledPin);
}

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

int ledSelect(String ledPinInput)
{
    if(ledPinInput == "D2"){
        ledPin = ledPin1;
        return 1;
    }else if(ledPinInput == "D4"){
        ledPin = ledPin2;
        return 1;
    }
    else{
        return -1;
    }
}

int ledControl(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(ledPin, state);
    return 1;
}
Click to Expand
0
Connected 2 LEDs
Wei Liang - https://youtu.be/cx1zNvLcqhI
0

Analog

I only did Exercise 2 for the analog part since the first one is an extension of previous examples.

The punchline is to assign one LED pin as the ledValue, and another one ledMax - ledValue.

The outcome is shown in the video.

0
// name the pins
int ledPin1 = D2;
int ledPin2 = D4;
int ledMax = 255;
int ledMin = 0;

// create a variable to store the
// current brightness of the LED
int ledValue = 0;

void setup()
{
    //Register our Particle function to allow
    // Control of the LED
    Particle.function("led", ledControl);

    // Make the variable 'ledValue' available through
    // the Particle cloud as 'brightness'
    Particle.variable("brightness", ledValue);

    // Set up pin for output
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
}

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

int ledControl(String command)
{
    // Convert the passed variable to an integer
    ledValue = command.toInt();

    // Check it is a valid number
    if( ledValue > ledMax ) return -1;
    if( ledValue < ledMin ) return -1;

    // Use PWM to set the brightness
    // of the LED
    analogWrite(ledPin1, ledValue);
    analogWrite(ledPin2, ledMax - ledValue);
    delay(5000);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    // Return 1 to say completed successfully
    return 1;
}
Click to Expand
0
Analog - Exercise 2
Wei Liang - https://youtu.be/6mZpG47vqWg
0

Built-In RGB LED

I only did one experiment, the built-in RGB LED for the bonus part since my RGB LED has not arrived.

I read the tutorial that handles the input of three RGB values and find out it is quite smart. However, I decide to use an alternative method to parse the command input.

The function I use is strtok(). It uses "tokens" to parse a character array and get the value between the delimiter.

The function strtok() only works for character array pointer so I convert the command input from string to character array using .toCharArray(). The delimiter in our input is "," so we can get the three numbers around the two ",".

The outcome is shown in the video.

0
int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255

void setup()
{
    //Register our Particle function here
    Particle.function("led", ledControl);
    
    Particle.variable("Red", redValue);
    Particle.variable("Green", greenValue);
    Particle.variable("Blue", blueValue);
}

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

// This function gets called whenever there is a matching API request
// the command string format is <red>,<green>,<blue>
// for example: 255,0,128 or 128,0,0

int ledControl(String command)
{

    String colors[3];
    char charBuf[20];
    char *token;
    const char *s = ",";
    int i = 1;
    command.toCharArray(charBuf, 20);
    /* get the first token */
    token = strtok(charBuf, s);
    colors[0] = token;
    /* walk through other tokens */
    while( token != NULL ) {
        token = strtok(NULL, s);
        colors[i] = token;
        i++;
   }
    
    redValue = colors[0].toInt();
    greenValue = colors[1].toInt();
    blueValue = colors[2].toInt();


    // We need to say we'll be controlling the RGB led
    RGB.control(true);

    // set it to white
    RGB.color(redValue,greenValue,blueValue);

    // wait 3 seconds
    delay(3000);

    // return control of the RGB led to Particle
    RGB.control(false);


   return 1;
}
Click to Expand
0
Built-in RGB
Wei Liang - https://youtu.be/aVGMn8ZLX8U
0

Reflection

I learned how to use the Particle online IDE and console. I also learned how to call the cloud function in the console and using API.

This is fun and I learned a lot.

x