Page 1 of 1

Multiplexed ShiftX lights

Posted: Fri Apr 03, 2015 6:35 pm
by paulbutcher
This weekend I've been testing a script which multiplexes the ShiftX so that it operates as both shift lights and an error light.

If there's an error condition (temperature too high or oil pressure too low), only the central red light is illuminated. If there's no error condition, it operates as shift lights.

Here's the script:

Code: Select all

FREQUENCY_HZ = 10
RPM_THRESHOLDS = {12000, 12300, 12500}
MAX_OIL_TEMP = 100
MAX_WATER_TEMP = 100
MIN_OIL_PRESSURE_LOW_REVS = 5
MIN_OIL_PRESSURE_HIGH_REVS = 10
LOW_REVS = 3000

setTickRate(FREQUENCY_HZ) 

function lightOn(light, on)
  if on then
    setPwmDutyCycle(light, 0)
  else
    setPwmDutyCycle(light, 100)
  end
end

function updateShiftLights()
  local rpm = getTimerRpm(0)
  lightOn(0, rpm > RPM_THRESHOLDS[1])
  lightOn(1, rpm > RPM_THRESHOLDS[2])
  lightOn(2, rpm > RPM_THRESHOLDS[3])
end

function oilTempError()
  return getAnalog(0) > MAX_OIL_TEMP
end

function waterTempError()
  return getAnalog(1) > MAX_WATER_TEMP
end

function oilPressureError()
  local oilp = getAnalog(2)
  if getTimerRpm&#40;0&#41; < LOW_REVS then
    return oilp < MIN_OIL_PRESSURE_LOW_REVS
  else
    return oilp < MIN_OIL_PRESSURE_HIGH_REVS
  end
end

function errorCondition&#40;&#41;
  return oilTempError&#40;&#41; or waterTempError&#40;&#41; or oilPressureError&#40;&#41;
end

function displayError&#40;&#41;
  lightOn&#40;0, false&#41;
  lightOn&#40;1, false&#41;
  lightOn&#40;2, true&#41;
end

function onTick&#40;&#41;
  if getGpsSpeed&#40;&#41; > 10 then
    startLogging&#40;&#41;
  end

  if errorCondition&#40;&#41; then
    displayError&#40;&#41;
  else
    updateShiftLights&#40;&#41;
  end
end

Posted: Tue Apr 14, 2015 7:03 pm
by brentp
This is great. mind if we put it in the wiki?

Posted: Wed Apr 15, 2015 3:20 pm
by paulbutcher
brentp wrote:This is great. mind if we put it in the wiki?
Of course not - feel free!