Hi All,
Just working on a little something for home where I want to be able to control two relays to switch on some lights in a mode dependent on the number of key presses from one single button.
Relay 1 controls some front lights, Relay 2 controls some side lights.
The CPU I am using is a 68908GZ8 and I prefer assembler if someone wanted to post some example code
So what I want to do is something like this -
A) - If the lights are off, one key press turns Relay 1 on.
B) - If the lights are off, two key presses turns Relay 1 & 2 on.
C) - If either light is on, one key press turns both Relays off.
Couple of questions, would using the Keyboard Module be necessary? Is the only way to do this by using timers and checking for 'x' time between key presses?
Obviously there would also need to be a time out function from A to skiping B and waiting for C.
Any advice on which path to head down would be appreciated.
Thanks,
Ross
Hello Ross,
Since you have only a single switch input, and you require to monitor and time both the on and off states, the code will probably be more straightforward if you don't make use of the KBI module, but simply monitor the switch state by testing the input state of the pin.
For each switching event, there will need to be a debounce interval (perhaps 25-50ms), followed by a switch timeout interval of say 500ms. These timing periods would need to be applied to both states of the switch. Timeout would occur both for a "held" switch, and following switch release.
The easiest way to implement these timing periods is probably as an integral number of TIM overflow cycles. Enable TIM overflow interrupts, with an overflow period in the vicinity of say, 5 ms. This timing is not critical, but would allow both intervals to be handled with an 8-bit counter. Within the ISR code, the counter would be simply decremented, unless its value was already zero.
From within the main loop, a timeout period is started by setting the counter to the required value, and timeout will have occurred when the counter becomes zero.
It seems that, if one or both lights are already on, it might be a good idea to require that the switch be held on for greater than the timeout period, before the turn-off occurs.
The operation of the code within the main loop would effectively become a simple "state machine". The following is a possible sequence of events.
Regards,
Mac