# Algorithm for value range mapping

My question perhaps belongs to general ability test or something like that. It is not directly related to Embedded devices.

I have been given a variable resister which can rotate infinitely and is of 10kΩ. ADC is set for value range 0 to 1023 sampling. By which I've to control light luminosity/intensity.

The ADC value range is divided into 32 steps for 1024/32 levels of brightness. The issue is the pot has to be rotated 16 times to get to extremities. Which is very harassing. Now its time when I can't change the pot hardware but the firmware.

I'll choose a range window within the [0 1023], say of 32 steps. Lets ignore the ADC noise for now.

Then we have 32 steps in that 32 step windows.

If I start from 0, the intensity will be increasing till 32. The 32 is max intensity possible. Then if I rotate more, the intensity should remain maximum at 32 all the way till 1023.

And as soon as it is started to decrease from 1023, I will have decrease in intensity till ( 1023-32 ) and will remain at 0 intensity all the way till 0.

if I rotate back from any place it should follow the above explained moving window rule. How to start with it?

I did as follows:

 void processADC_data()
{

// If Noise
/*

*/

if (IsInceasing)
{

// Extrimity has to be handled
if (!HasPerformedIncreased)
{
HasPerformedIncreased = true;
HasPerformedDecrease = false;
}
else if (HasPerformedIncreased)
{
if(IsCrossingLimit)
{
IsCrossingLimit = false;
}
else
{
}
}

}
else
{
// Extrimity has to be handled
if (!HasPerformedDecrease)
{
HasPerformedDecrease = true;
HasPerformedIncreased = false;
}
else if (HasPerformedDecrease)
{
if(IsCrossingLimit)
{
IsCrossingLimit = false;
}
else
{
}
}

}

}


Thanks

in pseudocode

// initialization
brightness = 0

// must use towards-0 division!
// divide-by-2 because each 1024/16 turn must give 32 steps
delta = ( new - previous ) / 2
if( delta > 0 ):
brightness = min( brightness + delta, maximum )
previous = new
if( delta < 0 ):
brightness = max( brightness - delta, minimum )
previous = new

• I think it worked: short delta = ( short ) ( ( *adc_value - adc_old_val ) * 10 ); if(delta > 0 ) brightness = (short)(min((brightness + delta), 1023)); if(delta < 0 ) brightness = (short)(max( 0, (brightness + delta))); Where multiplier 10 can be used to reduce the turns. Thanks. Oct 3 '12 at 7:14

Assuming that your pot is linear, you could measure the rate of change. Then, increment (or decrement) your brightness level as some software function of the rate of change.

This way, you can make it so that your brightness more with faster turning.