Problem with a loop

Hi guys,

I've got an issue with my loop below, am I doing something wrong here?

I've created a function void solenoid. When this action is being called using a MIDi note ON i d'like the Solenoid to turn ON and to stay ON till it get the NOTE off status: 127+chan

Right now, it does turn the solenoid ON but does not turn off even though the note is OFF. What am I doing wrong?

thanks for your answers

void solenoid () { // Let the Solenoid HIGH till it receives a MIDI note OFF. 

  while ( true )
  {
   digitalWrite (30, HIGH);

    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      if (incomingByte == 127+chan)
        break; // if incomingByte is equal to NoteOFF (127+Chan)     
    }
  }    
    digitalWrite (30, LOW); 
}

Thanks

You need to post the rest of your code… Buy my guess is that true is always 1…

Actually it probably is being changed to low, but for such a short period of time you don’t notice it…

here it is

#include <VarSpeedServo.h> 


byte type = 0;

byte incomingByte;
byte note;
byte velocity;
int chan = 1;  //specify what MIDI channel we're listing to

int action=2; //0 =note off ; 1=note on ; 2= null
const int lowNote = 60; //what's the first note?  36 is C1 in Ableton


void setup(){
 Serial.begin(38400);
 
//setup our outputs

  pinMode(30, OUTPUT);
  digitalWrite (30, LOW);
  pinMode(31, OUTPUT);
  digitalWrite (31, LOW);

void solenoid1 () { // Let the Solenoid HIGH till it receives a MIDI note OFF. 

  while ( true )
  {
   digitalWrite (30, HIGH);

    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      if (incomingByte == 127+chan)
        break; // if incomingByte is equal to NoteOFF (127+Chan)     
    }
  }    
    digitalWrite (30, LOW); 
}

void loop () {

    
    if(Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();       

   // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 143+chan){ // note on message starting starting
      action=1;
    }
    
    if (incomingByte== 127+chan){ // note off message starting
      action=0;
 
    }
    
    if ( (action==0)/*&&(note==0)*/ ){ // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
    } 
    
    if ( (action==1)/*&&(note==0)*/ ){ // if we received a "note on", we wait for the note (databyte)
      note=incomingByte;
      Solenoid(note);
  
    }
    }
 
}

void Solenoid(byte note){    //  call a function based on the note

    
switch (note){
  case lowNote: //if it's not 60
  solenoid1 ();
  break;  
}
}

Actually it probably is being changed to low, but for such a short period of time you don’t notice it…

How could it be? It goes from High to Low and there’s water going through. Once the function is called, the water is going through without stoping. If the note last 5 second, the water should go through for 5 sec and then be turned off. It stays true!

You have define the solenoid() function within setup(). That's not allowed, which is why your code doesn't compile.

Thanks for your answer,

You have define the solenoid() function within setup(). That's not allowed, which is why your code doesn't compile.

It does compile. I have other codes with tons of functions within setup. it is working. My issue come from the fact that the function is being called but the loop (which I'm not familiar with) isn't stopping when it should be.

It does compile

The code you posted in reply #2 doesn't look to me like it would compile.

Humor us for a test - move this from within setup() to the end of the sketch:

void solenoid1 () { // Let the Solenoid HIGH till it receives a MIDI note OFF. 

  while ( true )
  {
   digitalWrite (30, HIGH);

    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      if (incomingByte == 127+chan)
        break; // if incomingByte is equal to NoteOFF (127+Chan)     
    }
  }

My bad I had forgotten an } when copying the code onto the forum.

#include <VarSpeedServo.h> 


byte type = 0;

byte incomingByte;
byte note;
byte velocity;
int chan = 1;  //specify what MIDI channel we're listing to

int action=2; //0 =note off ; 1=note on ; 2= null
const int lowNote = 60; //what's the first note?  36 is C1 in Ableton


void setup(){
 Serial.begin(38400);
 
//setup our outputs

  pinMode(30, OUTPUT);
  digitalWrite (30, LOW);
  pinMode(31, OUTPUT);
  digitalWrite (31, LOW);
}

void solenoid1 () { // Let the Solenoid HIGH till it receives a MIDI note OFF. 

  while ( true )
  {
   digitalWrite (30, HIGH);

    if (Serial.available() > 0)
    {
      incomingByte = Serial.read();
      if (incomingByte == 127+chan)
        break; // if incomingByte is equal to NoteOFF (127+Chan)     
    }
  }    
    digitalWrite (30, LOW); 
}

void loop () {

    
    if(Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();       

   // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 143+chan){ // note on message starting starting
      action=1;
    }
    
    if (incomingByte== 127+chan){ // note off message starting
      action=0;
 
    }
    
    if ( (action==0)/*&&(note==0)*/ ){ // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
    } 
    
    if ( (action==1)/*&&(note==0)*/ ){ // if we received a "note on", we wait for the note (databyte)
      note=incomingByte;
      Solenoid(note);
  
    }
    }
 
}

void Solenoid(byte note){    //  call a function based on the note

    
switch (note){
  case lowNote: //if it's not 60
  solenoid1 ();
  break;  
}
}

But the loop is still not fully functioning. It is still a mystery to me … should I do a boucle for instead?

I don't see the corrected function
solenoid1 ()
called out in loop(), only
Solenoid(note)

Did you mean to call solenoid1 () instead?

void loop () {

    
    if(Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();       

   // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 143+chan){ // note on message starting starting
      action=1;
    }
    
    if (incomingByte== 127+chan){ // note off message starting
      action=0;
 
    }
    
    if ( (action==0)/*&&(note==0)*/ ){ // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
    } 
    
    if ( (action==1)/*&&(note==0)*/ ){ // if we received a "note on", we wait for the note (databyte)
      note=incomingByte;
      Solenoid(note);
  
    }
    }
 
}

Right.

I think I did a mess with my code. I'm gonna do it differently.

Thanks for you help.

Not a good idea to call one function Solenoid() and another one solenoid1(). Surely you can think up function names that are a little more different and based on what they do.