Sunday, September 22, 2013

Lab 3 - Arduino Material - Digital Input

Microprocessors can be used to read signals from the outside environment.

The command that can be used to read the state of a digital pin is digitalRead.

A simple way to see how this works is to use a pushbutton to toggle the voltage of an I/O pin between +5V and ground.

The following circuit created using Frizting shows one possible way that this can be accomplished.


So what's happening here??


  1. The Arduino board is used to supply both +5V and GND to the rails of the breadboard.
  2. The left pushbutton is used to light an LED in a simple electrical circuit. 
  3. The right pushbutton is used to control the voltage state of digital I/O pin 2.
    • Pin 2 is connected to both one leg of the pushbutton and to +5V through a 10k resistor. So when the button is open, pin 2 is HIGH.
    • The other leg of the pushbutton is connected to GND. When the button is closed the voltage of Pin 2 is "pulled-down" to GND. 
  4. The state of Pin 2 is determined using digitalRead  in the program loop. When the program determines that the button has been pushed, it sends a  digitalWrite(13, HIGH)  this results in the other LED to turn on.
There a a few more things that need to be explored and these will be covered in lab.

Using the above set-up explore the behavior of the example Sketches...

Button
StateChangeDetection
Debounce

After this exploration - take a look at the following code that combines some of these ideas together.

    


Next steps...

Modify the circuit such that indicator LED's are connected to pins 9, 10, 11, and 12.


The following code is a variation of the above code where functions are used to better define what is happening.

The Loop section only contains calls to a series of user defined functions and then updates to loop variables. The functions called are

debounceButton - this checks the current state of the button
checkForChange - this checks to see if the button has switched state (HIGH to LOW or LOW to HIGH)
getButtonNumber - this will increment the button counter it a change from a HIGH to LOW button state has been detected
indicatorLight - this turns on the LED associated with the button value. Either LED 1, 2, 3, or 4, on the 5th button push all LED's are tunred off.

The code for this setup is shown below the video...




Going even further...

One of the students in the class created a project that uses a sine wave to control an array of LED's. A pushbutton allows the user to toggle between different input modes and a potentiometer is used to adjust the parameter in each mode - phase, wave speed and brightness.

This project introduces two new functions, analogWrite and analogRead. The analogWrite function names use of pulse-width modulation (PWM) to control the brightness of the LED's. The analogRead function is used to read the value of the potentiometer. You can explore these two functions using the Arduino Example sketches...

analogWrite: File... Examples... Analog... Fading
analogRead: File... Examples... Analog... AnalogInput

The code is also a very nice example of both how to use functions to make your code easier to read and the proper use of comments to provide clarity.

Here is a video of the system in action. The code used to create this set-up is listed after the video.



No comments:

Post a Comment