Classes/2010/VIS147A/Lab7

From Robert-Depot
Revision as of 09:23, 23 February 2010 by Rtwomey (talk | contribs) (Part 4 - Looping over Multiple Digital Outputs)

Jump to: navigation, search

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

  • 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?
  • 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).

Part 2 - Simple Digital In

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.

Part 3 - Programming

Digital Out 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.

Digital In 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 4 - Looping over Multiple Digital Outputs

  • Set up six or more digital LED outputs similar to those in part 1. (work in pairs if you would like)

Three lights.jpg

  • 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.
  • Experiment with shorter and shorter timing until the point at which you are no longer able to discern individual LEDs turning on and off. 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 ____.
  • More examples of displays utilizing POV:
  • THOUGHTS: 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?

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 for next week: Extend the example in some way, particularly focusing on the idea of framing.
    • This could be through:
      • the addition of multiple different inputs.
      • ...multiple different outputs.
      • ...create a time-varying signal. (code?)
      • 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.
    • 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.