sources and files for the bombatuino project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. #include <Wire.h>
  2. //helper to check if a bit is set
  3. #define BIT_IS_SET(i, bits) (1 << i & bits)
  4. //array for inputs on bank a of MCP23017
  5. boolean button[8];
  6. void setup() {
  7. //set MIDI bautdrate
  8. Serial.begin(31250);
  9. //start I2C
  10. Wire.begin();
  11. }
  12. void loop() {
  13. //0x20 hardware address of MCP23017 (A0,A1,A2 to ground)
  14. Wire.beginTransmission(0x20);
  15. Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address
  16. Wire.endTransmission();
  17. Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
  18. int inputs = Wire.read();
  19. delayMicroseconds(10);
  20. int i;
  21. for(i=0;i<8;i++) {
  22. //check if button is pressed
  23. if (BIT_IS_SET(i,inputs)) {
  24. //only send Note-on, when button was not pressed before
  25. if(!button[i]) {
  26. button[i] = true;
  27. midiSignal(144,60+i,115);
  28. }
  29. } else {
  30. //only send Note-off, when button was pressed before
  31. if(button[i]) {
  32. button[i] = false;
  33. midiSignal(128,60+i,0);
  34. }
  35. }
  36. }
  37. }
  38. //send MIDI signal through softwareserial
  39. void midiSignal(byte b1, byte b2, byte b3) {
  40. Serial.write(b1);
  41. Serial.write(b2);
  42. Serial.write(b2);
  43. }