Help with MIDI lib code

I am using this MIDI lib
http://www.arduino.cc/playground/Main/MIDILibrary
to get photocells to trigger MIDI notes when there is no light. The code I have come up with works OK. The only thing for now that I have no clue how to implement is when I block the light I want to sequence on notes keep looping until I let the light get to the cell again. Now it plays just once.

#include <MIDI.h>
byte note = 0;
byte val = 0;


int THRESHOLD1 = val+1;

int AnalogValue = 1; 
  
void setup() {

 MIDI.begin(14); 
  MIDI.sendProgramChange (18,14);
}

void loop() {
 
  val = analogRead(0);
  if (val <= THRESHOLD1) {
    MIDI.sendNoteOn(54,127,14);
  delay(300);
  if (val = THRESHOLD1)
    MIDI.sendNoteOff(54,127,14);
     }
       if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(66,127,14);
  delay(300);
  if (val = THRESHOLD1)
    MIDI.sendNoteOff(66,127,14);
     
       }
        if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(68,127,14);
  delay(500);
  if (val = THRESHOLD1)
    MIDI.sendNoteOff(68,127,14);
     
       }
  }
if (val = THRESHOLD1)

There is a big difference between = (the assignment operator) and == (the equality operator).

As you can tell I am very new to this.
So I corrected this thinking that I want to equal the value(replaced = with ==). Strange thing is that now the noteOFF will not be run correctly - often notes stay on.
However this does not answer the question about looping.

I won't even try to guess whether you fixed all the places where = should have been ==. Re-post your code.

here

#include <MIDI.h>
byte note = 0;
byte val = 0;


int THRESHOLD1 = val+1;

int AnalogValue = 1; 
  
void setup() {

  Serial.begin(31250);   // set MIDI baud rate
  MIDI.sendProgramChange (18,14);
}

void loop() {
 
  val = analogRead(0);
  if (val <= THRESHOLD1) {
    MIDI.sendNoteOn(54,127,14);
  delay(300);
  if (val == THRESHOLD1)
    MIDI.sendNoteOff(54,127,14);
     }
       if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(66,127,14);
  delay(300);
  if (val == THRESHOLD1)
    MIDI.sendNoteOff(66,127,14);
     
       }
        if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(68,127,14);
  delay(500);
  if (val == THRESHOLD1)
    MIDI.sendNoteOff(68,127,14);
     
       }
  }

What is the range of values that the sensors returns? THRESHOLD1 is set to 1. That's pretty low.

As I read this, you read one sensor value.

If val is 0 or 1, you send a note on command, and wait 0.3 seconds. If val is 0, you send a note off command.
If val is 0 or 1, you send a note on command, and wait 0.3 seconds. If val is 0, you send a note off command.
If val is 0 or 1, you send a note on command, and wait 0.5 seconds. If val is 0, you send a note off command.

Then, loop ends, and is called again, where you read the sensor, and start over.

Thanks!
Your answer explains a lot. Mostly that have created something else that I wanted.
What I wanted was:
read ambient light value and add 1(or more) and make it threshold.
If val is less than threshold, send a note on command, and wait 0.3 seconds. If val is threshold(or more), you send a note off command.
But I want to keep the sequence 1-2-3 keep playing while the value is less than the threshold.
I assume the reading of the photocell is never constant(it could be the usual 0-1023) unless I can place this into windowless room. I have not get the actual readings though.

maybe this makes more sense, but no loop sequence still

#include <MIDI.h>
byte note = 0;
byte val;


int THRESHOLD1;

 
  
void setup() {

  Serial.begin(31250);   
  MIDI.sendProgramChange (18,1);
}

void loop() {
 THRESHOLD1==val+1;
  val = analogRead(0);
  if (val <= THRESHOLD1) {
    MIDI.sendNoteOn(54,127,1);
  delay(300);
  if (val >= THRESHOLD1)
    MIDI.sendNoteOff(54,127,1);
     }
       if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(66,127,1);
  delay(300);
  if (val >= THRESHOLD1)
    MIDI.sendNoteOff(66,127,1);
     
       }
        if (val <= THRESHOLD1) {
       MIDI.sendNoteOn(68,127,1);
  delay(500);
  if (val>= THRESHOLD1)
    MIDI.sendNoteOff(68,127,1);
     
       }
  }
