Sensors & I/O

     For the first part I spent some time getting set up with Fritzing and the Arduino IDE then got setup with the basic button example that was provided. The example worked great out of the box so it was pretty easy to modify it to use different inputs and outputs by changing the variables and the ports the wires were plugged into. I chose 4 as the button input and 12 as the new output and I also moved the LED to the breadboard with a resistor.


     The second part was more of a programming challenge I wanted to try for double click toggling the state of the LED and a long hold turning it on for as long as the button was held after the button was released. I used the same circuit layout as part on for this section.
     The double click was the harder one but I ended up with a click counter and a click delay variable. Each time the buttons state changed it compared the time the last state change was with the current time to decide if it should count as a click and then increment the click counter. When two clicks were counted it would call a method the changed the state from high to low and vice versa..
     The press and hold recorded the start and stop times of the click then calculated how long the button was held and passed that value of millis to a method that would turn the LED on then wait for that many millis then turn it off. This worked but would cause weird behavior when the button was pressed while the LED was on.
     For the third part I attached the potentiometer as per the example which was pretty cool even though I was under the impression the dial would change brightness. (After looking at it though it makes sense why it covers the delay instead of the brightness..). At this point my circuit looked like this.
 
To take it a bit farther I took a joystick I had laying around that had two analog outputs (one for x and one for y) and extended it to control the delays of two LEDs separately. This was interesting because the multiple inputs from one sensor were kind of intimidating but it was fun controlling two LEDs with the axis of a joystick.
On the fourth part to add sensors I hooked the potentiometer up to A2, and a photocell to A3 with two additional LEDs on pins 11 and 10. Each sensor was using similar code to turn the snsor value into a blink delay but with so many LEDs going at once I did switch from using delay() to milis().

Below is my setup for the modified PWM exercise. After getting the basic exercise to work with a few adjustments to the code pretty easily I decided to go back to using the joystick because I think its a fun sensor. This time I hooked up the button function of the joystick to turning the LED on or off and if it was on the x-axis of the joystick to the brightness. To get it to work I had to divide the sensor value by 4 and that seemed to keep the brightness value between 0-255.



Comments