• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MCU Switch State Change Detection | Falling Edge Detection Problem

Status
Not open for further replies.

46lukasz

New Member
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
 

MikeMl

Well-Known Member
Most Helpful Member
The correct way is do it in the Arduino code. The code execution speed inside the Arduino is thousands of times faster than anything the human hand can do.
 

46lukasz

New Member
The code execution speed inside the Arduino is thousands of times faster than anything the human hand can do.
I agree and this configuration works if a program is written to do just that, yet when I add all the remaining code it starts missing the LOW states and makes the toggle switches unusable.

Why not just use a push button?
I got push buttons working, but I like the idea of having toggle switches in a game controller for example for use in flight sims.

Lukasz
 

dr pepper

Well-Known Member
Most Helpful Member
Welcome.
I'm not sure I get the issue exactly however you can speed up an arduino's response to a switch input by using interrupts, then it will only take a few uS to respond, and for switching game pad signals you can use an analogue switch chip like a 4052/4053.
I like your avatar.
 

Pommie

Well-Known Member
Most Helpful Member
Why would you want a toggle switch that you don't know it's position?

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
If you spped up the code reading the switch then you will get problems with switch bounce.

Mike.
 

46lukasz

New Member
Why would you want a toggle switch that you don't know it's position?
I know what you mean: one time you start a game and a toggle switch is up but something in the game is turned off and you flick the switch down to turn it on. The next time the switch might be down and you flick up to turn something on. That's quite OK with me though. I would really like to get the toggle switches to work first.


speed up an arduino's response to a switch input by using interrupts
That's not possible in my case since the Arduino misses the input that would cause the interrupt in the first place, so it's a solution to a different problem.


I'm not sure I get the issue exactly
I probably added confusion by talking about the key matrix and so on... Here is a simple example of what's happening. Imagine that I only have one toggle switch plugged into the Arduino in the configuration shown in the second picture. The drive line is a HIGH output to the pole of the switch and the sense line is an input. The Arduino is probing the input which is HIGH most of the time since the two throws are shorted together. When the switch is flicked, there is a very short period of time where the pole doesn't make contact with either of the throws and thus the sense line goes LOW. That is what I'm looking for.

In fact if I write a program to do just that, the Arduino will sense the LOW state period every time, but because I have a few other things going on (like analogue readings, 8x4 key matrix to go through and other bits), the Arduino misses that LOW state period most of the time and doesn't detect it.

I like your avatar.
Thanks! :)

Lukasz
 

Pommie

Well-Known Member
Most Helpful Member
Could you make the 8x4 key matrix 8x8 by adding 8 toggle switches (2x4 switches = 4x4 contacts) or 8x6 by adding 4 toggles. This would only need 2 or 4 extra pins.

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
You will be able to read the actual position of each toggle switch as it is two contacts in the matrix.

Mike.
 

46lukasz

New Member
I already could do that by wiring the switches like shown in the first picture. But this uses 2 keys in the matrix, one key for each position of the switch.

The wiring in the second picture allows me to use the two positions of the switch on the same key in the matrix by just having a LOW state in between the flick of the switch, but this gives me problems. I'm also OK with not knowing the position of the switch.

Lukasz
 

Pommie

Well-Known Member
Most Helpful Member
By using 1 pin to make it an 8x5 matrix you can read 4 toggle switches as they will appear as 8 push buttons. I assumed the toggles were not part of the matrix. Maybe you need to post a schematic.

Mike.
 

46lukasz

New Member
The toggles are part of the matrix but the matrix itself is not a problem as its working fine and increasing its size won't improve things. Lets Isolate the problem from the rest of the circuit. please read my simplified explanation from post #8.

Lukasz
 

Pommie

Well-Known Member
Most Helpful Member
Increasing the matrix size will improve things as you will be able to ascertain the steady state rather than trying to detect a fleeting transition. To detect the transition you would have to continually scan the matrix which will detect switch bounces and false signals and also you won't know the position of the switch.

BTW, the matrix isn't working fine hence this thread.

Mike.
 

46lukasz

New Member
The key matrix that I have now would allow me to plug in 16 toggle switches in the configuration from the first picture. In this configuration I am able to ascertain the steady state and know the position of the switch. This configuration works 100% but each position of the switch takes up a separate key on the matrix.

The same key matrix would allow me to plug in 32 toggle switches in the configuration from the second picture. However getting this to work requires me to detect a fleeting transition which is the the thing I'm having problems with. It would also not give me the position of the switch which I'm OK with.

you would have to continually scan the matrix
Isn't that how a key matrix works? The controller I'm trying to make just loops the matrix scan function in order to detect new inputs.
 

Pommie

Well-Known Member
Most Helpful Member
Your matrix probably scans at 20 to 50 times per second. In order to see the transition you would probably have to increase that to maybe 1000 times per second. An Arduino is very capable of doing this but you will need to adjust your code. Try writing code to time how long the switch is open and then time your scan accordingly.

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
BTW, you will need diodes at each switch or you won't see any transitions due to the ghosting effect. See here.

Mike.
 

46lukasz

New Member
I have diodes in place as well as pull-down resistors on each of the sense lines so that the line is pulled LOW when the switch is not closed.
I will try to time how much it takes to run through the matrix and post my results soon

Lukasz
 

46lukasz

New Member
The link that you posted about the key matrix also has a very good point about the anti ghosting diodes. I have used 1N4001s :
1N4001s were designed to rectify the AC wall current. Since the wall current "switches" at 60 times per second, the 1N4001 must be within a 60Hz tolerance
Where as
1N4148s are designed for fast switching and have a switching time of 4 nanoseconds
This perhaps could be the problem source as the diode is between the switch and the sense line. I will have to check that out also.

Lukasz
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top