THRESHOLD1==val+1;

==?

You really need to read up on the differences between = and ==.

I am not going to argue about the importance or relevance of = and == with this context. The hints about it just got me confused.
So I went through the code checking what I wanted it to do and ended with this

#include <MIDI.h>
byte note = 0;
int valPin = 0; //analog pin 0
int val;

int THRESHOLD = 90; // max reading from valPin -30

void setup() {

  Serial.begin(31250);
}

void loop() {
 
  val = analogRead(valPin);
  if (val <= THRESHOLD) { 
  MIDI.sendNoteOn(54,127,6);
  delay(300);
  MIDI.sendNoteOn(58,127,6);
  delay(300);
  MIDI.sendNoteOn(66,127,6);
  delay(500);  
      }
}

I was hoping to periodically update the THRESHOLD (every time when reading from valPin reaches over THRESHOLD again) but I give up for a moment.

I am not going to argue about the importance or relevance of = and == with this context. The hints about it just got me confused.

The = is the assignment operator. The value on the right side is copied into the variable on the left side. The assignment operator returns the value assigned.

The -== is the equality operator. It determines whether the value on the left equals the value on the right. If they are, it returns true. Otherwise, it returns false.

There is a world of difference between them, It pays to understand this difference. There is no need to argue over a fact.

I was hoping to periodically update the THRESHOLD (every time when reading from valPin reaches over THRESHOLD again) but I give up for a moment.

I sincerely regret that.

Please explain what you are trying to accomplish, and we'll help you accomplish it.

Thank you for this clear explanation. I understood the = and == about the same way, but was not verbalizing it so well.
To illustrate what I am trying to do look this video

I have here Arduino just reading analog inputs from photocells and sending the readings to the computer. There I use Isadora app to calibrate and massage the values and output MIDI notes. Notes are played with FM softsynth. However I had difficulties to rely on the computer for working well weeks on the row. I had to go to fix the system few times. Now when this piece goes to other town I want to make this work without the computer so anybody could just plug it in and it works.
There I have a photocell under each glass bowl. By waving hands over the glass and blocking the light person could play short MIDI sequences. I placed the lightbulb over the table to have more consistent light source, but the ambient light was a huge issue still. It helped when I put the cells deeper into the drilled hole so the view angle got narrower.
So ideally I am trying to make Arduino to:
take a reading from analog pin
make this into threshold by reducing the value 20%,
when analog reading falls under the threshold trigger note sequence and keep it looping until reading gets over the threshold,
and take another reading and make a new threshold.
What I have now is a crude sketch where I need to calibrate the cells first with another sketch to calculate the threshold and go from there. This will be fine if I have just the lightbulb for the light source and ambient light is pretty constant. But eventually I need to come up with the system that works everywhere.
So I am working on it still. It is not so easy to give up.

int THRESHOLD = 90; // max reading from valPin -30

This comment does not match the code. The maximum value that can be read from an analog pin is 1023, not 120.

That your particular device never returns a reading above 120 might be true. If that is the case, that should be reflected in the comments, instead.

