Module 3: Hardware

In this module we take live coding off the keyboard. A micro:bit is a small programmable board with buttons, an accelerometer, a light sensor and more. Plugged into Rattle, it becomes an instrument: its readings flow into your code the same way a slider does, so tilting, shaking, or covering the board changes the music and the picture as it plays.

During the creation process, some of the key questions we ask are:

Resources:

You will need Chrome or Edge (Safari and Firefox cannot talk to the board) and a USB cable that carries data, not just power.

Project 3: Live coding with a micro:bit

Your goal is to perform a live coded piece in Rattle where a micro:bit is part of the instrument. You will record a video, with audio, of you performing — live coding and playing the board.

During your performance, you must:

Audio and visual features you must use (carried over from Projects 1 and 2):

Hardware features you must use:

The recording should last roughly 10 minutes (8 points). Do not plan out all 10 minutes exactly ahead of time, but instead have “goalposts” as discussed in class (5 points). Design the mapping so that the board feels like an instrument — for example, letting tilt choose where in a scale a figure sits rather than just turning a filter knob (5 points). Make sure gesture, sound, and picture belong together as one performance (5 points).

Key tips

What to hand in?

Submit the link in a .txt file along with your controller.py.

How am I graded?

Rubric


Lecture 3-1: Hardware and sensors

What is on a micro:bit, and how does a reading get from a sensor into a note? We cover the accelerometer, buttons, light sensor, and pins; how the board sends one name,value line at a time over the USB cable; why every channel carries 0..1; and the difference between things that vary (mb()) and things that happen (hit()). We also look at latency: Rattle runs a quarter of a second ahead of what you hear, so a button press lands on the next step of your loop’s grid.

@live_loop("bass")
def bass():
    play(36, cutoff=mb("tilt_y", 45, 105), release=0.4)
    if hit("a"):
        sample("clap")
    sleep(0.25)

Lecture 3-2: Designing an instrument

Mapping is the art of hardware performance. Hanging tilt off a cutoff works, but the music stays the same music, slightly brighter — the board feels like a knob because it is a knob. We look at mappings that make a board feel like an instrument: tilt choosing where in a scale a figure sits, one axis stretching a figure apart, covering the board to flood it with reverb, and the same gesture driving sound and picture at once.

How do audiences read a performer holding an instrument versus a performer typing? What do we lose, and gain, when the code keeps playing even if the performer holds still?

Lab 3-1: Hello, micro:bit

The goal of this lab is to get a micro:bit talking to Rattle, play it, and make a short recording.

Part 1: flash the board.

  1. Plug the micro:bit into your computer with a USB cable. (If nothing works later, try another cable first — some only charge.)
  2. In Chrome or Edge, open python.microbit.org.
  3. Delete everything in its editor and paste in the whole of controller.py.
  4. Click Send to micro:bit and pick the board. The lights flicker for a few seconds while it flashes.
  5. In Rattle, click micro:bit in the header and pick the board. Tilt it and watch tilt_x and tilt_y move in the panel in the bottom-left corner.

Part 2: play it. Run this code, turn on visuals, and play the board.

Use the hotkeys, not the buttons. Rerun your code with Cmd+Enter (Ctrl+Enter on Windows) — the music keeps playing and your new code takes over. Stop everything with Cmd+. (Ctrl+.). Get in the habit now; the projects require it.

use_bpm(112)

notes = scale("e2", "minor_pentatonic", num_octaves=3)

@live_loop("lead")
def lead():
    use_synth("pluck")
    here = int(mb("tilt_x", 0, 7))       # tilt walks up and down the scale
    for step in (0, 2, 4, 2):
        play(notes[here + step], release=0.45, cutoff=mb("tilt_y", 60, 112))
        circle(x=mb("tilt_x", -0.9, 0.9), y=-0.6 + step * 0.2,
               r=0.05, vr=0.3, hue=mb("tilt_x", 0.45, 0.95), life=2)
        sleep(0.25)

@live_loop("hits", sync="lead")
def hits():
    if hit("a"):
        sample("clap")
    sleep(0.125)
  1. Before you tilt it, predict what tilting left/right and away/toward will do to the sound and to the picture. Then try it.
  2. Make button B do something different from button A — a sample, and a shape.
  3. Make tilt_y stretch the arpeggio apart: replace (0, 2, 4, 2) with steps that depend on int(mb("tilt_y", 1, 4)).
  4. Cover the light sensor (the LEDs) with your hand. Use mb("light", 0.55, 0.0) as the room= of the play so covering the board floods it with reverb. Why are the numbers backwards?

To turn in:

Record a short video — less than 30 seconds is fine — showing your screen (code and visuals) and the board in your hand as you play it, with audio recorded directly from your computer. Post your video recording publicly, and save the link. For the lab, simply submit the link in a .txt file!


Lecture 3-3:

Lecture 3-4:

Lab 3-2: