Hi all.
I'm doing a project where I use the signal from a muscle sensor (https://www.sparkfun.com/products/13723) to trigger midi in Ableton. I'm doing this via the Hairless Midi-serial bridge and the arduino IDE.
I've mapped the analog signal from the muscle sensor to the midi-range and i've calibrated the values for when my muscle is relaxed and when it's contracted. I only want to send midi signal when it's contracted. so every time i flex my muscle it triggers a sample or a note in ableton.
I've been trying to do this with the threshold function, but it does not seem to work. it just sends midi continuously, even though i've set up the threshold function.
My code is as follows:
int MuscleSensor = 0; // connected to A0
int Signal; // incoming raw data
int Mapped; // mapped Signal
int velocity = 100;
int noteON = 144;
int noteOFF = 128;
int note = 60;
void setup() {
Serial.begin(9600);
}
void loop()
{
Signal = analogRead(MuscleSensor); //read values
Serial.println(Signal); // plot values
Mapped = map(Signal, 0, 1023, 0, 127); // map values to midi scale
Serial.println(Mapped);
Serial.println(" ");
delay(500);
if (Signal > 600)
{ MIDImessage(noteON, note, velocity);
delay(300);
}
else
{
}
}
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
Serial.write(command);
Serial.write(MIDInote);
Serial.write(MIDIvelocity);
}
can you tell me what I'm doing wrong, or if I am going about this the wrong way?
thank you.
What values are you getting from the sensor? Are you using the RAW output or SIG output?
Also, it doesn't look like you ever use the noteOFF variable. Is your code supposed to turn the midi off when the signal drops below 600? If so, you haven't written that in the else statement yet.
Hi! I'm getting values between 200-600 approximately, a little over and a little under. that's when I do analogRead on the pin.
I soldered three pins onto the sensor like this (MyoWare Servo Tutorial - EMG Arduino Circuit - YouTube) watch from 2 minute mark, so i'm guessing it's the SIG output? Sorry if I am being totally oblivious.
I know I'm not using the noteOFF variable, it's my fault. it didn't work, so I just deleted it, to see if that would work, I should have had that in the code i provided here.
Yes, it is supposed to turn midi off when the signal drops below 600. I want to be able to trigger a midi note whenever i flex a muscle (when the signal is over 600), and turn off whenever it drops below ( relaxed muscle).. do you think you can help me?
And thank you so much for your response, and thank you for your cool products!
best regards
I think we can get your code in order. What you're trying to do isnt that different than how we control our Iron Man repulsors or Wolverine claws.
Do you want the note to play continuously when above the threshold or just once when it crosses? Trying to understand your intent a little better.
Also, is the mapped variable something you functionally took out as well?
that sounds just great!
What I want to do is to just play one note (when that is achieved in the code, I will get it to trigger a sample, but that is doable within Ableton Live) and then to stop the note whenever it is under 600.
the mapped variable is something i saw in another tutorial, but maybe I don't need it, if I can just get the threshold function to work with the signal straight from the muscle sensor.
I was just told to map the values to midi (1-127) BUT if the threshold works with the signal values, I don't see why I need it.
But thank you so much for helping out, means the world to me. been sweating over this a couple of days now.
Mapping like that would make sense if you want to change the note based on how hard the sensor is flexing. Is that what you want?
Here's simple code to have the note play while the sensor value exceeds the threshold and turns off when it drops back below it.
Note: treat this as pseudo-code... I don't have the Arduino IDE on this machine and I haven't compiled it.
int MuscleSensor = A0; // connected to A0
// note variables
int velocity = 100;
int note = 60;
int noteON = 144;
int noteOFF = 128;
int noteThreshold = 200; // value muscle sensor must reach to play the note
void setup()
{
Serial.begin(9600);
}
void loop()
{
int Signal = analogRead(MuscleSensor); //read value from sensor
if (Signal >= noteThreshold)
{
// play note
MIDImessage(noteON, note, velocity);
}
else if (Signal < noteThreshold)
{
// stop note
MIDImessage(noteOFF, note, velocity);
}
delay(50);
}
Hi again, thank you so much!
I know you said to treat it as pseudo-code but I've run through it in the IDE and it seems fine. the problem is it won't compile, as it says that MIDImessage isn't declared.
this is the error code i receive:
Arduino: 1.8.2 (Mac OS X), Board: "Arduino/Genuino Uno"
sketch_jun13a.ino: In function 'void loop()':
sketch_jun13a:23: error: 'MIDImessage' was not declared in this scope
MIDImessage(noteON, note, velocity);
^
sketch_jun13a:28: error: 'MIDImessage' was not declared in this scope
MIDImessage(noteOFF, note, velocity);
^
exit status 1
'MIDImessage' was not declared in this scope
do you know how to work around this? I've tried #include <MIDI.h> but that does not seem to work either..
thank you for your time!
Right you still have to include the function you wrote
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
Serial.write(command);
Serial.write(MIDInote);
Serial.write(MIDIvelocity);
}
Hi again. I just wanted to say thank you so much, it works now, and it works completely like I imagined it in my head. Thank you so much for your BIG help!
I might just ask you some stuff again, when I want to expand on this, if it is okay?
Great! Always happy to help.