I new to C programming but thought I had my IF statements down from previous BASIC experience....aparently not so..
I wrote a program that interfaces with midi to play notes based on sensor readings. I had problems with it and it boils down to the program running through my if statements even though they might return a FALSE reading.
I stripped down my code to the bare bones and converted all midi send statements to serial.prints so that I could troubleshoot. If you copy this code and run it, you will see on the serial monitor that all comands withing every IF statement is executed regardless of what the conditional variable (vala) equals. I set vala to equal 150 so that only one of my 3 IF statements would be true but still the serial monitor shows all 3 IF statements are being executed.
Hope you can help
int note = 50; // initial note value (50 is an unused note value)
int prevnote = 50; // initial prevnote value (50 is an unused note value)
void setup()
{
Serial.begin(9600); //Initialize serial communication
}
void loop()
{
int vala = 150;
if (vala < 100 || vala >=450)
{
Serial.println( "VALA IS OUT OF RANGE");
Serial.println( "Executing set prevnote volume to 0");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(prevnote, 0);
Serial.println( "Executing set note volume to 0");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(note , 0);
Serial.print( "prevnote: ");
Serial.print( prevnote);
Serial.print( ", NOTE: ");
Serial.print(note);
prevnote = 50;
Serial.print( ", new prevnote: ");
Serial.println(prevnote);
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
}
if (vala >=100 && vala <300 && prevnote || 60)
{
note = 60;
Serial.println( "NOTE 60 LOOP");
Serial.println( "Executing set prevnote volume to 0");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(prevnote , 0);
Serial.println( "executing set note volume to 127 ");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(note , 127);
Serial.print( "prevnote: ");
Serial.print( prevnote);
Serial.print( ", NOTE: ");
Serial.print(note);
prevnote = note;
Serial.print( ", new prevnote: ");
Serial.println(prevnote);
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
}
if (vala >=300 && vala <450 && prevnote || 65)
{
note = 65;
Serial.println( "NOTE 65 LOOP: ");
Serial.println( "Executing set prevnote volume to 0");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(prevnote , 0);
Serial.println( "executing set note volume to 127 ");
Serial.print("vala: ");
Serial.println( vala);
playMidiNote(note , 127);
prevnote = note;
Serial.print( "prevnote: ");
Serial.print( prevnote);
Serial.print( ", NOTE: ");
Serial.print(note);
prevnote = note;
Serial.print( ", new prevnote: ");
Serial.println(prevnote);
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
}
}
void playMidiNote(byte note, int velocity)
{
Serial.print("0xC2: ");
Serial.print(0xC2); //code to call change of sound
Serial.print(", 41: ");
Serial.println(41); //code to set new sound to sound #41
Serial.print("0xB2: ");
Serial.print(0xB2); //code to call up reverb change (pt1)
Serial.print(", 0x5B: ");
Serial.print(0x5B);//code to call up reverb change (pt2)
Serial.print(", 80: ");
Serial.println(80);// code to set reverb level to 80
Serial.print("0x92: ");
Serial.print(0x92);// code to call up note change
Serial.print(", NOTE: ");
Serial.print(note);// specifies which note
Serial.print(", VEL: ");
Serial.println(velocity); //sets volume for the note 0-127
delay(2000); //ADDED DELAY FOR DEBUGGING TO SLOW DOWN SERIAL MONITOR
}