From 52975c9dba2682d4846f9c1ab257eea80960e86e Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 13 Feb 2013 20:13:18 +0100 Subject: [PATCH] MIDI-out_MCP23017 sketch --- source/sketches/testing/MIDI-Out_MCP23017.ino | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 source/sketches/testing/MIDI-Out_MCP23017.ino diff --git a/source/sketches/testing/MIDI-Out_MCP23017.ino b/source/sketches/testing/MIDI-Out_MCP23017.ino new file mode 100644 index 0000000..5226e12 --- /dev/null +++ b/source/sketches/testing/MIDI-Out_MCP23017.ino @@ -0,0 +1,49 @@ +#include + +//helper to check if a bit is set +#define BIT_IS_SET(i, bits) (1 << i & bits) + +//array for inputs on bank a of MCP23017 +boolean button[8]; + +void setup() { + //set MIDI bautdrate + Serial.begin(31250); + //start I2C + Wire.begin(); +} + +void loop() { + //0x20 hardware address of MCP23017 (A0,A1,A2 to ground) + Wire.beginTransmission(0x20); + Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address + Wire.endTransmission(); + Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317 + int inputs = Wire.read(); + delayMicroseconds(10); + + int i; + for(i=0;i<8;i++) { + //check if button is pressed + if (BIT_IS_SET(i,inputs)) { + //only send Note-on, when button was not pressed before + if(!button[i]) { + button[i] = true; + midiSignal(144,60+i,115); + } + } else { + //only send Note-off, when button was pressed before + if(button[i]) { + button[i] = false; + midiSignal(128,60+i,0); + } + } + } +} + +//send MIDI signal through softwareserial +void midiSignal(byte b1, byte b2, byte b3) { + Serial.write(b1); + Serial.write(b2); + Serial.write(b2); +}