Controller - ntb001/rear-fog GitHub Wiki

Rear Fog Controller

The controller needs to listen for the switch signal and activate the light and indicator if the headlights are on. Further switch input should toggle the rear fog off and on. The rear fog should turn off with the headlights and not come back on until a new switch input. The indicator should dim and brighten with the rest of the IP from manual and automatic (ambient light sensor) inputs.

Inputs

  • Headlight state
  • Dimmer value
  • Switch state

Outputs

  • IP indicator LED
  • Relay control

Wiring the Arduino

Schematic

The CAN bus shield attaches to the Arduino. CAN high and low connect to the shield. The shield leaves pins 4-8 unused, among others (CAN-BUS Wiki).

To dim the indicator, we need to use one of the Arduino's PWM output pins: 3, 5, 6, 9, 10, 11 (Arduino Pin Mapping). Of those, only 5 and 6 are unused by the shield. We choose 5.

The rear fog lamp runs on the car's 12V and draws more current than can pass through the Arduino anyway. A relay is used to convert Arduino's 5V low current output to the car's 12V high current. The relay can run on any digital output pin. We choose 7. The relay cannot be directly attached to the pin, we need to build a relay control circuit or buy one already built.

The switch shorts to ground when activated. The easiest way to detect the low signal on a pin is to use the Arduino's internal pull-up resistor. This works on any pin; we choose 4.

Debouncing the Switch

The switch might not immediately make steady contact, causing the pin's reading to alternate in the first few milliseconds (Wikipedia). Thomas Fredericks' debouncing library is used to stabilize the reading.

The Code

The code rear_fog combines all of these inputs, outputs, and logic. Global variables track the state of the headlights, dimmer, and the rear fog. The main loop checks the CAN bus for new messages, processing messages from the headlights or dimmer. Then the loop checks the state of the switch. Finally, the IP indicator and relay outputs are set.