Wednesday, June 06, 2018

Meet Gesty, The Musical Micro:Bit

One of the quintessential projects for the micro:bit is to create a musical instrument of some sort. This usually involves implementing a crude version of a theremin, often by using a light sensor or potentiometer to control the sound. I've implemented my own take on this, using the Tinkertools Electro-Theremin tutorial as helpful inspiration.

Here's my code:

let debug     = true;
let nextNote  = input.runningTime();

if(debug) {
  OLED.init(64, 128);
}

basic.forever(() => {
  let now = input.runningTime();
  let roll = 90 + input.rotation(Rotation.Roll);
  let pitch = 90 + input.rotation(Rotation.Pitch)
  let right = roll / 36;
  let down = pitch / 36;
  let freq = 131 + (roll * 5);
  let rest = 20;
  let duration = pitch * 3;
  let fudge    = pins.analogReadPin(AnalogPin.P1);

  basic.clearScreen();
  led.plot(right, down);

  if(now > nextNote) {
    nextNote = input.runningTime() + duration + rest;
    
    music.playTone(freq + (input.buttonIsPressed(Button.A) ? fudge  : 0) , 
                   duration + (input.buttonIsPressed(Button.B) ? fudge  : 0));
  }
  
  if(debug) {
    OLED.showString(roll + "/" + freq + "::" + pitch + "/" + duration + "::" + fudge);
  }
});

(You can find the full source code here.)

I'm using the left-right roll of the micro:bit to determine the note frequency, and the forward-back pitch of the micro:bit to determine note duration. By tilting the micro:bit in various directions, you can generate different musical effects.

But wait, there's more!

I've implemented a simple UI using the 5x5 LED grid on the micro:bit. As you tilt micro:bit in the various directions, the relevant LED lights up showing how far to the left/right or forward/back you are. I also added the potentiometer that came with the Tinkercademy kit to turn the A and B buttons on the micro:bit into overrides. You can dial in a value on the potentiometer then press A to modify the current frequency or B to modify the current direction. Finally, I'm using the tiny screen that comes with the Tinkercademy kit for debugging output.

I was pleased at how accurate the data from input.rotation(...) is. And it's downright amazing how simple the Tinkercademy kit is to operate. I plugged in a few piece of hardware and was making noise in no time.

To prove this all works, check out the video below. Warning: I've got no idea how to get Gesty, the Musical Micro:Bit to, actually make pleasing music. So for now, it just makes mind numbing screeches. Enjoy!

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails