#include #include "Adafruit_MCP23017.h" Adafruit_MCP23017 mcp; int debug = 13; //Led de debugage /* Definition des octets de la charte midi */ #define midi_start 0xFA #define midi_stop 0xFC #define midi_clock 0xF8 #define midi_continue 0xFB byte data; // Bit boolean play_flag = false; boolean CHOIX = false; boolean flag = false; boolean changeFlag = false; int tickNumber = 0; float BPM = 0; float BPM2 = 0; unsigned int bpmSansFP; unsigned int bpmSansFP1; float time2; byte wireTime; byte wireTime1; byte wireTime2; byte wireTimeFP; unsigned char wireTimeBuf[2]; unsigned long time = 0; //unsigned char wireTime[6]; void setup() { digitalWrite(debug, OUTPUT); pinMode(2, INPUT_PULLUP); Wire.begin(8); // Bus i2c adressé a la 8 Wire.onRequest(requestEvent); Serial.begin(31250); mcp.begin(); mcp.pinMode(0, OUTPUT); mcp.pinMode(1, OUTPUT); mcp.pinMode(2, OUTPUT); mcp.pinMode(3, OUTPUT); mcp.pinMode(4, OUTPUT); mcp.pinMode(5, OUTPUT); mcp.pinMode(6, OUTPUT); mcp.pinMode(7, OUTPUT); mcp.pinMode(8, OUTPUT); mcp.pinMode(9, OUTPUT); mcp.pinMode(10, OUTPUT); mcp.pinMode(11, OUTPUT); mcp.pinMode(12, OUTPUT); mcp.pinMode(13, OUTPUT); mcp.pinMode(14, OUTPUT); mcp.pinMode(15, OUTPUT); } void loop() { if (CHOIX != (digitalRead(2))) { CHOIX = digitalRead(2); play_flag = false; allLow(); } int donneesALire = Serial.available(); donneesALire = (donneesALire / 4); while (donneesALire == 1) { data = Serial.read(); if (data == midi_start) { tickNumber = 0; play_flag = true; allLow(); } else if (data == midi_continue) { play_flag = true; } else if (data == midi_stop) { play_flag = false; } else if ((data == midi_clock) && (play_flag = true)) { tickNumber++; if (tickNumber == 1) { time = micros(); mcp.digitalWrite(0, LOW); mcp.digitalWrite(7, HIGH); mcp.digitalWrite(15, LOW); mcp.digitalWrite(8, HIGH); } if (tickNumber == 2) {} if (tickNumber == 3) {} if (tickNumber == 4) { mcp.digitalWrite(7, LOW); mcp.digitalWrite(6, HIGH); mcp.digitalWrite(8, LOW); mcp.digitalWrite(9, HIGH); } if (tickNumber == 5) {} if (tickNumber == 6) {} if (tickNumber == 7) { mcp.digitalWrite(6, LOW); mcp.digitalWrite(5, HIGH); mcp.digitalWrite(9, LOW); mcp.digitalWrite(10, HIGH); } if (tickNumber == 8) {} if (tickNumber == 9) {} if (tickNumber == 10) { mcp.digitalWrite(5, LOW); mcp.digitalWrite(4, HIGH); mcp.digitalWrite(10, LOW); mcp.digitalWrite(11, HIGH); } if (tickNumber == 11) {} if (tickNumber == 12) {} if (tickNumber == 13) { mcp.digitalWrite(4, LOW); mcp.digitalWrite(3, HIGH); mcp.digitalWrite(11, LOW); mcp.digitalWrite(12, HIGH); } if (tickNumber == 14) {} if (tickNumber == 15) {} if (tickNumber == 16) { mcp.digitalWrite(3, LOW); mcp.digitalWrite(2, HIGH); mcp.digitalWrite(12, LOW); mcp.digitalWrite(13, HIGH); } if (tickNumber == 17) {} if (tickNumber == 18) {} if (tickNumber == 19) { mcp.digitalWrite(2, LOW); mcp.digitalWrite(1, HIGH); mcp.digitalWrite(13, LOW); mcp.digitalWrite(14, HIGH); } if (tickNumber == 20) {} if (tickNumber == 21) {} if (tickNumber == 22) { mcp.digitalWrite(1, LOW); mcp.digitalWrite(0, HIGH); mcp.digitalWrite(14, LOW); mcp.digitalWrite(15, HIGH); } if (tickNumber == 23) {} if (tickNumber == 24) // Lorsque 24 tick ont été recus (norme MIDI) { time = micros() - time; time2 = time; // On regarde le temps écoulé depuis le premier tick et l'inscrit dans la variable time = 0; time = micros(); BPM = (time2 / 1000); BPM2 = (1 / (BPM / 1000)) * 57.52; bpmSansFP = (BPM2 * 100); wireTime = BPM2; // Transtypage grace au passage du float a l'octet wireTimeFP = (bpmSansFP - (wireTime * 100)); // Transtypage avec une technique différente wireTimeBuf[0] = wireTime; wireTimeBuf[1] = wireTimeFP; tickNumber = 0; } } } } void requestEvent() { Wire.write(wireTimeBuf, 2); } void allLow() { mcp.digitalWrite(0, LOW); mcp.digitalWrite(1, LOW); mcp.digitalWrite(2, LOW); mcp.digitalWrite(3, LOW); mcp.digitalWrite(4, LOW); mcp.digitalWrite(5, LOW); mcp.digitalWrite(6, LOW); mcp.digitalWrite(7, LOW); mcp.digitalWrite(8, LOW); mcp.digitalWrite(9, LOW); mcp.digitalWrite(10, LOW); mcp.digitalWrite(11, LOW); mcp.digitalWrite(12, LOW); mcp.digitalWrite(13, LOW); mcp.digitalWrite(14, LOW); mcp.digitalWrite(15, LOW); }