Difference between revisions of "250a Firmware Lab"

From CCRMA Wiki
Jump to: navigation, search
m (The Autonomous Arduino)
 
(186 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Soon there will be a lab for 250a Firmware programming here.
+
<font size=5>Lab 3: Firmware Programming</font><br>
<font size=5>Lab 3: Firmware Programming/font><br>
+
See [https://ccrma.stanford.edu/courses/250a/schedule.html this quarter's schedule] for due dates.
Due on Wednesday, October 14th at 5PM
+
  
For this lab you need your [[MaxKit]], and Max/MSP or PD on a computer.
+
For this lab you need your [[MaxKit]], with your Satellite CCRMA .  
  
== 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 ===
 
  
 +
== Firmware Programming On Arduino ==
 
=== Compiling and uploading firmware code in the Arduino Program ===
 
=== 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 to your Arduino controller and see how they function:
+
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.  
* Flash
+
* Button
+
* Dimmer
+
 
+
=== Adapting firmware code ===
+
 
+
Use bits from the sample Arduino programs to create your own firmware code to create a flashing Metronome that uses a continuous sensor to control the speed of a flashing led.
+
 
+
 
+
 
+
 
+
 
+
== Mount your Arduino Nano on your breadboard==
+
 
+
We will be powering the Nano and the breadboard with current from the USB port, which is good for up to 500mA of 5 V±5%-- probably enough for most input circuits, although not enough if you plan to run a lot of LEDs or motors.
+
* The Nano should sit at the bottom of the breadboard, so that the pins lie in rows 49-64 on either side.
+
* Using jumper wires, connect the row 52 pin (GND) on the left side of the Nano to the blue ground (GND) rail.
+
* Connect the row 52 pin (+5V) on the right side of the Nano to the red Power rail.
+
* Use jumper wires to connect the power and GND rails on the left side of the breadboard to the right.
+
 
+
[[Image:Lab2-2.jpg]]
+
 
+
== Install Firmata onto your Arduino Nano ==
+
* Install the Arduino software
+
** Unarchive the file, and move it to your applications folder.
+
** Install the FTDI driver that comes with the Arduino software so that your computer will recognize the Arduino when it is plugged into the USB port.
+
* Unarchive the Firmata firmware that you downloaded previously.
+
* Connect your Arduino Nano to your computer using a USB cable.
+
* Use the Arduino software program to open ''StandardFirmata.pde'', which lies inside the folder ''Firmata-2.0beta3:Firmata:examples:StandardFirmata''.  ('''Do NOT open the version of StandardFirmata that lies directly within the pull-down menus of the Arduino software.''')
+
* Use Tools->Board and Tools->Serial Port to select the Arduino Nano (Atmega 328) and USBserial tty port, then hit the Play button to verify and compile the program.
+
* Upload the Firmata firmware to your Arduino Nano using upload button, the fourth square button from the left (the one with the sideways arrow).
+
* Close the Arduino program. (This is important because it frees up the USB serial port so that Max or PD can talk to the Arduino board next.)
+
  
== Buttons, Switches and LEDs ==
+
* Use the usual procedure (see [https://ccrma.stanford.edu/wiki/Software_Lab_250 described before]) to power up Satellite CCRMA and login as the user ''ccrma'' with the password ''temppwd''.
  
* Build the circuit that is detailed in the following figures. Use components and jumpers to construct your circuits on the solderless bread-board.
+
* Make an empty directory for lab3 using
  
[[Image:Breadboard.png]]
+
''mkdir ~/lab3''
  
* Here's how to wire a simple 2-resistor circuit on the solderless bread-board (for example R1 = 10K, R2 = 10K):
+
* Enter the lab3 directory by typing
  
[[Image:Jumper.png]]
+
''cd ~/lab3''
  
What will A0 read?
+
* Download the first files for the lab using
  
(How do you know what resistor you have? Use the [http://www.dannyg.com/examples/res2/resistor.htm resistor calculator]!)
+
''wget https://ccrma.stanford.edu/courses/250a/labs/lab3.zip
  
 +
* Unzip lab3 three with
  
=== Build the Button and LED Circuit ===
+
''unzip lab3.zip''
We'll start our tutorial with three simple light circuits.  
+
  
* In the first one, the LED is permanently on.
 
* In the second, the LED only lights up when a button is pressed and a circuit is completed.
 
* In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program.
 
  
[[Image:3Buttons.gif]]
 
  
==== Power a LED (always on) ====
 
  
[[Image:Lab2-3.jpg]]
 
  
Build the following circuit on your breadboard. Use a 220Ohm resistor (red red brown gold).  
+
* Start the Arduino software by typing the command ''arduino &'' in the XTerminal to your Satellite. Please upload the following firmware programs from your Arduino program's Examples folders to your Arduino controller and see how they function. Do attach LEDs, buttons, as is appropriate:
  
Because the LED is a diode, it has a set voltage drop across the leads; exceeding this causes heat to build up and the LED to fail prematurely. So! It is always important to have a resistor in series with the LED.
+
* Examples->Digital->Blink
 +
* Examples->Digital->BlinkWithoutDelay
 +
* Examples->Digital->Button
  
Also, another consequence of the LED being a diode is that it has directionality. The longer lead, the anode, should be connected towards power; the shorter, cathode, should be connected towards ground. (In the photo, the longer lead has a bent "knee.")
+
== Custom Communication ==
 +
One reason that you might want to program the Arduino microcontroller 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.
  
==== Make a light switch ====
+
=== Serial communication with the Arduino Serial Monitor ===
 +
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 IDE.
  
Next, we'll insert a switch into the circuit. The momentary switches in your kit are "normal open", meaning that the circuit is interrupted in the idle state, when the switch is not pressed. Pressing the switch closes the circuit until you let go again.
+
* Examples->Analog->Smoothing: Use the Serial Monitor (icon on the far left on the Arduino IDE toolbar) to get data back from the Arduino. When you do this, you'll notice the TX light on the Arduino lights up and stays lit.  
  
[[Image:Lab2-4.jpg]]
+
The high datarate on the Serial monitor can make the Xwindow for the program freeze. If the Arduino IDE crashes, go to the main terminal and type 'fg' to bring the arduino to the foreground, and then type 'cntl-x, cntl-c' to kill the program. Now add a delay to the loop function to prevent the freeze. <span style="color:green;">What size delay makes for speedy updates but doesn't cause freezing?</span>
  
Use a multimeter to see what happens to the voltage on either side of the LED when you press the switch.
+
* Examples->Communication->SerialCallandResponseASCII. This will show you how to do two-way communication with the Serial Monitor.
  
==== Toggling LED with PD or Max ====
+
=== Serial communication with PD ===
 +
Now let's try using serial to communicate with PD.
  
[[Image:Lab2-5.jpg]]
+
First let's try outputting information to the Arduino hardware:
 +
* In Arduino, load Examples->Communication->PhysicalPixel
 +
** The PD patch for communicating with this firmware is lab3/physicalpixel.pd.
 +
** You may need to hunt for the right serial port to make this work. Serial port 4 worked for us...
  
In the third example, we'll replace the manual switch with an Arduino pin (set to output mode), so we can control the LED from our program. The safe way to do this is to let the Arduino pin sink current - if we toggle the pin low, it acts as ground and current flows through the resistor and the LED as it did in the previous examples. When we take the pin high, to 5V, there is no potential difference and no current flows - the LED stays off.
 
  
* In the ArduinoLab patch, press the "toggling LED with software" button in the upper right to preset the outputs properly. The patch expects you to connect the LED to digital pin 2 (D2).
+
Next let's try getting information from the Arduino hardware:
 +
* In Arduino, load Examples->Communication->Graph
 +
** The PD patch for communicating with this firmware is lab3/Graph.pd
 +
** Again, you need to set the right serial port, ascii/binary, and graph on/off settings.
  
 +
== Low-latency sensing ==
 +
=== Encoder input ===
  
Optional: Try changing your patch so the light stays on when you press the mouse button, and stays off when you press it again. After that, change your patch so the light blinks on/off. Then, have your patch button switch the light between on and blinking.
+
* Download example firmware for interfacing with a rotary encoder using the command
  
=== Sensing buttons in software  ===
+
''wget ccrma.stanford.edu/courses/250a/labs/files/encoder2011.zip''
  
We've used code to trigger output - what about the other direction, sensing physical input in code? Just as easy. Here is a simple switch circuit:
+
(Or use scp, Fetch, or your favorite secure FTP program to get encoder2011.zip onto your kit.)
  
[[Image: switch.png]] [[Image: Lab2-6.jpg]]
+
* Extract the ZIP file by typing
+
When the switch is open, the Arduino pin (set to input mode) is pulled to 5V - in software, we'll read Arduino.HIGH. When the switch is closed, the voltage at the Arduino pin falls to 0V - in software, we'll read Arduino.LOW. The pull-up resistor is used to limit the current going through the circuit. In software, we can check the value of the pin and switch between graphics accordingly.
+
  
* In the ArduinoLab patch, press the "sensing buttons in software" button to preset the outputs properly. The patch expects you to connect the switch to digital pin 4 (D4).
+
''unzip encoder2011.zip''
  
=== Fading LEDs (optional) ===
+
* Enter the encoder2011 subdirectory by executing
  
What about those "breathing" LEDs on Mac Powerbooks? The fading from bright to dim and back is done using pulse-width modulation (PWM). In essence, the LED is toggled on and off rapidly, say 1000 times a second, faster than your eye can follow. The percentage of time the LED is on (the duty) controls the perceived brightness. To control an LED using PWM, you'll have to connect it to one of the pins that support PWM output - 9, 10 or 11 on the Arduino. Then write a patch that cycles the PWM values.
+
''cd encoder2011''
  
* In the ArduinoLab patch, press the "Fading LEDs" button to preset the outputs properly. The patch expects you to connect the LED to digital pins 9-11 (D9-11).
 
* In your Arduino Kit, you have a RGB LED which has four leads (it's white when not lit); it's basically like 3 LEDs sharing the same ground. Use PWM and this [[http://blog.ncode.ca/?p=38 pin out information]] to make the LED cycle through a rainbow of colors.
 
  
[[Image: Lab2-7.jpg]]
+
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.
  
== Using Analog Sensors ==
+
* Quit Pd to ensure that Pd isn't using the USB/serial connection to the Arduino.
  
Now we will work with the continuous input values provided by analog sensors - potentiometers, accelerometers, distance rangers, etc.
+
* Start up the Arduino software using the command ''arduino &'' and upload the sketch ''~/lab3/encoder2011/encoder/encoder.pde'' to the Arduino. Then quit the Arduino IDE and start up Pd by typing ''pd encoder.pd''
  
=== Make a Light Dimmer ===
+
* Now you need to build this schematic to interface the encoder: http://cm-wiki.stanford.edu/mediawiki/images/c/c1/Encoder_filter.png. 
 +
Pin two is the middle pin on the rotary encoder, and pins 1 and 3 are the other directly adjacent pins. It can be a bit difficult to plug the encoder into the solderless protoboard, but you can do it if you straighten out the pins first.
  
In this example we'll build a light dimmer: a knob connected to a light so that when you turn the knob, the light increases or decreases in brightness. We'll use a potentiometer. The potentiometer has three terminals - the resistance between the first and the third terminal is constant (10k Ohms in our case). The resistance between terminals 1 and 2 (and between 2 and 3) varies as you turn the shaft of the potentiometer. If you apply 5V to terminal 1, connect terminal 3 to Ground, you will get a continuously varying voltage at terminal 2 as you turn the shaft (Why that is the case will become clearer once you've learned about voltage dividers further below).
+
Note: In the schematic, ''AVR Phase A'' is digital pin 2 or the Arduino, and ''AVR Phase B'' is digital pin 3.
  
* Connect the middle pin of the potentiometer to analog input 0, the other two to +5V and ground.
+
Now you're ready to send out the position of the encoder over the serial bus. The values should start near zero and increase or decrease, depending on which direction you turn the encoder. <span style="color:green;">How are negative numbers represented?  Why does the encoder always start near 0 when the program on the Arduino is restarted?</span>
* Through a 220Ohm resistor, connect an LED to pin 9(anode or long side to resistor, cathode to pin 9)  
+
*  In the ArduinoLab patch, press the "Light Dimmer" button to preset the outputs properly. The patch expects you to connect the LED to digital pin 9 (D9) and the potentiometer to analog pin 0 (A0). Use the PWM controls under output controls in the ArduinoLab patch to control the lightness and dimness of the LED.
+
  
 +
== Make A Musical Interaction ==
 +
As the final main deliverable of the lab, you should cause your Satellite CCRMA kit make some sound that depends on how you manipulate the sensors. For example, you could make a musical instrument, a music controller, a mock-up sound art installation, or some other sonic interaction. Now that you have had some experience synthesizing sound, your sound synthesis patch should involve more than just a few ''osc~''s. Think about what you want to design before you get started.
  
 +
For your musical interaction, you can write your own firmware, or you can use Firmata. If you choose to use Firmata, then you need to re-flash Firmata to the Arduino. (See [https://ccrma.stanford.edu/wiki/Talk:250a_Microcontroller_%26_Sensors_Lab_Pd#Prepare_Arduino instructions from Lab 2].)
  
 +
Start up the Jack audio server by executing the command ''qjackctl &'' at the terminal and clicking on the ''Start'' button. In order to be able to hear audio, you will need to plug a pair of ear buds, headphones, or loudspeakers into the 1/8" (2.54mm) jack labeled AUDIO OUT on the beagleboard.
  
 +
It is probably easiest to make your patch by modifying ''~/on-startup/windy-day.pd'', which Edgar demonstrated in class. To start it up, first change to the directory ''~/on-startup'' by typing ''cd ~/on-startup'' in the terminal. Then type ''pd windy-day.pd &'' to startup ''pd''.
  
[[Image: Lab2-8.jpg]]
+
<span style="color:green;">Don't forget to make a video of your musical interaction!</span>
  
* Optional: hook up two LEDs, one red one green. as you turn in one direction, red gets brighter; in the other, green gets brighter. In the middle, both are off.
+
<!-- == Optional: Autonomous Satellite CCRMA ==
 +
By this point you should be done with your musical interaction. If you would like to go the extra mile, we will now show you how to make your musical interaction autonomous in the sense that it can operate all by itself, free from any external laptop computer. We will show you how to do this using the example ''windy-day.pd''.
  
=== Drawing a graph of analog input ===
+
=== To Enable A Default Patch On Boot ===
Let's understand better what the values are that we are reading from the analog input. To do so, we will use a slider graph to show how the analog values are changing in time.
+
* First put StandardFirmata back on the Arduino as described in lab 2.
  
Leave the potentiometer part of your circuit, you may take off the LED part if you want to. Use A0 or A1 to graph the analog input in the ArduinoLab patch.
+
* In the terminal, change to the ''on-startup'' directory by typing
  
 +
''cd ~/on-startup''
  
=== Thresholding with a Range Sensor (optional) ===
+
* The files need to be updated. You can do this by running the command
  
Thresholding is the process of turning continuous data into a discrete yes/no decision.
+
''wget ccrma.stanford.edu/courses/250a/labs/files/newonstartupfiles.zip''
  
To learn about thresholding, we'll connect the IR range sensor. The circuit is trivial: just connect red to 5V, black to ground, and yellow to analog input A0.
+
and then
  
Take a look at the data the sensor returns with sensor_graph_02 - when the field of view of the sensor is clear (no obstacle - point it at the ceiling), it returns a low voltage. Move your hand high over the sensor, then start lowering it - you should see the output voltage rise, until you are about 4" away. The sensor has a range of operation of 4"-30".
+
''unzip newonstartupfiles.zip''
+
Let's do something useful with that data. Imagine a smart cookie jar that reminds you not to snack in between meals. We could put an IR ranger into the lid. Whenever a hand comes too close, our program could play a warning sound or flash a warning light. Write a patch to manage this!
+
  
=== Tilt control with an Accelerometer ===
+
and agree to replace the files in question.
In this example, we'll simulate the motion of a ball on a tilting plane in software and control the tilt through a sensor. Think of it as a first step to build your own electronic game of Labyrinth. The right sensor to use is an accelerometer. Accelerometers can report on both static and dynamic acceleration -- think of static acceleration as the angle the accelerometer is held with respect to the ground (the acceleration measured here is due to gravity). Dynamic acceleration occurs when you shake the sensor.
+
  
[[Image:Lab2-9.jpg]]
 
  
The accelerometer in your kit is a 3-axis, +-2g sensor (1g is the acceleration due to gravity). It comes with 0.1" header pins that fit into the breadboard. The connections you need to make are VCC to the 3V3 pin on the Arduino (it will be in row 62), GND to ground, and X, Y, and Z to the first three analog input pins on the Arduino board. You can ignore the ST (self test) pin.
+
* Now, type ''ls -la'' to see what files are in there. You can see that the link (aka alias) ''default_patch.pd'' points to ''windy-day.pd''. In order to enable the default patch to load on boot, rename the file ''load_default_patch_disabled'' to ''load_default_patch'' using the command
  
[[Image:accel_on_board.jpg]]
+
''mv load_default_patch_disabled load_default patch''
  
Push the accelerometer into the breadboard and make the connections as shown:
+
* The ''cat'' command can be used to display the contents of any ASCII file. To look at the contents of ''load_default_patch'', type ''cat load_default_patch''. As you can see, it starts up Jack using the text-based ''jackd'' instead of ''qjackctl'', and then it loads ''default_patch.pd'' in pd with the graphical user interface (GUI) disabled.
  
Now, get a feel for the data the accelerometer provides. Use the Light Dimmer presets, which will track analog values on A0 and A1. Then pick up the Arduino+accelerometer board and tilt it in various directions. Start by holding it so that the accelerometer board is parallel to the ground. Find in which direction the X reading increases and decreases; do the same for the Y reading. Are the labels on the accelerometer board correct?
+
* Now your kit should be ready for autonomous Satellite CCRMA. To test it, just reboot the kit by typing ''sudo reboot'' and entering the password ''temppwd''. As it is rebooting, plug a pair of ear buds, headphones, or loudspeakers into the 1/8" (2.54mm) jack labeled AUDIO OUT on the beagleboard. Within about a minute, your kit should be fully rebooted. To check this, you can try logging in with SSH. Either way, it should start running ''windy_day.pd'' upon startup.  
  
=== Voltage Dividers ===
+
* Once your kit is fully rebooted, if you touch the anaog input pins on the Arduino A0-A7, it should start making some sound. However, unless you connect up a sensor circuit to some of the analog inputs, the exact behavior may be complicated. In particular, it will depend on the electrical properties of your body (how much sweat is on your fingers, whether you are touching a grounded device such as a laptop with any part of your body. Ask for help if you cannot get any sound.
  
In your kit, the potentiometer, IR distance ranger, and accelerometer are especially easy to work with since they directly output a changing voltage that can be read by one of Arduino's analog input pins.
+
* Finally, log back into your Satellite. Type ''ps -A'' at the prompt to get a list of the currently running processes. ''jackd'' is the Jack audio sound server, and ''pdextended'' is the pd process. To stop the default patch from running, type ''stop_default''. To see how ''stop_default'' works, type ''cat ~/on-startup/stop_default''.
 +
-->
  
 +
== Optional: Programming Linux ==
 +
'''We don't actually expect you to do anything here, we are just providing some more information that is maybe helpful for you linux gurus.''' Since Satellite CCRMA runs Ubuntu linux on an OMAP chip, many standard software packages have been compiled for it. This is why we were easily able to install software such as Jack, [http://audacity.sourceforge.net/ Audacity], [http://chuck.cs.princeton.edu/ ChucK], [http://faust.grame.fr/ Faust], [https://ccrma.stanford.edu/groups/soundwire/software/jacktrip/ Jacktrip], and the Arduino software.
  
Other sensors don't give you a varying output voltage per se, but instead change their resistance. Examples in your kit are the force sensitive resistor (FSR) and the bend or flex sensor. It is easy to get a changing voltage based on a changing resistance through a voltage divider circuit(Wikipedia page). The idea is that you put two resistors in series between power and ground: one that changes resistance (your sensor), and one of a known, fixed resistance. At the point in between the two resistors, you can measure how much the voltage has dropped through the first resistor. This value changes as the ratio of resistances between variable and fixed resistors change. More formally:
+
If you are lucky, you can install your favorite software using the ''apt-get'' utility. To get a list of packages available on the OMAP's ARM architecture, type
  
[[Image:res_divider.png]]
+
''sudo apt-cache pkgnames''
  
* potentiometer:
+
You will notice that this list is way too long to look at. You can pipe it to the text file using the command
  
[[Image:Pot.png]]
+
''sudo apt-cache pkgnames > packages.txt''
  
* force-sensitive resistor (FSR):
+
and then look at it using ''emacs packages.txt'', or you search for a particular package, such as
  
[[Image:FSR.png]]
+
''sudo apt-cache pkgnames | grep emacs''.
  
Try both circuits.  Test the resistance range of your sensor. If you want 2.5 volts to be the middle, make the comparison resistor (33k in the diagram) the "average" value of the FSR's resistance. Test this with a multimeter.
+
Or, you can compile linux software yourself on the Satellite. The gcc, g++ tools etc. are already installed.
  
* Bend Sensor
+
Type the ''df'' command. You can see that there is not a whole lot more than 1GB available on the SD card, so you should only install software if you decide that you need it.
[[Image:Bend_sensor.png]]
+
  
  
  
== Putting it all Together ==
 
  
* Create a patch to make sounds based on button and sensor values from the Arduino. You can try to adapt your patches from Lab 1, or come up with a new patch.
 
  
* Try to make a simple musical interaction. Think about music -
 
** does it have dynamics?
 
** can you turn the sound off?
 
** can it be expressive?
 
  
<center>[[250a 2009]]</center>
 
 
[[Category:250a]][[Category:PID]]
 
[[Category:250a]][[Category:PID]]

Latest revision as of 10:35, 26 September 2012

Lab 3: Firmware Programming
See this quarter's schedule for due dates.

For this lab you need your MaxKit, with your Satellite CCRMA .


Firmware Programming On Arduino

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.

  • Use the usual procedure (see described before) to power up Satellite CCRMA and login as the user ccrma with the password temppwd.
  • Make an empty directory for lab3 using

mkdir ~/lab3

  • Enter the lab3 directory by typing

cd ~/lab3

  • Download the first files for the lab using

wget https://ccrma.stanford.edu/courses/250a/labs/lab3.zip

  • Unzip lab3 three with

unzip lab3.zip



  • Start the Arduino software by typing the command arduino & in the XTerminal to your Satellite. Please upload the following firmware programs from your Arduino program's Examples folders to your Arduino controller and see how they function. Do attach LEDs, buttons, as is appropriate:
* Examples->Digital->Blink
* Examples->Digital->BlinkWithoutDelay
* Examples->Digital->Button

Custom Communication

One reason that you might want to program the Arduino microcontroller 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 Serial Monitor

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 IDE.

  • Examples->Analog->Smoothing: Use the Serial Monitor (icon on the far left on the Arduino IDE toolbar) to get data back from the Arduino. When you do this, you'll notice the TX light on the Arduino lights up and stays lit.

The high datarate on the Serial monitor can make the Xwindow for the program freeze. If the Arduino IDE crashes, go to the main terminal and type 'fg' to bring the arduino to the foreground, and then type 'cntl-x, cntl-c' to kill the program. Now add a delay to the loop function to prevent the freeze. What size delay makes for speedy updates but doesn't cause freezing?

  • Examples->Communication->SerialCallandResponseASCII. This will show you how to do two-way communication with the Serial Monitor.

Serial communication with PD

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

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

  • In Arduino, load Examples->Communication->PhysicalPixel
    • The PD patch for communicating with this firmware is lab3/physicalpixel.pd.
    • You may need to hunt for the right serial port to make this work. Serial port 4 worked for us...


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

  • In Arduino, load Examples->Communication->Graph
    • The PD patch for communicating with this firmware is lab3/Graph.pd
    • Again, you need to set the right serial port, ascii/binary, and graph on/off settings.

Low-latency sensing

Encoder input

  • Download example firmware for interfacing with a rotary encoder using the command

wget ccrma.stanford.edu/courses/250a/labs/files/encoder2011.zip

(Or use scp, Fetch, or your favorite secure FTP program to get encoder2011.zip onto your kit.)

  • Extract the ZIP file by typing

unzip encoder2011.zip

  • Enter the encoder2011 subdirectory by executing

cd encoder2011


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.

  • Quit Pd to ensure that Pd isn't using the USB/serial connection to the Arduino.
  • Start up the Arduino software using the command arduino & and upload the sketch ~/lab3/encoder2011/encoder/encoder.pde to the Arduino. Then quit the Arduino IDE and start up Pd by typing pd encoder.pd

Pin two is the middle pin on the rotary encoder, and pins 1 and 3 are the other directly adjacent pins. It can be a bit difficult to plug the encoder into the solderless protoboard, but you can do it if you straighten out the pins first.

Note: In the schematic, AVR Phase A is digital pin 2 or the Arduino, and AVR Phase B is digital pin 3.

Now you're ready to send out the position of the encoder over the serial bus. 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?

Make A Musical Interaction

As the final main deliverable of the lab, you should cause your Satellite CCRMA kit make some sound that depends on how you manipulate the sensors. For example, you could make a musical instrument, a music controller, a mock-up sound art installation, or some other sonic interaction. Now that you have had some experience synthesizing sound, your sound synthesis patch should involve more than just a few osc~s. Think about what you want to design before you get started.

For your musical interaction, you can write your own firmware, or you can use Firmata. If you choose to use Firmata, then you need to re-flash Firmata to the Arduino. (See instructions from Lab 2.)

Start up the Jack audio server by executing the command qjackctl & at the terminal and clicking on the Start button. In order to be able to hear audio, you will need to plug a pair of ear buds, headphones, or loudspeakers into the 1/8" (2.54mm) jack labeled AUDIO OUT on the beagleboard.

It is probably easiest to make your patch by modifying ~/on-startup/windy-day.pd, which Edgar demonstrated in class. To start it up, first change to the directory ~/on-startup by typing cd ~/on-startup in the terminal. Then type pd windy-day.pd & to startup pd.

Don't forget to make a video of your musical interaction!


Optional: Programming Linux

We don't actually expect you to do anything here, we are just providing some more information that is maybe helpful for you linux gurus. Since Satellite CCRMA runs Ubuntu linux on an OMAP chip, many standard software packages have been compiled for it. This is why we were easily able to install software such as Jack, Audacity, ChucK, Faust, Jacktrip, and the Arduino software.

If you are lucky, you can install your favorite software using the apt-get utility. To get a list of packages available on the OMAP's ARM architecture, type

sudo apt-cache pkgnames

You will notice that this list is way too long to look at. You can pipe it to the text file using the command

sudo apt-cache pkgnames > packages.txt

and then look at it using emacs packages.txt, or you search for a particular package, such as

sudo apt-cache pkgnames | grep emacs.

Or, you can compile linux software yourself on the Satellite. The gcc, g++ tools etc. are already installed.

Type the df command. You can see that there is not a whole lot more than 1GB available on the SD card, so you should only install software if you decide that you need it.