Deck 1 231-001 06 FX - Arduino sketch
//LIBRARIES
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
//LED and led mux
#include <Wire.h>
#include "ht16k33.h"
HT16K33 HT; // Define the class
//Pot
int pot[] = {A0,A1,A2}; //pots connected to these pins
int potcurrent[] = {0,1,2};
int potprev[] = {0,1,2};
int potCC[] = {14,15,16}; //CC values to map to pots (dec 14,15,16 = hex e,f,10)
int filter = 2;
//pushbuttons
int pushbutton[] = {10,11,12}; //pots connected to these pins
int pushbuttoncurrent[] = {0,1,2};
int pushbuttonprev[] = {0,1,2};
int pushbuttonNotes[] = {21,22,23}; //aka A0,A#0,B0; hex 15,16,17
//Leds
int led[128] = { //led(HT16K33 mux) adresses/midi notes - led address is the same as midi note 0-127
//a la fuzzywobble github.com/FuzzyWobble/DIY-Controller/blob/master/ControllerCode_FuzzyWobble/ControllerCode_FuzzyWobble.pde
//make sure to map each LED address to the correct MIDI note in VirtualDJ
//A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 anode pins
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C0 cathode pins
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C1
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C3
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C4
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C5
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //C6
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //C7
};
//MIDI general
const int midiChan = 1; //set MIDI channel
const int midiNoteVel = 100; //set MIDI note on value
const int midiCCMax = 127; //max value for MIDI CC
//variables to store current MIDI message data
int sendMidiNote;
int sendMidiVel;
int sendMidiChan;
int sendMidiCCno;
int sendMidiCCval;
//debug variables
String element; //type of hardware input being read
int elementNo; //number ID of element that read
int rawVal; //value that was read
void setup() {
// put your setup code here, to run once:
//pot setup
for (int i=0; i<3; i++){
pinMode(pot[i], INPUT);
potcurrent[i] = 0;
potprev[i] = 0;
}
//pushbutton setup
for (int i=0; i<3; i++){
pinMode(pushbutton[i], INPUT);
pushbuttoncurrent[i] = 0;
pushbuttonprev[i] = 0;
}
//LED setup
Wire.setSDA(18);//set data pin which is attached to the I2C bus on the HT16K33
Wire.setSCL(19);//set clock pin which is attached to the I2C bus on the HT16K33
HT.begin(0x00);
usbMIDI.setHandleNoteOn(readNoteOn); //when a MIDI note on message is recieved, go to (function)
usbMIDI.setHandleNoteOff(readNoteOff); //when a MIDI note off message is recieved, go to (function)
//Blink LEDs just make it look pretty
for (int led=0; led<128; led++) {//turn on all LEDs
HT.setLed(led);
} // for led
HT.sendLed();
for (int led=0; led<128; led++) {//turn off all LEDs
HT.clearLed(led);
} // for led
HT.sendLed();
void loop() {
// put your main code here, to run repeatedly:
potread();
pushbuttonread();
while(usbMIDI.read()); //read MIDI messages from channel 0, discard all others
}
void potread(){ //pot read and filter
for(int i=0; i<3; i++){
potcurrent[i] = analogRead(pot[i]);
if(potcurrent[i] < potprev[i] - filter || potcurrent[i] > potprev[i] + filter){ //if new value is outisde range of last value
//set MIDI variables
sendMidiCCval = map(potcurrent[i], 0, 1023, 0, midiCCMax); //convert to MIDI CC range; map(value, fromLow, fromHigh, toLow, toHigh)
//then assign to variable ready to send
sendMidiCCno = potCC[i]; //set correct MIDI CC number to attach value to
sendMidiCC(); //send MIDI message
//set debug variables
element = "pot"; //set debug variable type
elementNo = i; //set debug type number
rawVal = potcurrent[i]; //set debug value
datasend(); //send data
potprev[i] = potcurrent[i]; //save current value for comparison next time
}//close if
}//close for
}
void pushbuttonread(){ //pushbutton read and filter
for(int i=0; i<3; i++){
pushbuttoncurrent[i] = digitalRead(pushbutton[i]);
if(pushbuttoncurrent[i] != pushbuttonprev[i]){ //if new value is different from last value
//set MIDI variables
sendMidiNote = pushbuttonNotes[i]; //note to send
sendMidiChan = midiChan; //channel to send
if(pushbuttoncurrent[i] == HIGH){ //if pin is high, send note on
sendMidiVel = midiNoteVel; //velocity to send
sendMidiNoteON(); //call function to send midi note on, using values just defined
}
if(pushbuttoncurrent[i] == LOW){ //if pin is low, send note off
sendMidiVel = 0; //velocity to send
sendMidiNoteOFF(); //call function to send midi note on, using values just defined
}
//set debug variables
element = "pushbutton";//set debug variable type
elementNo = i; //set debug type number
rawVal = pushbuttoncurrent[i]; //set debug value
datasend(); //send data
pushbuttonprev[i] = pushbuttoncurrent[i]; //save current value for comparison next time
}//close if
}//close for
void readNoteOn(byte channel, byte note, byte velocity){ //when a MIDI 'note on' message is recieved
if(led[note] == 1){ //check to see if LED which corresponds to the MIDI note
//has been activated (set to 1) in the led/mux array
HT.setLedNow(note); //if the LED is in use, use the mux to turn on the LED
}
Serial.println(note);
}
void readNoteOff(byte channel, byte note, byte velocity){ //when a MIDI 'note off' message is recieved
if(led[note] == 1){ //check to see if LED which corresponds to the MIDI note
//has been activated (set to 1) in the led/mux array
HT.clearLedNow(note); //if the LED is in use, use the mux to turn off the LED
}
}
void datasend(){
Serial.print(element);
Serial.print(elementNo);
Serial.print(": ");
Serial.println(rawVal);
}
void sendMidiNoteON() { //function to send midi note on message, and print in serial monitor
usbMIDI.sendNoteOn (sendMidiNote, sendMidiVel, sendMidiChan);
Serial.print("Note ON ");
Serial.print(sendMidiNote);
Serial.print(" ");
Serial.print(sendMidiVel);
Serial.print(" ");
Serial.println(sendMidiChan);
}
void sendMidiNoteOFF(){ //function to send midi note off message, and print in serial monitor
usbMIDI.sendNoteOn (sendMidiNote, sendMidiVel, sendMidiChan);
Serial.print("Note OFF ");
Serial.print(sendMidiNote);
Serial.print(" ");
Serial.print(sendMidiVel);
Serial.print(" ");
Serial.println(sendMidiChan);
}
void sendMidiCC(){ //send MIDI control change message, and print in serial monitor
usbMIDI.sendControlChange(sendMidiCCno, sendMidiCCval, sendMidiChan); //(control, value, channel)
Serial.print("CC ");
Serial.print(sendMidiCCno);
Serial.print(" ");
Serial.print(sendMidiCCval);
Serial.print(" ");
Serial.println(sendMidiChan);