Difference between revisions of "Classes/2010/VIS147A/Lab7"

From Robert-Depot
Jump to: navigation, search
(Part 4 - Dot Matrix)
 
(43 intermediate revisions by the same user not shown)
Line 21: Line 21:
 
* computer with Arduino programming environment installed
 
* computer with Arduino programming environment installed
  
=== From last week ===
+
== Activities ==
* You should have the basic Blink example running (File->Sketchbook->Examples->Blink)
+
=== Setup ===
** This means you have soldered the board correctly.
+
* FROM LAST WEEK: You should have the basic Blink example running (File->Sketchbook->Examples->Blink). This means you have soldered the board correctly, and you can compile and upload files. We are going to extend from there.
** You can compile and upload files.  
+
* Power from arduino to breadboard.  Connect GND and +5 to the positive and negative power buses on the breadboard.
* We are going to extend from there.
 
 
 
=== Set up Breadboard ===
 
 
 
# Power from arduino to breadboard:
 
##connect positive and negative power buses on proto board
 
#run power, "5V" and "GND" from arduino to breadboard.
 
 
[[Image:Power_hookup.png|200px]]
 
[[Image:Power_hookup.png|200px]]
# verify that this worked (with multimeter, or LED test).
+
* verify that this worked (with multimeter).
 
[[Image:Verify_power.jpg|200px]]
 
[[Image:Verify_power.jpg|200px]]
  
=== Simple Digital Out ===
+
=== Part 1 - Simple Digital Out ===
# Set up LED on external board, check that it works
+
==== Hardware ====
# Connect the LED to one of your Digital Out pins.(Pins 0-13 on the "Digital" side of your board, by the power light.)
+
* Set up an LED on the breadboard with a current limiting resistor.
[[Image:Led_output.png|400px]]
+
[[Image:Led_output.png|400px]][[Image:Led_out_on_breadboard.jpg|400px]]
[[Image:Led_out_on_breadboard.jpg|400px]]
+
* Connect the LED to one of your Digital Out pins.(Pins 0-13 on the "Digital" side of your board, by the power light.)
''Basic LED Driver''
+
* Change the Blink program (from Sketchbook->Examples->Digital) to output to that pin you connected.
# change Blink program (from Sketchbook->Examples->Digital) to output to that pin you connected.
+
** use <code>pinMode()</code> to set your Digital Output pin to be an <code>OUTPUT</code>.
# Compile, upload, and run it.... Does it work?
+
** use <code>digitalWrite()</code> to the correct pin to turn on (or turn off) the LED.
# try changing the timing (HINT: the <code>delay(1000)</code> statement in Blink example)
+
* Compile, upload, and run the program.... Does it work?
 
+
* What is V_OUT (measured at the microcontroller pin) when the light is on ____.
=== Simple Digital In ===
+
* What is V_OUT (measured at the microcontroller pin) when the light is off ____.
[[Image:switch.jpg|thumb]]
+
* (Slow down the timing if you need to, to measure these).
[[Image:D_in.png]]
+
* Change the timing in some way. (HINT: the <code>delay(1000)</code> statement in the Blink example is what sets the time on and time off). Using what you know, create a different pattern or sequence of activation for the LED. (Massimo Banzi: 'One Circuit, A Thousand Behaviors' p 45-52).
 
+
====  Code ====
''Active High, Normally Open (N.O.) switch''
+
* In the simple blink example, this code
# Set up this circuit on your breadboard, with a switch, +5V, and current limiting resistor to ground. This is a basic digital input.
 
# Open the button example (File->Sketchbook->Examples->Digital->Button)
 
# Connect your switch circuit to on if the digital I/O pins, and change <code>inputPin</code> in the button program to this number. (Mine is Pin 7)
 
 
 
=== Some Programming ===
 
* In the simple blink exampe, this code here:
 
 
<code>
 
<code>
 
   digitalWrite(ledPin, HIGH);  // sets the LED on
 
   digitalWrite(ledPin, HIGH);  // sets the LED on
Line 63: Line 50:
 
   delay(1000);                  // waits for a second
 
   delay(1000);                  // waits for a second
 
</code>
 
</code>
* is what creates the behavior: half the time on, half the time off.  
+
is what creates the behavior: half the time on, half the time off.  
 
* The <code>digitalWrite(ledPin, HIGH)</code> turns on the LED
 
