Advertisement
vmuller

Untitled

Oct 6th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LedControl.h>
  2.  
  3. #define DinPin 12
  4. #define CsPin 11
  5. #define ClkPin 10
  6. #define chipno 0
  7.  
  8. LedControl lc = LedControl(DinPin, ClkPin, CsPin, chipno);
  9.  
  10. byte state = 0;
  11. byte channel = 0;
  12. byte note;
  13.  
  14. boolean noteDown = LOW;
  15.  
  16.  
  17. void onLED () {
  18.   if ((note >= 36) && (note <= 96)) {
  19.     int col = (96 - note) / 8;
  20.     int row = (96 - note) % 8;
  21.     lc.setLed(0, col, row, true);
  22.   }
  23. }
  24.  
  25. void offLED ()
  26. {
  27.   if ((note >= 36) && (note <= 96)) {
  28.     int col = (96 - note) / 8;
  29.     int row = (96 - note) % 8;
  30.     lc.setLed(0, col, row, false);
  31.   }
  32. }
  33.  
  34. void setup() {
  35.   Serial.begin(115200);
  36.   pinMode(13, OUTPUT);
  37.   lc.shutdown(0, false);
  38.   lc.setIntensity(0, 8);
  39.   lc.clearDisplay(0);
  40.   digitalWrite(13, LOW);
  41. }
  42.  
  43. void loop() {
  44.   if (Serial.available()) {
  45.     byte incomingByte = Serial.read();
  46.  
  47.     switch (state) {
  48.       case 0:
  49.         // look for as status-byte, our channel, note on
  50.         if (incomingByte == ( 0x90 | channel)) {  // read only one channel
  51.           noteDown = HIGH;
  52.           state = 1;
  53.         }
  54.         // look for as status-byte, our channel, note off
  55.         if (incomingByte == (0x80 | channel)) {   // read only one channel
  56.           noteDown = LOW;
  57.           state = 1;
  58.         }
  59.         // look for any after touch, or program message
  60.         if ((incomingByte & 0xE0) == 0xC0) {
  61.           state = 4; // just wait for the data
  62.         }
  63.         // look for any control or polyphonic after touch
  64.         if ((incomingByte & 0xE0) == 0xA0) {
  65.           state = 3; // just wait for two bytes of data
  66.         }
  67.         // look for any pitch wheel or Channel Mode data
  68.         if ((incomingByte & 0xF0) == 0xA0 || (incomingByte & 0xF0) == 0xB0) {
  69.           state = 3; // just wait for two bytes of data
  70.         }
  71.         break;
  72.       case 1:
  73.         // get the note to play or stop
  74.         if (incomingByte < 128) {
  75.           note = incomingByte;
  76.           state = 2;
  77.         }
  78.         else {
  79.           state = 0;   // reset state machine as this should be a note number
  80.         }
  81.         break;
  82.       case 2:
  83.         // get the velocity
  84.         if (incomingByte < 128) {
  85.           doNote(note, incomingByte, noteDown);
  86.         }
  87.         state = 0;   // reset state machine to start
  88.         break;
  89.       case 3: // first of two bytes to discard
  90.         state = 4; // next byte to discard
  91.         break;
  92.       case 4: // data to discard
  93.         state = 0; // reset state machine
  94.     }
  95.   }
  96. }
  97.  
  98. void doNote(byte note, byte velocity, int down) {
  99.   // if velocity = 0 on a 'Note ON1 command, treat it as a note off
  100.   if ((down == HIGH) && (velocity == 0)) {
  101.     down = LOW;
  102.   }
  103.   // send out this note message
  104.   if (down == LOW) offLED();
  105.   else onLED();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement