I’m having a problem with a variable of type float which I hope someone can help with.
Basically I have a pot which is my pitch fader. It sends MIDI values 0-127. The centre point of the pot is 0% pitch, and sends a MIDI value of 63. My pitch range is ±6.5%
I am trying to write a C function which will calculate the pitch and display it on the LCD.
My formula for working out the pitch is:
- Work out the equivilant pitch value for each pot step or movement:
eachIncrementValue = totalPitch / totalPossibleValues
eachIncrementValue = (6.5 X 2)/ 127
eachIncrementValue = 0.10236220472440944881889763779528 - Then work out the pitch:
pitch = 0.0 + ( eachIncrementValue * (pin_value - zeroPosition) )
pitch = 0.0 + ( 0.10236220472440944881889763779528 * (pin_value - 63) )
pitch is obviously dependant on pin_value, but if it were 12 pitch = -5.22… If pin_value were 79, pitch = 1.63…
So far so good. Now, I want to display the value on the LCD, and this is where I’m getting a bit stuck. I want to only display the value to 1 decimal place (eg. 1.6323534 should be displayed as 1.6; and 1.0 should be displayed as 1.0), and I want to display the + or - sign. As I don’t know C very well, like how to round down a float, or how the MIOS_LCD_Print* routines would handle a float and the polarity sign I decided to do it myself:
unsigned char totalPitch = 13;
unsigned char totalPossibleValues = 127;
unsigned char zeroPosition = 63;
float eachIncrementValue;
float pitch;
unsigned char wholeNum;
float remainder;
eachIncrementValue = (float)totalPitch / totalPossibleValues;
pitch = 0.0 + ( eachIncrementValue * (pin_value - zeroPosition) );
// position the cursor on the correct display
if (pin==0) {
MIOS_LCD_CursorSet(0x46);
} else {
MIOS_LCD_CursorSet(0xc6);
}
// if the pitch is negative then we need to display the minus sign, then multiply the value by -1 so we get the positive value
if (pitch < 0.0) {
MIOS_LCD_PrintChar('-');
pitch = pitch * -1;
} else
if (pitch == 0.0) {
MIOS_LCD_PrintChar(' ');
} else {
MIOS_LCD_PrintChar('+');
}
wholeNum = pitch; // this will explicitally truncate the fractional portion of the pitch - so we are left with the whole number
MIOS_LCD_PrintBCD1((unsigned char)wholeNum);
MIOS_LCD_PrintChar('.');
remainder = (pitch - wholeNum) * 10;
MIOS_LCD_PrintBCD1((unsigned char)remainder);
It all works with the exception of the line that attempts to convert a negative value to a positive:
// if the pitch is negative then we need to display the minus sign, then multiply the value by -1 so we get the positive value
if (pitch < 0.0) {
MIOS_LCD_PrintChar('-');
pitch = pitch * -1;
} else
The code goes into the if block correctly, and the - sign is correctly displayed. However, the pitch = pitch * -1; appears to corrupt the value, and what I get printed on screen is -F.F for any negative value !! I have tried
pitch = pitch * -1;
pitch = pitch * (-1);
pitch = pitch - pitch - pitch;
None of these work; and so all I can assume is there is something odd about a C float ??
Any ideas anyone ?
Cheers
JD