Clock and push botton questions

Discussion in 'Embedded Systems and Microcontrollers' started by dayv3, Feb 18, 2015.

  1. dayv3

    Thread Starter Member

    May 22, 2014

    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.
  2. MikeML

    AAC Fanatic!

    Oct 2, 2009
    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.
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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: Feb 18, 2015
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    How accurate do you want your time to be. Is a Milli second enough?
  5. dayv3

    Thread Starter Member

    May 22, 2014
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    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 (Text):
    2. #define LEDpin 13
    3. #define Buttonpin 12
    4. #define LEDontime 3000 // in ms
    6. void setup() {
    7.   // put your setup code here, to run once:
    8.   pinMode(LEDpin, OUTPUT);
    9.   pinMode(Buttonpin,INPUT_PULLUP);
    10. }
    12. void loop() {
    13.   // put your main code here, to run repeatedly:
    14.   unsigned long StartTime=0;
    15.   unsigned long ReactionTime=0;
    17.   digitalWrite(LEDpin,HIGH); // turn on the LED
    18.   delay(LEDontime); // pause for three seconds
    19.   digitalWrite(LEDpin, LOW); //turn off the LED
    21.   StartTime = millis(); //start timing
    23.   while(digitalRead(Buttonpin) == HIGH) ; //do nothing if button not pressed
    25.   ReactionTime = millis() - StartTime; // calculate elapsed time
    27.   Serial.print("Reaction time was ");  // output results
    28.   Serial.print(ReactionTime);
    29.   Serial.println("ms. ");
    30. }
    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.