For setting THRESHOLD under varying conditions of ambient light, perhaps what you need to do is to set up a photocell that is not interacted with, that measures ambient light, and use the value from that sensor to set THRESHOLD (which, by convention, should not be all upper case unless #defined) which then is used to adjust the sensitivity of the other sensors (the ones that ARE interacted with).

Right, the analog input I name valPin returns 120 as max.
As to use to adjust the sensitivity of the other sensors I have not find the strategy that works always. First all sensors have different max readings.
What do you think of this: if I make the master threshold by multiplying the control pin input by 0.3
and use this to make every threshold for each cell by pin input - master threshold= new threshold.
Then if that cell input will fall under this new threshold note will be sent.
If get over the new threshold note off will be sent.

There is an other issue still.
I want to run all these 4 cells triggering sequence of notes at the same time. Using delay() will not allow anything else to happen while in delay.
I looked at the Blink Without Delay example. While I understand what is done there I I don't get it how to use it in my case.

I think that you'll need to experiment with the threshold multiplier from the control pin to get a good value, but, otherwise, I think it is a good approach.

As for using millis(), you'll need to record several times - when the last note was sent for each cell - several booleans - do we need to send a note for each cell and do we need to send a note off for each cell - and several ints - which note to send next for each cell.

Then, in loop, you do something like this:

void loop()
{
   for(byte c=0; c<CELLCOUNT; c++)
   {
      // Read the sensor
      // Set the note on and off flags

      // If note off is to be sent, send it
      // Else
      //    see if it's time to send another note
      //    (does now - then equal or exceed interval?) 
      //    Send next note & set time note was sent
   }
}

Could something like this be possible:
there is a sequence that runs all the time (like in step sequencer) with 0 velocity in the background,
when sensor activates it will rise velocity to audible level(127) and sequence will be heard from the point it was when velocity changed and will stay so until sensor becomes passive again.
this is for each sensor separate.

Beats me. I know nothing about MIDI. Sounds reasonable, though.

The only fly in the ointment that I see is that the Arduino doesn't do "background processing". It is not a multi-threaded architecture.

But, this can be simulated by checking the sensors, adjusting the velocity (strange term for volume) and then seeing if the next note needs to be sent, for each cell. The loop() function is called millions of times per second, so missing input/output opportunities won't result in any noticeable issues.

I am running out of time to experiment and study. I shall continue however I would like to solve this delay replacement by millis() ASAP.

Here is what I came up with and obviously it does not do the trick

#include <MIDI.h>
byte note = 0;
int valPin = 0; //analog pin 0
int s; // value to trigger note
int a = 150; // calculated threshold from actual reading -20
// following is pasted from millis sketch
unsigned long currentTime  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime     = 0; //no time has passed yet
int           wait         = 0; //no need to wait, nothing has happened yet






void setup() {

  Serial.begin(31250);
}

void loop()
{
  pastTime    = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
  currentTime = millis();    //currentTime is now the current time (again).
  
  unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed

  if(timePassed >= wait) //part1 of the state engine
  {
   
 
  s = analogRead(valPin);
 
 
  if (s <= a-33) { //set new threshold when notes will be played
  MIDI.sendNoteOn(54,127,6);
  wait = 300; //attempt to delay between notes played 0.3s
  MIDI.sendNoteOn(58,127,6);
  wait = 300;
  MIDI.sendNoteOn(66,127,6);
  wait = 500; 
  MIDI.sendNoteOff(54,0,6);
  MIDI.sendNoteOff(58,0,6);
  MIDI.sendNoteOff(66,0,6);
  }
  }
      }

You should be doing:-
wait = 300 + millis();
so that you have an absolute time of when you want things off.

Then at the start of every loop:-
if(timePassed >= wait) { // do the turning off stuff }

You do not mean anything like this, do you. This does not work as much I can tell - there is no delay what-so-ever. All notes are played at once.
I have been reading about the millis() and I understand what the idea is, but I do not find a good example how it is used for delay.

#include <MIDI.h>
byte note = 0;
int valPin = 0; //analog pin 0
int s; // value to trigger note
int a = 212; // calculated threshold from actual reading -20
// following is pasted from millis sketch
unsigned long currentTime  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime     = 0; //no time has passed yet
int           wait         = 0; //no need to wait, nothing has happened yet






void setup() {

  Serial.begin(31250);
}

void loop()
{
  pastTime    = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
  currentTime = millis();    //currentTime is now the current time (again).
  
  unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed

  s = analogRead(valPin);
 
 
  if (s <= a-33) { //set new threshold when notes will be played
  MIDI.sendNoteOn(54,127,6);
  if(timePassed >= wait) 
  wait = 300 + millis(); //attempt to delay between notes played 0.3s
  MIDI.sendNoteOn(58,127,6);
  if(timePassed >= wait)
  wait = 300 + millis();
  MIDI.sendNoteOn(66,127,6);
  if(timePassed >= wait)
  wait = 500 + millis(); 
  MIDI.sendNoteOff(54,0,6);
  MIDI.sendNoteOff(58,0,6);
  MIDI.sendNoteOff(66,0,6);
  }
  }