High Beamer
From Wiki
This project is for my Katana GSX600F motorcycle. I'm tired of never having my garage door opener handy when I need it. It's either in my backpack, in my Joe Rocket jacket, in my pocket, or in my man-purse. This device solves the problem with a small microcontroller-based device that watches the high-beam switch. When it sees it change state 3 times (3 quick flashes of the passing switch, or 3 changes from high beam to low beams), it triggers the garage door opener.
#define SLEEP_TIME_IN_MS 5
#define HALF_SECOND (500 / SLEEP_TIME_IN_MS)
#define TWO_SECONDS (2000 / SLEEP_TIME_IN_MS)
void main (void)
{
UINT16 switches = 0;
UINT16 switchesLast = 0;
UINT8 edges = 0;
UINT16 timer = 0;
UINT8 activeRelay = 0;
relay_output_1_low ();
relay_output_2_low ();
while (1)
{
sleep_5_milliseconds ();
//
// Sample switch
//
switchesLast = switches;
switches = (switches << 1) | (switch_state () ? 0x0001 : 0x0000);
//
// If timer running and it expires, reset edge count
//
if (timer)
{
if (!--timer)
{
if (activeRelay)
{
relay_output_1_low ();
relay_output_2_low ();
activeRelay = 0;
}
else
{
if (edges == 6)
{
timer = TWO_SECONDS;
activeRelay = 1;
}
else if (edges == 8)
{
timer = TWO_SECONDS;
activeRelay = 2;
}
edges = 0;
}
}
else if (activeRelay & 1)
relay_output_1_toggle ();
else if (activeRelay & 2)
relay_output_2_toggle ();
}
//
// Switch changed
//
if (!activeRelays && (switches != switchesLast))
{
//
// If debounced open or closed for 80ms
//
if (!switches || (switches == (UINT16) -1))
{
timer = HALF_SECOND;
++edges;
}
}
}
}
