Week 5 (Buttons)

I was sick this past Tuesday so I wasn’t able to meet in person with Professor Siek, but she was able to give me this week’s assignment via email which focused on buttons.

Projects:

Simple Button

My first assignment was to get some online code from http://lilypadarduino.org/?page_id=216 for a sensing switch/button to work on my Lilypad. I first read through the code to understand how the button was going to work and found it pretty simple. All that the setup function did was establish the LED pin as an output, the button pin as an input, and set the default (unpressed) state of the button pin to HIGH. Then the loop function simply consisted of creating a variable to determine if the button was being pressed and an if/else function that would turn the LED on if the button was being pressed or leave it off if it wasn’t. In addition to the code being very simple, the physical setup of this project was really easy as all I needed to do was connect a button and an LED to the Lilypad.

Button Code

Button Sensor and LEDs

The second assignment was a much greater challenge than the first. In order to complete the second assignment I needed to create a system that involved a button, a light sensor, and 3 LEDs. Whenever the button was pressed I would need to store the value of the light sensitivity being picked up by the sensor as a variable called my threshold and make the 3 LEDs blink to show that this value had been recorded. After the value of the threshold was recorded I would need the system to compare the current value of the light sensitivity to my threshold value and make the LEDs gradually turn on/turn off as the sensor’s values got closer to/farther from the value of the threshold. Finally, I would need to create a counter that would tell me how many times the sensor’s value had equaled or exceeded my threshold value.

I ran into a couple problems with this assignment, but none of them were too big and I was able to work them out. My first problem involved my light sensor which wasn’t giving me consistent values and was giving me random values in the hundreds whereas the values read by the light sensor have usually been below 100. I’m not exactly sure why I was getting these inconsistent values, but I was able to resolve the issue by disconnecting my sensor and then reconnecting it to the Lilypad.

Another issue that I had with this assignment involved my thresholdCounter, which was only supposed to increase when the sensorValue equaled or exceeded the threshold value, but it was increasing with every loop. I quickly realized this was because my threshold was initialized to 0 before I pressed the button and stored a new threshold so the sensorValue, which couldn’t be negative, would always equal or exceed my initial threshold value. I resolved this problem by initializing my threshold to 1000, a number which was high enough so that the sensorValue could never meet the threshold or be greater than 1/3 of the threshold (the value required for the first light to turn on) until after the button was pressed and the threshold was lowered.

All in all I think this project was a good challenge and will be very applicable to the Muscle Memory knee brace that I am working towards creating.

 

Button Sensor and LEDs