I am building some behavioral equipment for my neuroscience lab. I will have two levers each attached to their own button. I need to be able to run a subroutine if both buttons are pressed simultaneously or within 0.1 sec of each other. My idea is to use a hardware interrupt to detect the first button push. Have that interrupt run a routine to check if the other button is pushed within the allowed time. If that happens then call the routine to execute the code the should be run. With this there would need to be two routines to check for the second button push trigger by their interrupt. One for button A waiting on B and one for B waiting on A.
My questions:
-Using interrupts seems like they would interfere with each other after the second button is pushed. So I don't think hardware interrupts are the right way to go. What is the right way to do this?
-How should the timing be handled? I don't think I need (nor want) an RTC, but I do not know how to handle some relatively precise timing without one.
-What happens in the remote condition if both buttons are pressed simultaneously? I have no clue how to handle that.
In summary, Help and help. Thanks in advance for any direction given.
Is the sketch doing anything else whilst waiting for the buttons to be pressed ?
Why use an interrupt ?
Read the inputs in a tight loop. When either button becomes pressed, save the value of millis() to separate variables. Calculate the absolute value of one value subtracted from the other and you have the time between button presses
No debounce is required, as the OP indicates he's interested in the time delta between button pushes; just store the time of first transition on either (and block all further on that input), then watch for first transition on the other; reset the whole schmear when, for example, 100 ms have passed without contact closure on either, and continue. Code it right, you can start the timing with either button and watch for the other.
when a button is pressed start a timer. if it expires before a 2nd button is pressed, recognize that that button is pressed. otherwise recognize that the pair of buttons is pressed when the 2nd button is pressed
This is what I needed! I taught myself to program 35 years ago and then didn't get back to it until a few years ago. I know structure and concept but I don't know functions and syntax in contemporary languages. Thanks!
I looked up bouncing. I'm just going to pretend it does not exist. lol Using millis() is what I really needed. Thanks for introducing me to a new concept!
Chuckling... I got into microcontrollers 7-8 years ago when my son entered the EE/ESET program at A&M. Judging by your user-name and length of time coding, it looks like you are having a similar introduction. I've found it quite enjoyable, and you are taking the right approach -- enjoy.
Tips - avoid blocking in loop(). Using millis() or micros() to keep the state of a couple variables updated -- as the other posts provide, is the way to go. Good luck!
I am actually the lab manager in a neuroscience lab. I'm usally the oldest and most experienced one around so I try to look out for and help everyone while also giving them a good natured ribbing. Hence, I'm the dad in the lab.
There's no reason not to run the Serial at 115200.
while (millis() <= time1 + Limit)
while (millis() <= time2 + Limit)
These statements which use <= will be subject to rollover issues if the system is run for more than 49 days continuously. This may or may not be an issue for you.
Since there are 2^32 bits in an unsigned long it can count from 0 to 4294967295.
Computing this in terms of days we have:
2^32 / 1000 / 60 / 60 / 24 = 49.710 days