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.

92 lines
2.1 KiB

  1. #include <SoftwareSerial.h>
  2. // RX, TX for MIDI out
  3. SoftwareSerial MIDI(10, 11);
  4. //button pin
  5. const int switchPin = 6;
  6. //rotary encoder pins
  7. const int encAPin = 4;
  8. const int encBPin = 5;
  9. //for sending note-off once after button is released
  10. boolean btnOff = false;
  11. //old rotary encoder value
  12. int encA = LOW;
  13. //read rotary encoder value
  14. int enc = HIGH;
  15. void setup()
  16. {
  17. //printing baudrate
  18. Serial.begin(9600);
  19. //MIDI baudrate for software serial (pin 10 & 11)
  20. MIDI.begin(31250);
  21. //button and encoder pins as input
  22. pinMode(switchPin, INPUT);
  23. pinMode(encAPin, INPUT);
  24. pinMode(encBPin, INPUT);
  25. //activate pullup-resistors (read value is inverted, so LOW is active)
  26. digitalWrite(switchPin, HIGH);
  27. digitalWrite(encAPin, HIGH);
  28. digitalWrite(encBPin, HIGH);
  29. }
  30. void loop()
  31. {
  32. //print incoming bytes on softwareserial, just for checking MIDI-in, worked
  33. if (MIDI.available())
  34. Serial.println(MIDI.read());
  35. //check if button is pressed
  36. if (digitalRead(switchPin) == LOW)
  37. {
  38. if (!btnOff) {
  39. //send note on
  40. midiSignal(144,60,100);
  41. btnOff = true;
  42. }
  43. }
  44. if (digitalRead(switchPin) == HIGH)
  45. {
  46. //send note off
  47. if (btnOff) {
  48. midiSignal(128,60,0);
  49. btnOff = false;
  50. }
  51. }
  52. //read encoder pin A
  53. enc = digitalRead(encAPin);
  54. //check if rotary encoder is turned
  55. if ((encA == HIGH) && (enc == LOW)) {
  56. //check direction of turning
  57. if (digitalRead(encBPin) == HIGH) {
  58. //send note on and note off directly, so signal is send on every turn
  59. midiSignal(144,62,100);
  60. midiSignal(128,62,100);
  61. } else {
  62. //other direction, other note value
  63. midiSignal(144,61,100);
  64. midiSignal(128,61,100);
  65. }
  66. }
  67. //save "old" encoder value
  68. encA = enc;
  69. }
  70. //send MIDI signal through softwareserial
  71. void midiSignal(byte b1, byte b2, byte b3) {
  72. //debug printing
  73. Serial.print("send: ");
  74. Serial.print(b1);
  75. Serial.print(" | ");
  76. Serial.print(b2);
  77. Serial.print(" | ");
  78. Serial.print(b3);
  79. Serial.println("");
  80. MIDI.write(b1);
  81. MIDI.write(b2);
  82. MIDI.write(b2);
  83. }