Multiple Midi Switch States

Status
Not open for further replies.

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 :)

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:
Here's a cleaned up version of the code above ^ that's probably a lot easier to decipher.
How do I get both sensors to send cc values simultaneously?
Any ideas would be awesome!
Thanks!

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 button1 = Bounce(20, 5);
Bounce button2 = Bounce(21, 5);

//Analog read pins

const int Pin1 = 8; // Infrared 
const int Pin2 = 9; 



const int AVERAGE_COUNT = 20; // Iterations for smoothing out the values

// hold the value when reading is triggered

double Trig1;
double Trig2;


int counter=0;


double ir1[AVERAGE_COUNT]; // store readings every cycle
double ir2[AVERAGE_COUNT]; 


double Smooth1 = 0;// store the smoothed out value
double Smooth2 = 0;

// States flag
// ir
//  0 
//  1 

const int OFFSTATE = 0;

const int STATE1 = 1;

const int STATE2 = 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

  button1.update();
  button2.update();
  
  // READ VALUES

  ir1[counter] = analogRead(Pin1);
  ir2[counter] = analogRead(Pin2);

  // check the button states
  // if pressed

  if (button1.fallingEdge()) {
    state+=1;                        // update triggered state
    Trig1 = analogRead(Pin1);        // save reading
    Trig1 = constrain(Trig1,10,900); // you might have to change this for your sensor
  }
  if (button2.fallingEdge()) {
    state+=2;
    Trig2 = analogRead(Pin2);
    Trig2 = constrain(Trig2,10,900);
  }
  // released
  if (button1.risingEdge()) {
    state-=1;
  }
  
  if (button2.risingEdge()) {
    state-=2;
  }
  
  // check triggered states
  switch (state) {
    case OFFSTATE: // nothing pressed, do nothing
      break;
      
    case STATE1: // button1 pressed, infrared sensor
      Smooth1 = 0;
      for (int i=0; i<AVERAGE_COUNT; i++) // calculate the average of AVERAGE_COUNT readings
        Smooth1 += ir1[i];                // for a smooth value
      Smooth1 = Smooth1 / (AVERAGE_COUNT);
      Smooth1 = constrain(Smooth1,Trig1,970); // constrain 
      midiCC(map(Smooth1,Trig1,970,0,127),1); // send cc message
      break;
     
      
    case STATE2: // button2 pressed, infrared sensor
    
      Smooth2 = 0;
      for (int i=0; i<AVERAGE_COUNT; i++) 
        Smooth2 += ir2[i];
      Smooth2 = Smooth2 / (AVERAGE_COUNT);
      Smooth2 = constrain(Smooth2,Trig2,970);
      midiCC(map(Smooth2,Trig2,970,0,127),2); 
      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) {
}
 
Status
Not open for further replies.
Back
Top