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.
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.
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.
Modify the cloud function to blink the LED 3 times after it is called
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.
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 -
I also use a cloud variable to show on the console what the current LED selected is. The outcome is shown in the video.
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.
You can upload files of up to 20MB using this form.