Dilicktal_MIDI v2_0
//****************************************************************//
// Dilicktal_MIDI v2_0 //
// //
// This sketch shows how to use Dilicktal platinum //
// To send MIDI messages with buttons and display the //
// State of the button on the corresponding LED. //
// //
// The buttons are in TOGGLE mode, which means that when the //
// First pressure is sent to MIDI ON (127) and the button //
// Remains ON until the second button press. //
// //
// The SHIFTOUT SHIFTIN and functions used in this sketch //
// Tutorials are available on the Arduino website //
// Http://www.arduino.cc/en/Tutorial/ShiftIn //
// Http://arduino.cc/en/Tutorial/ShiftOut //
//****************************************************************//
// Number of connected Dilicktal board (1 to 8)
#define numDigital 1
// Set PINs of 4021
#define latchPinIn 10
#define dataPinIn 11
#define clockPinIn 9
// Set PINs on 74hc595
#define latchPinOut 12
#define clockPinOut 9
#define dataPinOut 13
byte preButtonRead[numDigital] = { 0 }; // the value of flag EVERY buttons
byte led[numDigital] = { 0 }; // send value EVERY turntables Dilicktal
byte stateButton[numDigital] = { 0 }; // state of EVERY buttons
boolean stateLed[numDigital][8]= { 0 }; // EVERY status leds
byte ButtonCC[]={ 0,8,16,24,32,40,48,56 }; // Control number changes to EVERY Dilicktal
void setup() {
// Serial baud rate to 57600 to use with USB and 31250 for use in MIDI
Serial.begin(31250);
//define pin modes
pinMode(latchPinIn, OUTPUT);
pinMode(clockPinIn, OUTPUT);
pinMode(dataPinIn, INPUT);
pinMode(latchPinOut, OUTPUT);
SendToLed (255,255,255,255,255,255,255,255); //Turn all LEDs on
delay (1000); //Wait 1 second
SendToLed (0,0,0,0,0,0,0,0); //Turn all LEDs off
}
void loop() {
for (int y = 0; y < numDigital; y++){ // loop as many times as the number of connected Dilicktal
preButtonRead[y] = ButtonRead(y); // Get the current value of the byte buttons corresponding to the Dilicktal board
if (stateButton[y] != preButtonRead[y]){ // Has the buttons byte value changed?
stateButton[y] = preButtonRead[y]; // Store the value of the changed byte
for (int x = 0; x < 8; x++){ //scan the 8 bits of byte
if (bitRead (preButtonRead[y], x)){ //EVERY test buttons
if (stateLed[y][x]==0){ //if the corresponding LED on the button is set to 0
bitSet (led[y],x); // they put 1
stateLed[y][x]=1; // it updates the state of the led
sendCC ((x+ButtonCC[y]),127); // It sends the message MIDI Control Change "ON" corresponding to the selected channel
}
else{ // but if the corresponding LED on the button is 1
bitWrite (led[y],x,0); // It puts 0
stateLed[y][x]=0; // We update the state of the led
sendCC ((x+ButtonCC[y]),0); // It sends the message MIDI Control Change "OFF" corresponding to the selected channel
}
}
}
SendToLed (led[0],led[1],led[2],led[3],led[4],led[5],led[6],led[7]); //Sends 8 bytes corresponding to the LEDs
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////FUNCTION//////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
void SendToLed (byte data1,byte data2,byte data3,byte data4,byte data5,byte data6,byte data7,byte data8) {
digitalWrite(latchPinOut, 0);
delayMicroseconds(20);
shiftOut(dataPinOut, clockPinOut, data8);
shiftOut(dataPinOut, clockPinOut, data7);
shiftOut(dataPinOut, clockPinOut, data6);
shiftOut(dataPinOut, clockPinOut, data5);
shiftOut(dataPinOut, clockPinOut, data4);
shiftOut(dataPinOut, clockPinOut, data3);
shiftOut(dataPinOut, clockPinOut, data2);
shiftOut(dataPinOut, clockPinOut, data1);
digitalWrite(latchPinOut, 1);
}
byte ButtonRead (int flag) {
if (flag ==0){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==1){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==2){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==3){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn); // We stock the two bytes in a variable
return i;
}
if (flag ==4){
int i;
digitalWrite(latchPinIn,1); // It puts in play mode 4021
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==5){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==6){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
if (flag ==7){
int i;
digitalWrite(latchPinIn,1);
delayMicroseconds(20);
digitalWrite(latchPinIn,0);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
shiftIn(dataPinIn, clockPinIn);
i = shiftIn(dataPinIn, clockPinIn);
return i;
}
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
}
byte shiftIn(int myDataPin, int myClockPin) {
int i;
int temp = 0;
int pinState;
byte myDataIn = 0;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);
for (i=7; i>=0; i--)
{
digitalWrite(myClockPin, 0);
delayMicroseconds(2);
temp = digitalRead(myDataPin);
if (temp) {
pinState = 1;
myDataIn = myDataIn | (1 << i);
}
else {
pinState = 0;
}
digitalWrite(myClockPin, 1);
}
return myDataIn;
}
void sendCC (int CCnum, int value)
{
Serial.write (0xC0); // Message " program change " on MIDI channel 1
Serial.write (CCnum);
Serial.write (value);
}