OK, I am building one of these devices, I designed and coded 90% of it before looking at this thread. ill share my code here and see what you think so far. I am having trouble with the code for the BlinkM, but thats in another thread so ill skip it.
//Touchpad
#define TouchX 0 // X axis
#define TouchY 1 // Y Axis
//Rotary Encoder
#define RotorA 3 //Interrupt pin 1
#define RotorB 6 //Digital IO 6
//Hold button
#define HoldButton 2 //Interrupt 0
//Switches
#define S1 4 //GP button 1
#define S2 5 //GP Button 2
#define debounce 10 //Debounce value
//Led
#define LED 13 // LED Pin
//Touchpad Variables
int touchX = 0;
int touchY = 0;
//Hold Flag
volatile int holdflag = LOW; //Hold Flag
//Rotary encoder
int currVal = 1; //Value of rotation
void setup()
{
//Serial Setup for output of MIDI
Serial.begin(31250);//midi=31250 baud
pinMode(LED, OUTPUT);
attachInterrupt(0, hold, HIGH);//Hold Button interrupt pin
attachInterrupt(1, rotate, HIGH);//Rotary Encoder interrupt pin
//Blinkm I2C setup
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send('c'); // 'c' == fade to color
Wire.send(0xff); // value for red channel
Wire.send(0xff); // value for blue channel
Wire.send(0xff); // value for green channel
Wire.endTransmission(); // leave I2C bus
}
void loop() {
if (touched())
{
midiCC(0xB0,92,127); //Touchpad down command
midiX = map(touchX,0,1016,0,127); // maps the value range of the axis to midi 0->127
midiCC(0xB0, 12, midix); // sends midi control change for touch pad X axis
midiy = map(touchy,0,1016,0,127); // maps the value range of the axis to midi 0->127
midiCC(0xB0, 13, midiy); // sends midi control change for touch pad y axis
}
else
{
if(holdflag)
{
digitalWrite(LED, HIGH);
}
else
{
midiCC(0xB0,92,0) //Touchpad off
digitalWrite(LED, LOW);
}
}
// return TRUE if touched, and coords in touchX and touchY
boolean touched()
{
boolean touch = false;
// wait a bit, then read from Top
delay(10);
touchX = analogRead(TouchchX);
// wait a bit, then read from Right
delay(10);
touchY = analogRead(TouchchY);
// Only if coords are below 1000 and above 0
//TODO set up calibration for not touching.
if(0 < touchX < 1000 and 0 < touchY < 1000)
touch = true;
return touch;
}
//Toggles hold on interrupt 0 goes high
void hold()
{
delay(debounce) //Wait a bit
if(digitalRead(HoldButton) == HIGH) //Check if its still toggled.
{
holdflag = !holdflag; //Set flag
}
}
//Interrupt called by rotary encoder interrupt
void rotate()
{
if(digitalRead(RotorB) == HIGH) //Interrupt is called, means RotorA has changed, direction can be found by lookign at RotorB
currVal++; //If High, ++
if(digitalRead(RotorB) == LOW)
currVal--; //If low, --
//sanitise output and wrap to 127 programs.
if(currVal > 127)
currVal = currVal - 127;
if(currVal < 0)
currVal = currVal + 127;
midiCC(0xB0,0xC0,currVal); //0xC0 is the Program change cc so send midi command.
//Send MIDI command
void midiCC(char command, char value1, char value2){
Serial.print(command, BYTE);
Serial.print(value1, BYTE);
Serial.print(value2, BYTE);
}
Ideas and comments please.