FAQ & Terms of UseFAQ & Terms Of Use   Wiggler RadioMW Radio   Muff Wiggler TwitterTwitter   Support the site @ PatreonPatreon 
 SearchSearch   RegisterSign up   Log inLog in 

Constraining the input on Analog In knobs
MUFF WIGGLER Forum Index -> nw2s  
Author Constraining the input on Analog In knobs
With the following code I have managed to
1. read the input on Analog In 3 (4 on the hardware)
2. Flip the knob so that CCW is 0 and CW is max
3. Constrain the value from being 0 to thousands to being 0 to 500.

int inputFlipped = ::analogRead(DUE_IN_A03);
int input = 500 - (constrain(inputFlipped, 0, 500));

My trouble is that when turning the knob and reading the result, it's 0 from CCW up to 3 o'clock where it starts to rise quickly and up to 500 at full CW.

When I tried this with 0 to 11 it didn't work at all. Just jumped from 0 to 11 at once, probably since all the other values got crammed in between the two towards the end of the knob turning.

How would you guys do a thing like this? Ideally I'd love to have an even rising of the numbers, and be able to set values 0,1,2,3,4,5,6,7,8,9,10,11 for the last example there.

Looking at the documentation for constrain(), it's not doing any scaling where it sounds like what you want is to scale [4095, 0] to [0, 500]

What you're seeing is the result of that... the only values the input pot produces that get through the constrain() method are when they are 0-500, which is a pretty small range in the movement of the pot.

You could do it in one line of code, but I'll write it out longhand just to be clear:


// Get the raw value at input
int inputflipped = ::analogRead(DUE_IN_A03);

// Invert it
int inputval = 4095 - inputflipped;

// Constrain it to [0, 4095] in case the values were off a bit
int inputconstrained = constrain(inputflipped, 0, 4095);

// Scale it from 12 bits (4096 values) to 9 bits (512 values)
int inputscaled = inputval >> 3;

If you really want 0-500, you can constrain again, but whenever you can use even powers of two instead of arbitrary powers/multiples of 10, you can do multiplication and division much faster on a microcontroller.

Thank you Scott!
And thank you for getting back to me so quickly!

This solution worked best for me:

int setNoteProgramFlipped = ::analogRead(DUE_IN_A01); // Around 2011 - 0
int setNoteProgram = 2020 - setNoteProgramFlipped; // 0 - 2011
int setNoteProgramConstrained = constrain(setNoteProgram, 0, 2047); // 0 - 2047
int noteProgramScaled = setNoteProgramConstrained >> 7; // 1 - 16
int noteProgram = constrain(noteProgramScaled, 0, 15); // force it to be between 0 and 15

Not sure why the analog in read 2000-2011 something but using 2020 worked better.
Yes, you're right. The difference being the full range -5 to +5 that's available for bipolar signals, but the pots only operate over half that range: 0V to 5V, then there's always the error in gain and offset that makes it 2011 instead of 2048.

MUFF WIGGLER Forum Index -> nw2s  
Page 1 of 1
Powered by phpBB © phpBB Group