Project Overview
In this project I got acquainted with the particle platform to make a web-connected led. Using the Particle console environment the user can enter text to select an led to control, turn that led on or off and set the led to blink either a given number of times or indefinitely.
The code uses a couple strategies to handle user input and control of each led - a tree of if-else statements takes the user's command and matches it to a set command. For commands that set a variable I created a substring to split up the text and integer components of the command. A structure stores variables for the LED including pin, current state, timer and blink count so that any number of LEDs could operate and blink independently without interference. I managed to get a pointer to function so that I could more easily assign and re-assign values to the specified LEDs.
Process:
I have a good amount of experience with arduino devices so I used this lab as an opportunity to try a couple of methods I am less familiar with in order to make the code more functional - such as structs and pointers. After getting the "HIGH"/ "LOW" function to work I wrote a function to blink the led using a timer method so it doesn't cause the code to block. I made a counter that can be set and decreases after each blink to allow the user to set a number of blinks to show. I thought about making another app specifically for setting the blink number but decided to just add it via checking if the user writes "BLINK:[number of blinks]". The actual implementation just checks if the 6th character is ':' and converts the rest of the string to an integer. I used a similar method for setting the LED. You could write any input with the 3rd char as "D" and it would try to set the LED - so it's definitely not a perfect solution!
I tried for a while using a list of the LED objects but that method doesn't actually allow me to assign values to the LED objects in the list - I attempted a number of different methods to make an array of the LED objects but there are some confusing problems with how structs and arrays interact. I found with this code if there is an issue the IDE just refused to compile without an error - so after some gnashing of teeth I gave up and just set each LED address manually - eventually I'd like to return to this so I can more easily scale with any number of objects.
Pointers are very funky - they allow me to make sort of temporary objects that I can assign values to which then transfer to the pointed object - in this case I used a pointer pLED so that in the ledControl function I could first set pLED to whichever led the user had selected and then any variables would be assigned to that pointer. I think there are alternative methods like making a function for assigning values to the LED object - maybe a class? Below I posted my code prior to adding the second LED and the pointers.
One issue I definitely felt was the lack of a serial monitor for debugging the code - it was very difficult to figure out what was going on sometimes. I attempted to use the Particle build environment which did have a serial monitor although there were some different things that tripped me up with that method.
At some point I my code was crashing the board firmware if I entered more than one command - unsure what happened here but it got me acquainted with reflashing the firmware on the board. I expect this will come in handy in the future.
Next Steps:
As mentioned above there are a couple of less optimal bits in this code - namely handling user input and handling object pointing. I think I could figure out the latter with a little more time. For the former I would probably want to create a function to parse user input in a more flexible way.
Reflection:
I was definitely stoked to get my pointers working correctly as I still find pointers to be pretty confusing. I spent a lot more time on this lab than expected - but felt I have gotten a lot more familiar with troubleshooting the particle. Without the serial monitor and more info on how the web input works I feel a little in the dark about handling different user inputs - but I am sure that will become clearer over time.
One thing I really need to be better at is checking functions work before I write whole blocks of code that use them. I found myself commenting out several sections of code so I could figure out what wasn't working - especially with things like pointers where I was sometimes misusing them entirely. It would definitely save me time to validate a function first.