Unusual if-else thingy

OK so I have this code:

#include <MIDI.h>
/*
 *  THIS IS THE FINAL VERSION FOR FUNCTIONAL PROTOTYPE
 */
 
int sensorArray[10];
int size = 10;
int prevNoteValue = 0;


void setup() {
  MIDI.begin();
  //Serial.begin(9600);
  //turn legato on, creates the smooth transition between notes
  //MIDI.sendControlChange(68,127,0);
  MIDI.sendProgramChange(41,0);
}

void loop() {
  for(int i = 0; i < size; i++){
    int tempValue = (int)((12343.85 * pow(analogRead(A4),-1.15))+ 0.5);
  sensorArray[i] = tempValue;
  delay(5);
  }
  int sensorValue = quickSortMedian();
  int temp = constrain(sensorValue,12,57);
  int noteValue = map(temp,12,57,0,27);;
  
  Serial.println(noteValue, DEC);
  
  if(noteValue != prevNoteValue){
     prevNoteValue = noteValue;
     Serial.println("w00t");
  }
  else{
    //MIDI.sendControlChange(123,0,0);
   // MIDI.sendNoteOn(60 + noteValue,127,0);
    prevNoteValue = noteValue;
  }
  
  
}


int quickSortMedian(){
  for(int  j = 2 ; j <= size; j++){
    int key = sensorArray[j];
    int i = j-1;
    while(i > 0 && sensorArray[i] > key){
      sensorArray[i+1] = sensorArray[i];
      i = i - 1;
    }
    sensorArray[i + 1] = key;
  }
  
  //Get the median value
  int median = sensorArray[5] + sensorArray[6] / 2;
  return median;
}

However, the if-else statement always fires (i .e. always enters if block), despite prevNoteValue being equal to noteValue, if fact, in the serial monitor, you get:
27
w00t
27
w00t

Any thoughts or help?

pow(analogRead(A4),-

WoW! Two in five minutes!

What's wrong with the A4? In 0019, I think it was, aliases were added for the analog pins, so A4 is analog pin 4.

WProgram.h

http://arduino.cc/en/Reference/PinMode

RTFM

OK, so A4's value is not 4, it's 18, for a Duemilanove or equivalent. Got it.

Right, so what you are saying is that I cannot use aliases?
So I change the code to:

#include <MIDI.h>
/*
 *  THIS IS THE FINAL VERSION FOR FUNCTIONAL PROTOTYPE
 */
 int irSensorPin = 18;
 
int sensorArray[10];
int size = 10;
int prevNoteValue = 0;


void setup() {
  pinMode(irSensorPin,INPUT);
  MIDI.begin();
  //Serial.begin(9600);
  //turn legato on, creates the smooth transition between notes
  //MIDI.sendControlChange(68,127,0);
  MIDI.sendProgramChange(41,0);
}

void loop() {
  for(int i = 0; i < size; i++){
    int tempValue = (int)((12343.85 * pow(analogRead(irSensorPin),-1.15))+ 0.5);
  sensorArray[i] = tempValue;
  delay(5);
  }
  int sensorValue = quickSortMedian();
  int temp = constrain(sensorValue,12,57);
  int noteValue = map(temp,12,57,0,27);;
  
  Serial.println(noteValue, DEC);
  
  if(noteValue != prevNoteValue){
     prevNoteValue = noteValue;
     Serial.println("w00t");
  }
  else{
    //MIDI.sendControlChange(123,0,0);
   // MIDI.sendNoteOn(60 + noteValue,127,0);
    prevNoteValue = noteValue;
  }
  
  
}


int quickSortMedian(){
  for(int  j = 2 ; j <= size; j++){
    int key = sensorArray[j];
    int i = j-1;
    while(i > 0 && sensorArray[i] > key){
      sensorArray[i+1] = sensorArray[i];
      i = i - 1;
    }
    sensorArray[i + 1] = key;
  }
  
  //Get the median value
  int median = sensorArray[5] + sensorArray[6] / 2;
  return median;
}

and it does exactly the same as before.

In addition, it’s reading data from an analogue IR sensor, and I can confirm that the sensor is working correctly (i.e. the value changes, but it ALWAYS fires the if statement, regardless of whether the value has changed or not)

int irSensorPin = 18;

NO! A4 == 18.

Use 4.

As a comment, if two different users does the same error in a window of just five minutes, it might be that the users got it right and the API got it wrong... ?

No , it was me got it wrong. Apologies.

Ok I finally get the idea what your saying, I should simply do analogRead(4).

Which I have now changed, however, it's still firing every time, not just when the previous value != the current one...

This bit of the code is a bit screwed:-

if(noteValue != prevNoteValue){
     prevNoteValue = noteValue;
     Serial.println("w00t");
  }
  else{
    //MIDI.sendControlChange(123,0,0);
   // MIDI.sendNoteOn(60 + noteValue,127,0);
    [glow]prevNoteValue = noteValue;[/glow]
  }

The hi-lighted bit is not needed because it gets done only if prevNoteValue = noteValue so there is no need to do it again.

Still the same,

if the value changes, output is e.g. 15 w00t 16 w00t 17 w00t

if the value staus the same, output is e.g. 27 w00t 27 w00t 27 w00t.

Which of course should not be happening, the w00t should only be appearing if the value changes. Trying to figure out what causes this to happen, the only thing I can think of is there is something wrong the maths to get the values, but since Iive converted the float into an integer, this shouldn't occur.

Print both noteValue and prevNoteValue, instead of just noteValue. There may be a clue there.

I think you need to take a look at your sort function. It does not appear to be quite right.

  for(int  j = 2 ; j <= size; j++){
    int key = sensorArray[j];
    int i = j-1;
    while(i > 0 && sensorArray[i] > key){
      sensorArray[i+1] = sensorArray[i];
      i = i - 1;
    }
    sensorArray[i + 1] = key;

The largest value in j can be 10. When that happens, key is assigned the value in the 11th position in the 10 position array.

Then, i will be set to 9. After the while loop completes, sensorArray[10] will be set. But, there are only 10 positions in the array, numbered 0 to 9.

PaulS, you have saved my bacon!

Turns out it was all caused by that extra array index!

Both of the values were working correctly, however that extra non-existent index was somehow throwing the whole thing off!

Thanks for all your help guys!

//Serial.begin(9600);

I'm pretty sure the baud rate needs to be 31250 for MIDI