sugarcube
New member
I'm working on a project that uses two Sharp Infrared Sensors to control usb midi CC values.
Both sensors have a corresponding switch that enables midi mapping & midi CC messages.
I would like to use both sensors at the same time to control
individual elements within my DAW, but after I have mapped both sensors using their
corresponding switches I can only use one sensor at a time.
The way it stands only one sensor works if the other sensors switch is off.
Also when both sensor switches are on there is no response from either sensor :l
The code I'm using is extracted from an existing project on the PJRC website I'll attach the link at the bottom.
I realize that I probably have some repetitive code and possibly some lines that
aren't being used but I'm still a beginner and have a limited understanding
concerning programming language. I've been working on this for several days with no success, I'm pretty sure
it has to do with the storing of switch state variables but can't say for sure so any help would be much appreciated
https://github.com/ghztomash/Aftertouch-Glove
Both sensors have a corresponding switch that enables midi mapping & midi CC messages.
I would like to use both sensors at the same time to control
individual elements within my DAW, but after I have mapped both sensors using their
corresponding switches I can only use one sensor at a time.
The way it stands only one sensor works if the other sensors switch is off.
Also when both sensor switches are on there is no response from either sensor :l
The code I'm using is extracted from an existing project on the PJRC website I'll attach the link at the bottom.
I realize that I probably have some repetitive code and possibly some lines that
aren't being used but I'm still a beginner and have a limited understanding
concerning programming language. I've been working on this for several days with no success, I'm pretty sure
it has to do with the storing of switch state variables but can't say for sure so any help would be much appreciated
Code:
#include <Bounce.h>
//debugging will print out in serial instead of midi
boolean debugging=false;
// the MIDI channel number to send messages
const int midiChannel = 1;
Bounce irbutton1 = Bounce(20, 5);
Bounce irbutton2 = Bounce(21, 5);
//Analog read pins
const int irPin1 = 8; // Infrared
const int irPin2 = 9; // Infrared
const int AVERAGE_COUNT = 20; // Iterations for smoothing out the values
// hold the value when reading is triggered
double irTrig1;
double irTrig2;
int counter=0;
double ir1[AVERAGE_COUNT];
double ir2[AVERAGE_COUNT];
double irSmooth1 = 0;
double irSmooth2 = 0;
// States flag
// ir
// 0
// 1
const int OFFSTATE = 0;
const int IRSTATE1 = 1;
const int IRSTATE2 = 2;
int state = 3; // store button states
void setup() {
//DEBUG
if (debugging) {
Serial.begin(9600);//open serial port
}
else {
Serial.begin(31250);//open midi
usbMIDI.setHandleControlChange(myCCIn);
}
pinMode(20, INPUT_PULLUP); //buttons
pinMode(21, INPUT_PULLUP);
}
void loop() {
//recieve MIDI messages
if (!debugging) {
usbMIDI.read();
}
// read buttons
irbutton1.update();
irbutton2.update();
// READ VALUES
ir1[counter] = analogRead(irPin1);
ir2[counter] = analogRead(irPin2);
// check the button states
// if pressed
if (irbutton1.fallingEdge()) {
state+=1;
irTrig1 = analogRead(irPin1);
irTrig1 = constrain(irTrig1,10,900); // you also might have to change this for your sensor
}
if (irbutton2.fallingEdge()) {
state+=2;
irTrig2 = analogRead(irPin2);
irTrig2 = constrain(irTrig2,10,900); // you also might have to change this for your sensor
}
// released
if (irbutton1.risingEdge()) {
state-=1;
}
if (irbutton2.risingEdge()) {
state-=2;
}
// check triggered states
switch (state) {
case OFFSTATE: // nothing pressed, do nothing
break;
case IRSTATE1: // irbutton1 pressed, infrared sensor
irSmooth1 = 0;
for (int i=0; i<AVERAGE_COUNT; i++) // smooth value
irSmooth1 += ir1[i];
irSmooth1 = irSmooth1 / (AVERAGE_COUNT);
irSmooth1 = constrain(irSmooth1,irTrig1,970);
midiCC(map(irSmooth1,irTrig1,970,0,127),1); // send cc
break;
case IRSTATE2: // irbutton2 pressed, infrared sensor
irSmooth2 = 0;
for (int i=0; i<AVERAGE_COUNT; i++) // smooth value
irSmooth2 += ir2[i];
irSmooth2 = irSmooth2 / (AVERAGE_COUNT);
irSmooth2 = constrain(irSmooth2,irTrig2,970);
midiCC(map(irSmooth2,irTrig2,970,0,127),2); // send cc
break;
}
counter = (counter+1)%AVERAGE_COUNT; // update counter
delay(5);
}
// function to handle outgoing cc messages
void midiCC(int v, int n) {
if (debugging) {//debugging enabled
Serial.print("P:");
Serial.print(n);
for (int i=0;i<map(v,0,127,0,210); i++)
Serial.print("|");
Serial.println("");
}
else {
usbMIDI.sendControlChange(n, v, midiChannel);
}
}
// function to handle incoming cc messages
void myCCIn(byte channel, byte control, byte value) {
}
https://github.com/ghztomash/Aftertouch-Glove
Last edited: