Last active
July 12, 2021 03:41
-
-
Save interstar/271669461e6b6471c1d97f569243da8f to your computer and use it in GitHub Desktop.
Processing sketch to turn Microbit Accelerometer data (received from the serial cable) into MIDI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://www.youtube.com/watch?v=DcgRHOqSFm8 | |
import processing.serial.*; | |
import themidibus.*; | |
Serial serialPort; | |
MidiBus midiBus; | |
void setup() { | |
size(700,500); | |
// Setting up the serial connection | |
printArray(Serial.list()); | |
if (Serial.list().length > 0) { | |
serialPort = new Serial(this, Serial.list()[0], 115200); | |
} else { | |
println("No serial found"); | |
exit(); | |
} | |
MidiBus.list(); | |
// Windows | |
midiBus = new MidiBus(this, -1, "LoopBe Internal MIDI"); | |
// Linux | |
//myBus = new MidiBus(this, -1, "Gervill"); | |
} | |
void sendCtrl(int channel,int param,int value) { | |
midiBus.sendControllerChange(channel, param, value); | |
} | |
int x; | |
int y; | |
void draw() { | |
background(0); | |
fill(200,255,240); | |
int ix=0,iy=0,iz=0; | |
int dx=0,dy=0; | |
boolean a=false,b=false; | |
if (serialPort!=null) { | |
while (serialPort.available() > 0) { | |
String serialBuffer = serialPort.readString(); | |
if (serialBuffer != null) { | |
try { | |
serialBuffer = serialBuffer.trim(); | |
String[] parts = serialBuffer.split(","); | |
ix = Integer.parseInt(parts[0]); | |
if (ix < -300) { dx = (int) map(ix,-1024,0,-20,-1); } | |
if (ix > 300) { dx = (int) map(ix,0,1024, 1, 20); } | |
iy = Integer.parseInt(parts[1]); | |
if (iy < -300) { dy = (int) map(iy,-1024,0,-20,-1); } | |
if (iy > 300) { dy = (int) map(iy,0,1024, 1, 20); } | |
iz = Integer.parseInt(parts[2]); | |
a = (parts[3].equals("True")) ? true : false; | |
b = (parts[4].equals("True")) ? true : false; | |
} | |
catch (Exception e) { | |
println("Exception "); | |
println(e); | |
break; | |
} | |
} | |
} | |
} else { | |
fill(255, 0, 0); | |
} | |
x = x + dx; | |
if (x < 0) { x = 0;} | |
if (x > width) { x = width;} | |
y = y + dy; | |
if (y < 0) { y = 0; } | |
if (y > height) { y = height; } | |
ellipse(x,y, 20, 20); | |
sendCtrl(1,1,(int)map(x,0,width, 0,127)); | |
sendCtrl(1,2,(int)map(y,height,0,0,127)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment