Back to Parent

Outcome


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.

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Show Advanced Options
Show Advanced Options

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.

Drop files here or click to select

You can upload files of up to 20MB using this form.