Clock and push botton questions

Thread Starter

dayv3

Joined May 22, 2014
38
Hi,

I want to build a reaction timer, but I have questions.

I want to turn on an led and after an amount time the led will
turn off. When the led turns off the user will press a button and
then the display will run and then stop to display the amount of time
between the led turning off and the button press.

Right now I am using a 12F683 but I can use a different PIC.

My questions are how to deal with the button.
How do I handle the button debounce? I wrote a button debounce ISR
but anything with a button will cost some time. How do I calculate the
button press/ debounce time and then correct for it for the displayed time?

I am also a little confused about starting and stopping the clock because
I am starting it internal to the program and stopping it by an external, push
button, event.

Finally, I am using a MAX7219 for the display via its SPI interface, is it better
to use the PIC to control the 7 segment displays directly?

If anyone could give me some direction I would greatly appreciate it.
Dave
 

MikeML

Joined Oct 2, 2009
5,444
Don't do software debouncing. Use an edge-triggered interrupt from the button to read the timer that you start when you turn off the LED. Write the ISR so that it responds only to the first edge from the button. All you care about is the time from LED off to the first button edge.
 

ErnieM

Joined Apr 24, 2011
8,377
Why debounce the button at all?

You are looking for the earliest press event, that event ends your wait for press timing loop and enters the next section of the program where the button state does not matter tiny bit.

The software starts the timer. Then when the software detects the button press the software stops the timer.

The MAX7219 trades one problem for another. Using that means using example hardware then you need learn the (SPI?) interface so your PIC can run it. Not using it means writing multiplex code and designing all the hardware.

Going the MAX7219 route may be the easier way of the two.
 
Last edited:

Thread Starter

dayv3

Joined May 22, 2014
38

djsfantasi

Joined Apr 11, 2010
9,163
Here's a simple Arduino sketch that does what you've described, to the best of my understanding. At a minimum, it serves as an example of reading an external pushbutton and using the Arduino's clock to measure the event, down to a millisecond accuracy.
Code:
#define LEDpin 13
#define Buttonpin 12
#define LEDontime 3000 // in ms

void setup() {
  // put your setup code here, to run once:
  pinMode(LEDpin, OUTPUT);
  pinMode(Buttonpin,INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long StartTime=0;
  unsigned long ReactionTime=0;

  digitalWrite(LEDpin,HIGH); // turn on the LED
  delay(LEDontime); // pause for three seconds
  digitalWrite(LEDpin, LOW); //turn off the LED

  StartTime = millis(); //start timing

  while(digitalRead(Buttonpin) == HIGH) ; //do nothing if button not pressed

  ReactionTime = millis() - StartTime; // calculate elapsed time
  
  Serial.print("Reaction time was ");  // output results
  Serial.print(ReactionTime);
  Serial.println("ms. ");
}
Some improvements I can think of is adding a second button to start the LED, or making the pause time some random value between a minimum and maximum.
 

Attachments

Top