* The <code>digitalWrite(ledPin, HIGH)</code> turns on the LED
 
* <code>digitalWrite(ledPin, LOW)</code> turns off the LED.
 
* <code>digitalWrite(ledPin, LOW)</code> turns off the LED.
* What is this actually doing?  The <code>HIGH</code> is setting that output pin to High, 1, True, +5V.  
+
* What is this actually doing?  The <code>HIGH</code> is setting that output pin to High or +5V. You can measure this with your multimeter while the example is running.
* similarity, the <code>LOW</code> is setting that output pin to Low, 0, False, 0V or GND.
+
* Similarly, the <code>LOW</code> is setting that output pin to LOW or GND (0V). Again, this is measurable with the multimeter.  
 
* This is where your programmable, internal world of the microcontroller is interfacing with the hardware, LED world of intro to electronics.  
 
* This is where your programmable, internal world of the microcontroller is interfacing with the hardware, LED world of intro to electronics.  
* We needed to do some setup first <code>pinMode(ledPin, OUTPUT)</code>
+
* Other code: We needed to do some setup first <code>pinMode(ledPin, OUTPUT)</code> so the microcontroller knows whether to use those pins as inputs or outputs.  After that, output is simply a matter of <code>digitalWrite</code> calls.
* and then it is a matter of <code>digitalWrite</code> calls.
+
 
* Similarly, in the digital_in example above, we do some setup <code>
+
=== Part 2 - Simple Digital In ===
 +
==== Hardware ====
 +
[[Image:Digital_in.png|300px]]
 +
[[Image:switch.jpg|400px]]
 +
 
 +
''Active High, Normally Open (N.O.) switch''
 +
* Set up this circuit on your breadboard, with a switch, +5V, and current limiting resistor to ground. This is a basic digital input.
 +
* With your multimeter, check the output voltage of the circuit (V between button and resistor):
 +
** V_OUT with the button pressed ____.
 +
** V_OUT with the button released ____.
 +
* Open the button example (File->Sketchbook->Examples->Digital->Button) in the Arduino software.
 +
