Greetings everyone.
Thanks in advance for reading. Am working on an ever evolving Midi controller seeking advice using the map() function for pot calibration.
Before I added map() to the mix, I was getting readings 0 - 1023 from the pots.
In the following code, the pot on A0 outputs values 1=1021. One of my intended Midi target devices wants to see 0 - 1023. I'm sure I can figure a quick and dirty kludge for that one application however I'd like to get the math right as here lurks a bunch of divides and truncated remainders. Am a newcomer to C and all the C's that follow and math ain't one of my strengths..
Hopefully the code is reasonably self-explanatory.
Have migrated From Mega and have horsepower to spare here so don't mind using machine cycles for the math.
Thanks in advance for reading. Am working on an ever evolving Midi controller seeking advice using the map() function for pot calibration.
Before I added map() to the mix, I was getting readings 0 - 1023 from the pots.
In the following code, the pot on A0 outputs values 1=1021. One of my intended Midi target devices wants to see 0 - 1023. I'm sure I can figure a quick and dirty kludge for that one application however I'd like to get the math right as here lurks a bunch of divides and truncated remainders. Am a newcomer to C and all the C's that follow and math ain't one of my strengths..
Hopefully the code is reasonably self-explanatory.
Code:
// This runs and works, stripped to the guts, displaying output on the Serial Monitor. Board type T3_2, USB type Serial.
// Hook a pot to A0. You can leave A1 floating as is muted See OUTPUT_TYPE
// From Many Knobs Buttons in Teensy examples. Added arrays for per-element Range, Scaling etc.
#include <ResponsiveAnalogRead.h>
const int A_PINS = 2; // number of Analog PINS
const int ANALOG_PINS [A_PINS] = { A0, A1};
const uint16_t CCID [A_PINS] = { 0, 1}; //uint16_t coz we might be sending NRPN#.
const uint16_t CENTER_VALUE [A_PINS] = { 511, 511}; //Useful for Center detent pots were detent spot is not at electrical center.
const uint16_t DETENT_WIDTH [A_PINS] = { 40, 0}; //Allows a dead zone to give us room to climb out of the detent.
const uint16_t INPUT_MINIMUM [A_PINS] = { 0, 0}; //Useful for pots where wiper does not reach track Min.
const uint16_t INPUT_MAXIMUM [A_PINS] = {1023, 1023}; // " " " " " " " " " Max.
const uint16_t OUTPUT_MINIMUM[A_PINS] = { 0, 0}; //Useful if you have a string of equal value resistors and say a 6 pos rotary switch.
const uint16_t OUTPUT_MAXIMUM[A_PINS] = {1023, 127}; //You set OUTPUT_MINIMUM to 0 and OUTPUT_MAXIMUM to 5.
const byte OUTPUT_TYPE [A_PINS] = { 3, 0}; //0=Muted,1=7-bitCC,2=14-bitCC, 3=14-bit NRPN.
const byte OUTPUT_CHANNEL[A_PINS] = { 14, 14};
//
uint16_t data[A_PINS];
uint16_t dataLag[A_PINS]; // when lag and new are not the same then update MIDI CC value
ResponsiveAnalogRead analog[] {
{ANALOG_PINS[0], true},
{ANALOG_PINS[1], true},
};
void setup() { Serial.begin(115200); }
void loop() { getAnalogData(); }
void getAnalogData() { for (int i = 0; i < A_PINS; i++)
{ analog[i].update(); // update the ResponsiveAnalogRead object every loop
if (analog[i].hasChanged())// if the repsonsive value has change then do something with it.
{
data[i] = (analog[i].getValue());
//******************** SUBJECT OF QUERY ****************************
data[i] = constrain(data[i], INPUT_MINIMUM[i], INPUT_MAXIMUM[i]);
{
if ((data[i]) < (CENTER_VALUE [i]) - (DETENT_WIDTH [i]))
{
(data[i]) = map((data[i]), (INPUT_MINIMUM [i]), (CENTER_VALUE [i]) - (DETENT_WIDTH [i]) , (INPUT_MINIMUM [i]) , (CENTER_VALUE [i]));
}
else if ((data[i]) > (CENTER_VALUE [i]) + (DETENT_WIDTH [i]))
{
(data[i]) = map((data[i]), (CENTER_VALUE [i]) + (DETENT_WIDTH [i]), (INPUT_MAXIMUM [i]), (CENTER_VALUE [i]), (INPUT_MAXIMUM [i]));
}
else if ((data[i]) < (CENTER_VALUE [i]) + (DETENT_WIDTH [i]) || (data[i]) >= (CENTER_VALUE [i]) - (DETENT_WIDTH [i]))
{
(data[i]) = (CENTER_VALUE [i]);
}
(data[i]) = map((data[i]), (INPUT_MINIMUM [i]), (INPUT_MAXIMUM [i]), (OUTPUT_MINIMUM [i]), (OUTPUT_MAXIMUM [i]));
}
//************************** END OF SUBJECT ******************************
if (data[i] != dataLag[i])
{
dataLag[i] = data[i];
Serial.print(data[i]); Serial.println();
}}}}
Have migrated From Mega and have horsepower to spare here so don't mind using machine cycles for the math.