5 Flex sensors to MIDI

Hello,

For a theatre project I want to stick 5 flex sensors to a hand.
Each flex sensor sends midi data over usb.
This is the code that works perfectly to do this for 1 flex sensor (or also works with a LDR light sensor).
Now when I try this with more than 1 sensor I begin to have problems in my hairless MIDI output data.
I'm working on arduino uno and mac 10.12 and I use hairless MIDI for the conversion from serial to usb.
Then I use the MIDI data in ableton live.
I want that every sensor sends a specific range of MIDI notes.
Like the first sensor notes 0 - 15, 2nd sensor note 16 - 30 and so on.
I imagine that this is perfectly possible with the map function.

This is the code with one flex sensor:

byte noteON = 144;//note on command
int analogPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  int analogVal = analogRead(analogPin);//read data
  
  //we have to scale the lsr data to fit between 0 and 127 (this is the range of MIDI notes)
  byte note = map(analogVal, 0, 900, 0, 127);//use the 0-900 range I measured
  
  MIDImessage(noteON, note, 100);//turn note on
  delay(300);//hold note for 300ms
  MIDImessage(noteON, note, 0);//turn note off (note on with velocity 0)
  delay(200);//wait 200ms until triggering next note
}

//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
}

This is my attempt for 2 sensors an LDR and a flex sensor, wich has not been working.
I get my data in hairless , which are more different MIDI notes. But when I use the MIDI notes that come into Ableton there is one note constantly playing the same tone.

byte noteON = 144;//note on command

int analogPin5 = A5;// LSR sensor
int analogPin1 = A1;// FLEX sensor

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  int analogVal5 = analogRead(analogPin5);//read data
  int analogVal1 = analogRead(analogPin1);
  //we have to scale the lsr data to fit between 0 and 127 (this is the range of MIDI notes)
  byte note5 = map(analogVal5, 0, 900, 0, 50);//use the 0-900 range I measured
  byte note1 = map(analogVal1, 0, 900, 51, 127);
  
  MIDImessage(noteON, note5, 100);//turn note on
  delay(300);//hold note for 300ms
  MIDImessage(noteON, note5, 0);//turn note off (note on with velocity 0)
  delay(200);//wait 200ms until triggering next note

  MIDImessage(noteON, note1, 100);//turn note on
  delay(300);//hold note for 300ms
  MIDImessage(noteON, note1, 0);//turn note off (note on with velocity 0)
  delay(200);//wait 200ms until triggering next note
 
}

//send MIDI message
void MIDImessage(byte command, byte data1, byte data2) {
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
  
}

Is it possible to use multiple sensors with this code in a usable way?
Or another way that I can make this project work?
It is necessary that the MIDI goes via usb, without a hardware MIDI port.

Thank you very much for any help!

I may be missing something but if you can see all the MIDI messages coming through correctly in Hairless then surely the problem is not in the Arduino program but in something at the backend of Hairless?

With all those delays in the program i’s not going to be very responsive. With 5 inputs it will only be reading the inputs about once every 3 seconds but that may be o.k. for your purposes. Of course you could always write all the NoteOns, then delay and then write all the NoteOffs.

BTW why only 9600 for Serial…the Hairless default is 115200 IIRC.

Steve

Hey, thank you for the replay.
I changed my baud rate and this really helped out, now I’m getting my 2 different MIDI signals.

It is important that all the sensors act fast. What do you mean with the delays? Do I just lower them or?

Also in Ableton and in hairless I am using channel 1.
Is it in a way possible to send each sensor to a different channel? Is this possible on one serial port?

Pictures below.
Thanks in advance!

Certainly you can send notes on different MIDI channels. Your NoteON = 144 includes the channel number. If you make some of them 145 that will be Ch2, 146 Ch3 etc.

The delays as you have them means that each note takes a full 1/2 second and they all add up. A better way to do it might be:

  MIDImessage(noteON, note5, 100);// Ch1, turn on as many notes as needed
  MIDImessage(noteON+1, ...., 100);// Ch2
  MIDImessage(noteON+2, ..., 100); // Ch3 etc
  delay(300);                                   // then the delay    
  MIDImessage(noteON, note5, 0);//turn all notes off (note on with velocity 0)
  MIDImessage(noteON+1, ...., 0);
  MIDImessage(noteON+2, ...., 0);
  delay(200);                              //and then wait 200ms until triggering next notes

That way all notes are sent in about 1/2 second total.

Give that a try.

Steve

Wow this works.
Now I can route each channel to different speakers!

Thanks!

You're very welcome, it sounds like an interesting project. Good luck with it.

Steve