* Connect your switch circuit to one of the digital I/O pins on your Freeduino, and change <code>inputPin</code> in the button program to correspond to this pin. (Mine is Digital Pin #7)
 +
* Compile, upload and run the Button program, verify that it works.  You should see your external LED blinking in time with the program.
 +
==== Code ====
 +
* In the Button example above, we do some setup  
 +
<code>
 
   pinMode(ledPin, OUTPUT);      // declare LED as output
 
   pinMode(ledPin, OUTPUT);      // declare LED as output
 
   pinMode(inputPin, INPUT);    // declare pushbutton as input
 
   pinMode(inputPin, INPUT);    // declare pushbutton as input
 
</code>
 
</code>
 
* And then we read inputs and make outputs with <code>digitalRead</code> and <code>digitalWrite</code> calls.
 
* And then we read inputs and make outputs with <code>digitalRead</code> and <code>digitalWrite</code> calls.
* If you ever seen anything in a program, and you want to learn more about what it does, for example digital read, hilight the term, and select Help->Find in reference.
+
* If you ever seen anything unfamiliar in a program and you want to learn more about what it does, for example <code>digitalRead()</code>, highlight the term and right-click->Find in Reference (or select Help->Find in reference in the menu). This will pull up the Arduino help files.  There is a lot to be found here. Very useful.  
** This will pull up the arduino reference files.  There is a lot to be found here. Very useful.  
+
* In this example, you could insert your own code, for instance:
=== So what can we change? ===
 
* You could insert your own code, for instance
 
 
<code>
 
<code>
 
   digitalWrite(ledPin, HIGH);  // sets the LED on
 
   digitalWrite(ledPin, HIGH);  // sets the LED on
Line 90: Line 90:
 
   delay(1000);                  // waits for one second
 
   delay(1000);                  // waits for one second
 
</code>
 
</code>
* to get a "long-short-long-short" blinking.  
+
* to get a "long-short-long-short" blinking. Try this out.  
** Try this out.  
+
* These kinds of patterned blinkings suggest the idea of coded communications.  For instance Morse code is a very simple way to encode information as on/off pulses (LONG and SHORT),and transmit them over a distance.  What are some other examples of codes of that sort?
* As you add more complicated behavior, things are going to get ridiculous very soon, you will have tons of special cases, lines and lines of code. There are more efficient ways to do this...
+
* As you desire more complicated behavior, your program is going to get ridiculous very soon, with endless lists of <code>digitalWrite()</code> and <code>if</code> statements. There are more efficient ways to do this... for instance using <code>for</code> loops, <code>switch</code> statements, and arrays.
* What are more efficient ways to do this, use For loops or arrays.
 
  
=== Loop (Optional) ===
+
=== Part 3 - For Loop, Iteration ===
* Specifically, looping across multiple outputs.
+
==== Hardware ====
* File->Sketchbook->Examples->Digital->Loop
+
* multiple LEDs, multiple 220 Ohm resistors.  The ForLoopIteration example (below) expects six LEDs, though you could alter the program to run with less.  For instance I set up 3 LEDs in the image below.
* Same as single input above, connect multiple LED outputs.
+
[[Image:Three_lights.jpg|400px]]
 +
==== Code ====
 +
* Open File->Sketchbook->Examples->Control->ForLoopIteration.  This program is similar to the single input above, however with multiple outputs at once. It uses a <code>loop</code> statement to run across a series of pins rather than having to write each output as a separate line.
 
* Compile, upload, and run the loop example.
 
* Compile, upload, and run the loop example.
* Change the behavior of the loop: how about instead of equal on/off timing, change it in some other way.  
+
* Try changing the value of the <code>timer</code> variable.  As the comment in the program says, <code>// The higher the number, the slower the timing.</code>. Conversely, the smaller the value the faster the timing. At what value of <code>timer</code> do the lights appear to stay on continuously? ____.
* Can you make the loop run through a series of meaningful states, rather repeating a simple on/off behavior.
+
* Remember my multiplexed display example in class, I was technically turning each column on one-at-a-time, but past a point it looked as if the whole smiley-face image was on non-stop.  This has to do with the idea of [http://en.wikipedia.org/wiki/Persistence_of_vision persistence of vision]. At what value for <code>timer</code> does it appear that all LEDs are on continually ____.
* Differently, with only one output, how could you make a program that changes over time using a loop.
+
* What does this correspond to in Hertz (Cycles/Second).  Calculate this by taking 1 / TIME_ON.  (For instance, 1 / 100 ms = 1 / 0.100 s = 10 Hz).  ____. 
 +
* Examples of displays utilizing POV:
 +
** http://www.ladyada.net/make/minipov3/index.html
 +
** bike wheel poke POV http://www.instructables.com/id/SpokePOV%3a-LED-Bike-Wheel-Images/
 +
** http://www.youtube.com/watch?v=mT13ZcpwYtA
 +
* Extension: Instead of equal on/off timing, can you make the loop run in a different way?  Spelling out key presses for a piano keyboard, something like that. Can you make the loop run through a series of meaningful states, rather repeating a simple on/off behavior?  Alternately, with only one output, how could you make a program that changes over time using a loop?
 +
 
 +
=== Part 4 - Dot Matrix ===
 +
* Working in pairs, set up six or more digital LED outputs.  Set these up as a dot matrix display (say 2 X 3 or 3 X 2 or 3 X 3) rather than in a line. Technically you have 13 digital outputs to work with (4 X 3 dot matrix?).
 +
* The code and hardware here are the same as driving a single LED, or multiple LEDs in a line.  What you are changing is the spatial configuration of the LEDs.  Having them arrayed in a box reads differently than having them in a line--now they are "pixels" instead of single lights.
 +
* Experiment with code and output to get something to happen.  Bounce a ball (tricky!), draw a smiley,... this is an open exercise.
  
 +
== Homework ==
 
=== Thoughts ===
 
=== Thoughts ===
 
* The idea of encoding information.  
 
* The idea of encoding information.  
Line 117: Line 129:
  
 
=== Assignment ===
 
=== Assignment ===
* A quick thought/lab experiment for next week: Extend the example in some way, particularly focusing on the idea of framing.
+
* A quick thought/lab experiment: Extend one of the examples in some way, particularly focusing on the idea of framing (e.g. the idea of establishing a context for your project).  
 
** This could be through:
 
** This could be through:
 
*** the addition of multiple different inputs.  
 
*** the addition of multiple different inputs.  
 
*** ...multiple different outputs.  
 
*** ...multiple different outputs.  
 
*** ...create a time-varying signal.  (code?)
 
*** ...create a time-varying signal.  (code?)
 +
*** ...create a simple display. (dot-matrix?)
 
*** change through packaging, framing, context.  
 
*** change through packaging, framing, context.  
*** getting off of the breadboard (or tucking the breadboard inside of something) how could you change what the LED indicates.
+
*** getting off of the breadboard (or hiding the breadboard) how could you change what the LED indicates.
 
** Some of these are technical challenges, some or more concept based.  
 
** Some of these are technical challenges, some or more concept based.  
** For this assignment, I am interested in the more concept based... having an idea you want to make.
+
** For this assignment (as always!) I am interested in the more concept based approaches, having an idea you want to make.  Due in lab next week.
 +
* DUE IN LAB NEXT WEEK.  We will look at particular interesting/daring examples at the beginning of that lecture.

Latest revision as of 09:49, 23 February 2010

Lab 7 - Digital Input/Output, Microcontrollers

Topics

  • Digital input, output from Freeduino.
  • basic LED driver circuit for Freeduino.
  • Basic Freeduino programming:
    • delay()
    • digitalRead()
    • digitalWrite()
    • pinMode()
  • Compiling and uploading sketches.

Materials

  • red LED
  • 220 Ohm resistor
  • hookup wire
  • wire clippers, strippers, pliers.
  • breadboard
  • fully assembled Freeduino
  • USB cable
  • computer with Arduino programming environment installed

Activities

Setup

  • FROM LAST WEEK: You should have the basic Blink example running (File->Sketchbook->Examples->Blink). This means you have soldered the board correctly, and you can compile and upload files. We are going to extend from there.
  • Power from arduino to breadboard. Connect GND and +5 to the positive and negative power buses on the breadboard.

Power hookup.png

  • verify that this worked (with multimeter).

Verify power.jpg

Part 1 - Simple Digital Out

Hardware

  • Set up an LED on the breadboard with a current limiting resistor.

Led output.pngLed out on breadboard.jpg

  • Connect the LED to one of your Digital Out pins.(Pins 0-13 on the "Digital" side of your board, by the power light.)
  • Change the Blink program (from Sketchbook->Examples->Digital) to output to that pin you connected.
    • use pinMode() to set your Digital Output pin to be an OUTPUT.
    • use digitalWrite() to the correct pin to turn on (or turn off) the LED.
  • Compile, upload, and run the program.... Does it work?
  • What is V_OUT (measured at the microcontroller pin) when the light is on ____.
  • What is V_OUT (measured at the microcontroller pin) when the light is off ____.
  • (Slow down the timing if you need to, to measure these).
  • Change the timing in some way. (HINT: the delay(1000) statement in the Blink example is what sets the time on and time off). Using what you know, create a different pattern or sequence of activation for the LED. (Massimo Banzi: 'One Circuit, A Thousand Behaviors' p 45-52).

Code

  • In the simple blink example, this code

 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for a second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for a second

is what creates the behavior: half the time on, half the time off.

  • The digitalWrite(ledPin, HIGH) turns on the LED
  • digitalWrite(ledPin, LOW) turns off the LED.
  • What is this actually doing? The HIGH is setting that output pin to High or +5V. You can measure this with your multimeter while the example is running.
  • Similarly, the LOW is setting that output pin to LOW or GND (0V). Again, this is measurable with the multimeter.
  • This is where your programmable, internal world of the microcontroller is interfacing with the hardware, LED world of intro to electronics.
  • Other code: We needed to do some setup first pinMode(ledPin, OUTPUT) so the microcontroller knows whether to use those pins as inputs or outputs. After that, output is simply a matter of digitalWrite calls.

Part 2 - Simple Digital In

Hardware

Digital in.png Switch.jpg

Active High, Normally Open (N.O.) switch

  • Set up this circuit on your breadboard, with a switch, +5V, and current limiting resistor to ground. This is a basic digital input.
  • With your multimeter, check the output voltage of the circuit (V between button and resistor):
    • V_OUT with the button pressed ____.
    • V_OUT with the button released ____.
  • Open the button example (File->Sketchbook->Examples->Digital->Button) in the Arduino software.
  • Connect your switch circuit to one of the digital I/O pins on your Freeduino, and change inputPin in the button program to correspond to this pin. (Mine is Digital Pin #7)
  • Compile, upload and run the Button program, verify that it works. You should see your external LED blinking in time with the program.

Code

  • In the Button example above, we do some setup

 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input

  • And then we read inputs and make outputs with digitalRead and digitalWrite calls.
  • If you ever seen anything unfamiliar in a program and you want to learn more about what it does, for example digitalRead(), highlight the term and right-click->Find in Reference (or select Help->Find in reference in the menu). This will pull up the Arduino help files. There is a lot to be found here. Very useful.
  • In this example, you could insert your own code, for instance:

 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(1000);                  // waits for one second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for one second
 digitalWrite(ledPin, HIGH);   // sets the LED on
 delay(500);                  // waits for half a second second
 digitalWrite(ledPin, LOW);    // sets the LED off
 delay(1000);                  // waits for one second

  • to get a "long-short-long-short" blinking. Try this out.
  • These kinds of patterned blinkings suggest the idea of coded communications. For instance Morse code is a very simple way to encode information as on/off pulses (LONG and SHORT),and transmit them over a distance. What are some other examples of codes of that sort?
  • As you desire more complicated behavior, your program is going to get ridiculous very soon, with endless lists of digitalWrite() and if statements. There are more efficient ways to do this... for instance using for loops, switch statements, and arrays.

Part 3 - For Loop, Iteration

Hardware

  • multiple LEDs, multiple 220 Ohm resistors. The ForLoopIteration example (below) expects six LEDs, though you could alter the program to run with less. For instance I set up 3 LEDs in the image below.

Three lights.jpg

Code

  • Open File->Sketchbook->Examples->Control->ForLoopIteration. This program is similar to the single input above, however with multiple outputs at once. It uses a loop statement to run across a series of pins rather than having to write each output as a separate line.
  • Compile, upload, and run the loop example.
  • Try changing the value of the timer variable. As the comment in the program says, // The higher the number, the slower the timing.. Conversely, the smaller the value the faster the timing. At what value of timer do the lights appear to stay on continuously? ____.
  • Remember my multiplexed display example in class, I was technically turning each column on one-at-a-time, but past a point it looked as if the whole smiley-face image was on non-stop. This has to do with the idea of persistence of vision. At what value for timer does it appear that all LEDs are on continually ____.
  • What does this correspond to in Hertz (Cycles/Second). Calculate this by taking 1 / TIME_ON. (For instance, 1 / 100 ms = 1 / 0.100 s = 10 Hz). ____.
  • Examples of displays utilizing POV:
  • Extension: Instead of equal on/off timing, can you make the loop run in a different way? Spelling out key presses for a piano keyboard, something like that. Can you make the loop run through a series of meaningful states, rather repeating a simple on/off behavior? Alternately, with only one output, how could you make a program that changes over time using a loop?

Part 4 - Dot Matrix

  • Working in pairs, set up six or more digital LED outputs. Set these up as a dot matrix display (say 2 X 3 or 3 X 2 or 3 X 3) rather than in a line. Technically you have 13 digital outputs to work with (4 X 3 dot matrix?).
  • The code and hardware here are the same as driving a single LED, or multiple LEDs in a line. What you are changing is the spatial configuration of the LEDs. Having them arrayed in a box reads differently than having them in a line--now they are "pixels" instead of single lights.
  • Experiment with code and output to get something to happen. Bounce a ball (tricky!), draw a smiley,... this is an open exercise.

Homework

Thoughts

  • The idea of encoding information.
  • Idea of input and response. Triggering behavior.
  • Framing: how does the meaning of a thing change through context. LEDs (or other lights) surround us, everywhere, to indicate all kinds of things.
    • "Record" light on a camera. Warning light.
    • think about contextualization... if the light is in an empty fuel gauge... warning light... how does its meaning change
    • how could you change the meaning through contextualization
    • what novel package or "frame" could change the meaning of a thing.

Assignment

  • A quick thought/lab experiment: Extend one of the examples in some way, particularly focusing on the idea of framing (e.g. the idea of establishing a context for your project).
    • This could be through:
      • the addition of multiple different inputs.
      • ...multiple different outputs.
      • ...create a time-varying signal. (code?)
      • ...create a simple display. (dot-matrix?)
      • change through packaging, framing, context.
      • getting off of the breadboard (or hiding the breadboard) how could you change what the LED indicates.
    • Some of these are technical challenges, some or more concept based.
    • For this assignment (as always!) I am interested in the more concept based approaches, having an idea you want to make. Due in lab next week.
  • DUE IN LAB NEXT WEEK. We will look at particular interesting/daring examples at the beginning of that lecture.