As was mentioned, on an Arduino you could use the map command. Which is a very simple function:
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Not sure what you are using for the code base, but the XBee version of the DIY code(I think both BAP and Arduino) has stuff in place to handle this for you. That is the is a mode where you move all of the joysticks to their Minimum/maximum values (actually I recommend only moving straight up/down and Left/Right for this as you may get slightly higher values in diagonals, but you probably don’t want these values. And for most of the joysticks you release it to allow it to get back to the center position (which in your case you hope is near 512). My code then maps everything below the center point to the range 0-127 and everything at the center point (plus or minus slop factor) to 128 and everything above to 129-255.
The code/should be cleaned up, but one of the calculations looks like:
sums(0) = sums(0) - buffer0(index) ; subtract off old value
adin 3, buffer0(index) ;
sums(0) = sums(0) + buffer0(index) ; Add on new value
if sums(0) >= AtoDMidT8(0) then
;(v-510)*128/126 so 636->128
bPacket(2) = (128 + ((sums(0)-AtoDMidT8(0))*128)/(AtoDMaxT8(0)-AtoDMidT8(0))) max 255
elseif sums(0) <= AtodMinT8(0)
bPacket(2) = 0
else ; (v-510)*128/118 so 392-> -128 Note converted 0-127
bPacket(2) = ((sums(0)-AtoDMinT8(0))*128)/(AtoDMidT8(0) - AtoDMinT8(0))
endif
;bPacket(2) = ((((((sums(0) + SumOffsets(0)) min (AToDMins(0)*8)) / 8) - AToDMins(0)) * 256) / AToDRanges(0)) max 255
The Arduino version uses the map function and also tables to know which joysticks/sliders/pots have a center value to decide how to map. Also I do the code in a loop instead of exploding it out for each item.
[code]void ReadJoysticks(boolean fCheckandTrans) { // Initialize the joysticks.
byte i;
// Calculate which index of our raw array we should now reuse.
g_iRaw = (g_iRaw+1) & 0x7;
// Now lets read in the next analog reading and smooth the values
for (i=0; i<NUMANALOGS; i++) {
g_awRawSums* -= g_aawRawAnalog[g_iRaw]*; // remove the value we overwrite from sum
g_aawRawAnalog[g_iRaw]* = analogRead(i);
g_awRawSums* += g_aawRawAnalog[g_iRaw]*; // Add the new value in
// Lets calculate our calibrated values from 0-255 whith 128 as center point
if (g_awRawSums* <= g_awAnalogMins*)
diyp.ab[g_aiRawToDIYP[i]] = 0; // Take care of out of range low
else if (g_awRawSums* >= g_awAnalogMaxs*)
diyp.ab[g_aiRawToDIYP[i]] = 255; // out of range high
else if (!g_afAnalogUseMids*)
diyp.ab[g_aiRawToDIYP[i]] = map(g_awRawSums*, g_awAnalogMins*, g_awAnalogMaxs*, 0, 255);
else if (g_awRawSums* <= g_awAnalogMids*)
diyp.ab[g_aiRawToDIYP[i]] = map(g_awRawSums*, g_awAnalogMins*, g_awAnalogMids*, 0, 128);
else
diyp.ab[g_aiRawToDIYP[i]] = map(g_awRawSums*, g_awAnalogMids*, g_awAnalogMaxs*, 128, 255);
// Try to give the robot as quick a response as possible when a request comes in...
if (fCheckandTrans)
CheckAndTransmitDataPacket(&diyp);
}
// Do the same for the Battery voltage
g_wRawBatterySum -= g_awRawBatAnalog[g_iRaw];
g_awRawBatAnalog[g_iRaw] = analogRead(BATTERY_PIN);
g_wRawBatterySum += g_awRawBatAnalog[g_iRaw];
}[/code]
Hope that helps
Kurt*********************