#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
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)
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... ?
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.