Please tell me why my IF statement is not working correctly

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
}

I'm not sure why you said the first if statement is being run, but the second two run because you have given them a true value.

if (vala >=100 && vala <300 && prevnote || 60)

what are you trying to accomplish? Any non zero value is true, so this will always be true.

prevnote  || 60

Should be

prevnote  ==  60

and same for the 65.

The first if could never have executed though.


Rob

What I am doing is trying to trigger midi notes using a variable analog input. The values Im getting from the sensor on analog(0) are between 100 and 450. any values outside of this range are inaccurate readings...
Because its MIDI I need to check the value of analog(0) which is assigned to variable "vala". Then if the signal is within a range (for example: vala >=100 && vala <300) then it goes and triggers a midi note. My original program did this but kept triggering midi notes 1000x per second every time the program looped. What I need is for it to trigger MIDI note ON and turn off the previous note. If the input signal stays the same for the next loop around, then I need it to do nothing (just leave the note on), if on the next loop the note changes, then it should turn off the previous note and turn on the new note. If a reading comes in outside of the range (100-450) then it should shut off the note and do nothing else.

I figured Id read analog(0) (vala=analog(0))
then I would check to see if the value was in a specific part of the range (performed in the IF statement)
If TRUE, then I would send a midi signal to turn off the previous note ( playmidinote(prevnote, 0))
and send a second MIDI signal tu turn on the current note (playmidinote(note, 127)
then I would set prevnote = note for the next loop around.
The reason for the prevnote || 60 in the IF statement is that if the note played in the previous loop had a value of 60, then the line prevnote = note would make prevnote=60. In the IF statement the prevnote ||60 would make the IF statement false so it would skip those procedures...
in othe words if the sensor says I am in the range for NOTE 60 but the previous note played was already 60 then dont send the play signal again, skip the IF statements procedures...dont do anything....just let the note ring...
Now if the sensor is in the NOTE 60 range but the previous note was something else, then go ahead and turn of the previous note then play the new note.

What I dont get is why it keeps looking at both IF statements as true???

Hope this makes sense... I really appreciate the help... Ive been stumped for 3 days now and its driving me crazy!!!

what are you trying to accomplish? Any non zero value is true, so this will always be true.

To me it seems like it isnt automatically a TRUE statement...

if (vala >=100 && vala <300 && prevnote || 60)

Right now I have vala set as a constant value of 150 so the first two conditions would be true in this case, but prevote starts out as 50 so that would be true for the first loop but then when the line prevnote=note is read prevnote becomes 60 and in the next loop even though vala is still in the range, prevnote ||60 would make the IF statement FALSE and those procedures would be skipped.... Right???

"I figured Id read analog(0) (vala=analog(0))"

where do you do that? I only see this:

int vala = 150;

and nothing that will make vala change value?

Your 2nd question why both if's are true

if (vala >=100 && vala <300 && prevnote || 60)
you have 4 things here:
vala =>100 ,this is true because set vala =150 at the top of loop
vala <300, this is true for same reason
prevnote, True because it is not 0
|| 60 this is true by definition " || (logical or) True if either operand is true"
you set prevnote to 50, and 50 OR 60 are both true (i.e. not 0)

So that's the first if.

The 2nd if,
if (vala >=300 && vala <450 && prevnote || 65)
four parts again:
vala >=300, ok not true
vala <450, is true
prevnote, true because it is not 0,
|| 65 - True because it is not 0. So the IF is treated as true.

So that's the 2nd if.

I think it will help if you read the A0 to start
vala = analog.Read(A0);

then make your If's more workable with ()s
if ( (vala >=100) && (vala <300) && (prevnote == 60) ) { // or whateve checking you are doing with prevnote

if ( (vala >=300) && (vala <450) && (prevnote == 65) ){

Fix that, then revisit the rest of your question.

That fixed it. My stupid mistake was that i was using ...&& prevnote || 60
what it should have been was ... && prevnote != 60

I confused || with <>

Thanks for the help...

I LOVE THIS FORUM... FINALLY I CAN GET A GOOD NIGHT'S SLEEP!!!