MCU Switch State Change Detection | Falling Edge Detection Problem

Thread Starter

46lukasz

Joined Jul 5, 2017
2
Dear All,

I am a new user on this forum, so first of all a big Hi to everyone that decides to take a look at this thread.

To give you some context to the problem, my project involves using an Arduino Leonardo to make a HID device that will be used as a game controller. It pretty much works and takes in both digital and analogue inputs that can then be mapped to different controls in games.

The digital inputs part is a key matrix with switches that are assigned to the logical buttons on the HID device. For this purpose I use mostly DPDT toggle switches, and wire them in a way that the two positions of the switch use two opposite throws and separate poles, like shown below.


This works well but uses two keys on the key matrix, which in turn means it uses two logical buttons on the HID device. Some games allow you to map the same control to two buttons (e.g. to flip a light ON and OFF), but some don't which makes this not ideal.

So the next thing I tried was to use one pole and connect the two corresponding throws together. This is also shown below.


In this configuration the sense line is always HIGH because the drive line is set high when cycling through the key matrix, and because the switch is always closed due to the shorted throws. While the switch is closed, the logical button on the HID device remains pressed.

There is a short period of time where the state of the sense line is LOW, and this is when the switch is being flicked and the lever is in the middle position right between the two throws, causing the pole not to make contact with neither of the throws. When the flick of the lever is complete the switch is closed again.
This is the equivalent of pressing a button on the HID device, releasing it for a very short period of time and then pressing it again.



The Problem:
The Arduino platform doesn't poll the key matrix fast enough and the LOW state of the sense line is not detected during the fast flick of the switch. This results in the logical button being constantly pressed, even during the flick. While in game, this means that flicking the switch has no effect. On the odd occasion the LOW period is detected and the switch works, but it happens very rarely and is unusable.

How can I highlight that LOW period and make it more detectable to the Arduino MCU?
Is there something I can do to make the LOW state longer, with some kind of a delay circuit?
Maybe using some Falling Edge detector could work. Maybe there is and IC just for what I need but I just don't know about it.
Or maybe there is some completely other way to do it.

I look forward to your ideas.

Thanks in advance and kind regards
Lukasz
 

ebeowulf17

Joined Aug 12, 2014
3,307
Two thoughts:
1) If you have a scope, it would be interesting to see how long, and how consistent, the period of the low switch output is. My gut feeling is that this will be somewhat unreliable and difficult to work with, but I could be wrong.
2) Depending on how many switches you want configured this way, and how many interrupts the Leonardo has, I would suggest using hardware interrupts for the relevant pins. They run independent of polling frequency. With hardware interrupts, the moment the input is pulled low (or high, or whatever you set the interrupt to look for) the Leonardo will call a special subroutine you write for that specific task. It's a good idea to keep these ISRs as short and simple as possible. If the ISR takes too long to execute or modifies variables that the code you interrupted was also modifying, strange things can happen. I'm making them sound scarier and more difficult than they are - they're actually wonderful things; you just have to learn how to play nice with them.
 

ebeowulf17

Joined Aug 12, 2014
3,307
One more thought. If you do get a reliable low pulse out of every switch transition and simply need to extend its length, I've seen lots of circuits on these forums that would do the trick. I think some versions of hardware debouncing circuits might provide the effect you need.

There are several people on these forums who are great at this particular kind of problem: crutschow, alec_t, and analog kid spring to mind, but I'm sure I'm forgetting plenty of others. Any time I try to solve this sort of problem I make the circuit twice as big as it needs to be!
 

Thread Starter

46lukasz

Joined Jul 5, 2017
2
Hi there ebeowulf17, thanks for replying to the thread.
The problem turned out to be quite easy to fix and I was just over complicating things! All i had to do was to use only one side of the switch so that its either OFF or ON, instead of being ON in both positions. Now I just detect the change of state to make it work.

However because of your reply I looked into ISR's and they are something new to me but I already have a lot of applications for them. So thank you for introducing them to me!


Kind regards
Lukasz
 

Deleted member 115935

Joined Dec 31, 1969
0
dont poll,

use interrupt,

also use fast_read not the normal read.

Also try the faster arduinos , such as the teeny 3.2
 
Top