LDR to Midi note, but why is it strumming?

Hey People

Setup:
One Laser beam to a LDR. When the beam is broken, it turns on a LED and sends a midi note (middle C, 0x 60) through a midi output (http://itp.nyu.edu/physcomp/Labs/MIDIOutput). However, the LED is lit up for as long as I am breaking the beam, but the note is strumming with the same pace as the delay eventhought the value from the LDR is >=600. It seems like the midimessage keeps jumping into the else loop.
How can I make the midi note last for as long as I am breaking the beam?
If I make the delay longer the note will hold for that amount of time so that is not really working.

Anyone who can see the soulution for my problem? or have a nicer way of writing the code?

Here is the code I'm using--->

////////////////////////////////////////////////////////////////////////////////////////////////////////
// One string laserharp, sending a midi message when beam is broken
// Variables: ///////////////////////////////////////////////////////////////////////////////////////////
#define MIDICMD_NOTEON 0x90 // MIDI command (Note On, Channel 0)
int sensorPin = 2; // select the input pin for the LDR
int ledPin = 13; // select the control pin for the LED
int val = 0; // variable to store the value coming from the sensor

void SendMIDI(char cmd, char data1, char data2)
{
Serial.print(cmd);
Serial.print(data1);
Serial.print(data2);
}

void setup()
{
// Set MIDI baud rate:
Serial.begin(31250);
}

void loop() {

int val = analogRead(sensorPin);
if (val >= 600){

SendMIDI(MIDICMD_NOTEON, 60, 127); // NOTE_ON
digitalWrite(ledPin, LOW); // turn the led off
delay(250);
}
else {

SendMIDI(MIDICMD_NOTEON, 60, 0); // NOTE OFF
digitalWrite(ledPin, HIGH); // turn the led on
delay(250);

}
}

first oaf all you need :

pinMode(ledPin, OUTPUT) ;

in the setup section.

Every time you go around in your loop you send a new note on if val >= 600 is that really what you want ?

I'm not exactly a MIDI specialist :slight_smile: but i think you need to implement some kind of state maintenance to keep track of the state of the note on or off, so you only send new note on commands if the note is off otherwise i think you will continously retrigger the note if val >= 600

Hej MikMo, thanks for your quick reply.
ops, yes, i should declare the led Pin as output. :slight_smile:

Every time you go around in your loop you send a new note on if val >= 600 is that really what you want ?
Once the val is >=600 I don't want to send a new command until the value is <600. Yeah. I believe your conclusion is correct. It's now just re triggering.

Anyone who could show me how to hold the note on-state until the value is less than 600? Or... know some other topic or code snippet i should check out. Has this something to do with a for loop, could i write a stop running until value is <600 in some way?

But then I would like to use the the code for several LDR inputs later. Maybe writing a wait would also affect the other inputs.

(Gosh, I tried to desolder some buttons from an old alarmclock. Really got an instataneous headach from the fumes. Also tried grabbing the soldering iron on the very hot are instead of the handle. Thinks it's time to call it a day maybe.)

cheers!

after this bit in your code
digitalWrite(ledPin, LOW); // turn the led off
delay(250);

put
while(val > 600) {val = analogRead(sensorPin);}

this will hold it until val drops below 600. Next time through the loop it will turn the note off and not turn it on again until the val reaches 600.

However while that will work it will keep sending lots of MIDI NOTE OFF commands so put

while(val < 600) {val = analogRead(sensorPin);}

after your note and led off commands.