Difference between revisions of "250a Firmware Lab"

From CCRMA Wiki
Jump to: navigation, search
(Serial communication with Max or PD)
(Low-latency sensing (optional))
Line 58: Line 58:
  
 
== Low-latency sensing (optional) ==
 
== Low-latency sensing (optional) ==
 +
 +
Now, try reimplementing your instrument from the end of Lab 2 in firmware! Program the Arduino firmware so that it only detects inputs and performs outputs to the pins you use, and modify your Lab 2 patch to work with it.
 +
 +
Your code on the Arduino should be running your program a lot faster than Firmata. Does your instrument work any better?
 +
 
=== Encoder input ===
 
=== Encoder input ===
  

Revision as of 17:48, 7 October 2009

Lab 3: Firmware Programming
Due on Wednesday, October 14th at 5PM

For this lab you need your MaxKit, and Max/MSP or PD on a computer.

The Autonomous Arduino

One of the major motivations for learning how to program firmware for the AVR is the opportunity to have the Arduino hardware do things without being connected to a computer. In this segment of the lab, we work on setting up the Arduino hardware so that it can work in autonomous mode.

Powering your Arduino via Battery

Previously we have powered our Arduino hardware using current from the computer via the USB connnection. However, to operate autonomously, we need to locally supply power. To do this, we will be connecting 9V batteries to our breadboards.

To get power from our 9V batteries, connect the battery connector. The red wire is +9v power, and the black wire is ground.

Looking at the Arduino User Manual, we can see that pin 30 (in the upper right hand corner of the Arduino board) is the Voltage input Vin pin. Connect the red wire to pin 30 and the black wire to the ground bus on your breadboard. Be careful that you plug the battery in in exactly this way. Otherwise you risk damaging something, including possibly even the USB port on your laptop! Use your multimeter to check the voltage coming out on your 5V and 3V3 outputs.

Secure your battery using electrical tape!

Compiling and uploading firmware code in the Arduino Program

For this class, we assume that you will be adapting existing working code for your own applications. While this is certainly easier than writing firmware from scratch, it does require understanding how existing code functions.

Please upload the following firmware programs from your Arduino program's Sketchbook->Examples folders to your Arduino controller and see how they function. Do attach LEDs, buttons, as is appropriate:

* Digital->Blink
* Digital->BlinkWithoutDelay
* Analog->Fading
* Digital->Button (Even though it works, there is a bug in this file. Can you find it?)

Modify the following programs:

* Digital->Melody: Use the piezo speakers supplied with the lab for this one. Change the song. Incorporate some user interaction so that the song does not loop endlessly. 
  • Analog->AnalogInput: Change the code so that you create a digital metronome whose speed can be controlled by a continuous sensor.

Custom Communication

Another reason that you might want to program the Arduino microcontroller even if you are connected to a laptop or desktop computer is to enable greater control over the communications from the Arduino. In this segment of the laboratory, we learn a wider variety of ways to send data from the Arduino hardware to the computer than we have previously used.

Serial communication with the Arduino software

You might have noticed in the previous lab segment that it can be very hard to know what is going wrong when the Arduino hardware is in autonomous mode. Here, we send serial communications between the Arduino hardware and the Arduino software.

  • Sketchbook->Examples->Analog->Smoothing: Use the Serial Monitor (icon on the far left on the Arduino software toolbar) to get data back from the Arduino.
  • Sketchbook->Examples->Communication->SerialCallandResponse.

Serial communication with Max or PD

Now let's try using serial to communicate with Max or PD.

First let's try outputting information to the Arduino hardware:

  • Load Examples->Communication->PhysicalPixel
    • The maxpatch for communicating with this firmware is embedded in the firmware program. Just copy the text (minus the "/*" and "*/"comment markers), open a new Max patch window, and paste.
    • Here is the equivalent pd patch; use "Save Link As" to save it to your lab3 directory. PhysicalPixel.pd

Next let's try getting information from the Arduino hardware:

  • Load Examples->Communication->Graph
    • The maxpatch for communicating with this firmware is embedded in the firmware program. Just copy the text (minus the "/*" and "*/"comment markers), open a new Max patch window, and paste.
    • Here is the equivalent pd patch; use "Save Link As" to save it to your lab3 directory. graph.pd

Low-latency sensing (optional)

Now, try reimplementing your instrument from the end of Lab 2 in firmware! Program the Arduino firmware so that it only detects inputs and performs outputs to the pins you use, and modify your Lab 2 patch to work with it.

Your code on the Arduino should be running your program a lot faster than Firmata. Does your instrument work any better?

Encoder input

One final reason that you would want direct control over the Arduino firmware is that you might have very low-latency sensing needs. As an example of this, we show you here how to use encoder input with the Arduino

Now you will learn how to use a rotary encoder. Quit Pd or Max/MSP. Then download encoder.zip and flash it to your Arduino board.

Build this schematic: http://cm-wiki.stanford.edu/mediawiki/images/c/c1/Encoder_filter.png.

Connect pin 2 of the Arduino to Phase A. Connect pin 3 of the Arduino to Phase B, and you're ready to send out the position of the encoder over the serial bus.

You can read the values coming in from the encoder if you use the Graph patch for Pd or Max/MSP from the previous exercise.

The values should start near zero and increase or decrease, depending on which direction you turn the encoder. How are negative numbers represented? Why does the encoder always start near 0 when the program on the Arduino is restarted?

Restore Firmata?

If, after all of this, you just long for the halcyon days where you just had to program in Max or PD, you are welcome to reflash your Arduino hardware with the Firmata software.


250a 2009