Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LedControl.h>
- #define DinPin 12
- #define CsPin 11
- #define ClkPin 10
- #define chipno 0
- LedControl lc = LedControl(DinPin, ClkPin, CsPin, chipno);
- byte state = 0;
- byte channel = 0;
- byte note;
- boolean noteDown = LOW;
- void onLED () {
- if ((note >= 36) && (note <= 96)) {
- int col = (96 - note) / 8;
- int row = (96 - note) % 8;
- lc.setLed(0, col, row, true);
- }
- }
- void offLED ()
- {
- if ((note >= 36) && (note <= 96)) {
- int col = (96 - note) / 8;
- int row = (96 - note) % 8;
- lc.setLed(0, col, row, false);
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(13, OUTPUT);
- lc.shutdown(0, false);
- lc.setIntensity(0, 8);
- lc.clearDisplay(0);
- digitalWrite(13, LOW);
- }
- void loop() {
- if (Serial.available()) {
- byte incomingByte = Serial.read();
- switch (state) {
- case 0:
- // look for as status-byte, our channel, note on
- if (incomingByte == ( 0x90 | channel)) { // read only one channel
- noteDown = HIGH;
- state = 1;
- }
- // look for as status-byte, our channel, note off
- if (incomingByte == (0x80 | channel)) { // read only one channel
- noteDown = LOW;
- state = 1;
- }
- // look for any after touch, or program message
- if ((incomingByte & 0xE0) == 0xC0) {
- state = 4; // just wait for the data
- }
- // look for any control or polyphonic after touch
- if ((incomingByte & 0xE0) == 0xA0) {
- state = 3; // just wait for two bytes of data
- }
- // look for any pitch wheel or Channel Mode data
- if ((incomingByte & 0xF0) == 0xA0 || (incomingByte & 0xF0) == 0xB0) {
- state = 3; // just wait for two bytes of data
- }
- break;
- case 1:
- // get the note to play or stop
- if (incomingByte < 128) {
- note = incomingByte;
- state = 2;
- }
- else {
- state = 0; // reset state machine as this should be a note number
- }
- break;
- case 2:
- // get the velocity
- if (incomingByte < 128) {
- doNote(note, incomingByte, noteDown);
- }
- state = 0; // reset state machine to start
- break;
- case 3: // first of two bytes to discard
- state = 4; // next byte to discard
- break;
- case 4: // data to discard
- state = 0; // reset state machine
- }
- }
- }
- void doNote(byte note, byte velocity, int down) {
- // if velocity = 0 on a 'Note ON1 command, treat it as a note off
- if ((down == HIGH) && (velocity == 0)) {
- down = LOW;
- }
- // send out this note message
- if (down == LOW) offLED();
- else onLED();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement