Skip to content

Commit

Permalink
Add in Microbit code, tidy files, add README
Browse files Browse the repository at this point in the history
  • Loading branch information
glenpike committed Mar 18, 2020
1 parent 956fc16 commit 4c93d8b
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Intro

An experiment to use Microbits to control MIDI devices.

One Microbit acts as a [receiver](microbit/js/receiver.js) for others that [transmit](microbit/js/transmitter.js) values over radio. It relays these to the serial bus (USB)

These are picked up by a Ruby [programme](ruby/src/microbit-reader.rb) and converted to MIDI controller values hardcoded for a Novation Bass Station synth and sent

# Setup

## Microbit

You need 2 of these.

Paste the code for the Transmitter into the [editor](https://makecode.microbit.org/#editor)
and download this to your Microbit.

Repeat for the Receiver and leave this one connected. Connect the Transmitter to a set of batteries.

## Ruby

Run `cd ruby && bundle install` to add the required Gems.

In the ruby directory, run `ruby src/microbit-reader.rb` and chose a MIDI output (you need one of these). If you move your Transmitter Microbit around, it will output the values it maps to the MIDI controllers and send to your synth.

# TODO

- Make the buttons and gestures do something.
- Make synth agnostic (configurable mapping of controls, e.g. 'learn')
- Allow ranges to be set for controllers, e.g. so we can restrict resonance, etc.
- Make Microbit transmit without 'throttling' - do this in the ruby code (was done to reduce sensitivity)
12 changes: 12 additions & 0 deletions microbit/js/receiver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Simple Microbit programme to act as a reciever
// - streams radio packets from other Microbits to
// the serial output (which is USB)

radio.onReceivedValue(function (name, value) {
radio.writeReceivedPacketToSerial()
})
radio.onReceivedString(function (receivedString) {
radio.writeReceivedPacketToSerial()
})
basic.showIcon(IconNames.Pitchfork)
radio.setGroup(1)
51 changes: 51 additions & 0 deletions microbit/js/transmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// JavaScript for a Microbit to transmit it's orientation
// and other input values via Radio to another Microbit

function compare(current: number, previous: number) {
return Math.floor(current / 2.0) != Math.floor(previous / 2.0)
}
input.onButtonPressed(Button.A, function () {
radio.sendString("input: A")
})
input.onButtonPressed(Button.B, function () {
radio.sendString("input: A")
})
input.onButtonPressed(Button.AB, function () {
radio.sendString("input: AB")
})
input.onGesture(Gesture.Shake, function () {
radio.sendString("input: shake")
})
let lastCompass = 0
let compass = 0
let lastRoll = 0
let roll = 0
let lastPitch = 0
let pitch = 0
basic.showLeds(`
. . # . .
. # # # .
# . # . #
. . # . .
. . # . .
`)
radio.setGroup(1)
radio.setTransmitSerialNumber(true)
basic.forever(function () {
pitch = input.rotation(Rotation.Pitch)
if (compare(pitch, lastPitch)) {
radio.sendValue("pitch", pitch)
lastPitch = pitch
}
roll = input.rotation(Rotation.Roll)
if (compare(roll, lastRoll)) {
radio.sendValue("roll", roll)
lastRoll = roll
}
compass = input.compassHeading()
if (compare(compass, lastCompass)) {
radio.sendValue("compass", compass)
lastCompass = compass
}
basic.pause(100)
})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4c93d8b

Please # to comment.