• 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.

switch state based on timer

Status
Not open for further replies.

Patchouli

New Member
Hi again.

What is the proper way to make use of multiple states using switches, based on time?

What I mean is - say you press button1, it's associated IO pin sinks current via a resistor to ground, I can detect this and make a subroutine execute. But what if I want a different routine to execute if that pin is held to gnd for more than 2 seconds and yet a 3rd routine if it's held for 5 seconds?

Do you pass timing to the next loop? ie start a timer after first detection and check again at intervals? But then doesn't that change the logic for all button checking code? You now have to process based on leading edge of the switch release and not the press/sink?

I am confusing myself. Am I on the right track?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
Patchouli said:
Hi again.

What is the proper way to make use of multiple states using switches, based on time?

What I mean is - say you press button1, it's associated IO pin sinks current via a resistor to ground, I can detect this and make a subroutine execute. But what if I want a different routine to execute if that pin is held to gnd for more than 2 seconds and yet a 3rd routine if it's held for 5 seconds?

Do you pass timing to the next loop? ie start a timer after first detection and check again at intervals? But then doesn't that change the logic for all button checking code? You now have to process based on leading edge of the switch release and not the press/sink?

I am confusing myself. Am I on the right track?
One way would be to make the keys auto-repeat, usually you would have it start repeating after being held down for a certain time (say after 1 second), them repeat at a faster rate (say every 250mS) while still held down. For your purposes you could count the number of repeats and branch accordingly.
 

TheAnimus

New Member
another way is to use a "tick" counter, this way you use the timer1 register, set to auto-increment, and when the button is first pushed your store this value in a register, then in your timer0 int routine, you wait until the timer1 is say 20+value u saved.

Quick n dirty.

But yes u have the right idea
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top