I'm neither a math or a code genius and I'm most certainly missing something but how about this (compiles but not tested):/Henrik.Code:Temp VAR BYTE AssistPot VAR BYTE RegenPot VAR BYTE CmdPwrHigh VAR BYTE PwrLevel VAR BYTE CmdPwrIn VAR GPIO.0 ' 50 counts for 50% dutycyle @2kHz input and 8MHz oscillator PULSIN CmdPwrIn, 1, CmdPwrHigh ' Read inputs (8bit mode!) and convert values to +/-40 in two's complement. ADCIN 0, Temp AssistPot = (Temp ** 20480) - 40 ADCIN 1, Temp RegenPot = (Temp ** 20480) - 40 '------------------------------------------------------------------------------- IF CmdPwrHigh > 50 THEN ' Assist pot in play PwrLevel = CmdPwrHigh + AssistPot ' No risk of overflowing a BYTE here If PwrLevel > 90 THEN PwrLevel = 90 ' But we need to clamp at 90% for top end... If PwrLevel < 51 THEN PwrLevel = 51 ' ...and at 51% for the bottom end. ENDIF '------------------------------------------------------------------------------- If CmdPwrHigh < 50 THEN ' Regen pot in play PwrLevel = CmdPwrHigh - RegenPot If PwrLevel < 10 THEN PwrLevel = 10 ' Clamp at 10 for the low end IF PwrLevel.7 THEN PwrLevel = 10 ' Clamp at 10% if we went negative If PwrLevel > 49 THEN PwrLevel = 49 ' Clamp at 49% for the top end. ENDIF '-------------------------------------------------------------------------------
Bookmarks