Lap timer with StopWatch library

Hi guys,

I’m a beginner with arduino so be comprehensive with me…
I want to display the lap times of a race bike using a reed contactor. (like an alfano device)
The first step is displaying the last lap time, not necessary yet to show elapsing time.
So after the first interrupt, the chronometer must start, after the second interrupt, it has to show my lap time until the next interrupt, reset and restart it and so on.
I found the StopWatch Library whiwh seems to be easier, so how can I use it ?
There are plenty of examples doing what I want but not using interruptions (For my project, I need to do numerous things at the same time).

I know it is quite easy to do but I’m struggling…

Here is my code (but not working, it displays mad values for every single interrupt) :

#include <StopWatch.h>
#include <LiquidCrystal.h>
long debouncing_time = 30; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
StopWatch sw_millis;    // MILLIS (default)
StopWatch sw_micros(StopWatch::MICROS);
StopWatch sw_secs(StopWatch::SECONDS);
volatile unsigned long lap;
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);

void setup()
{
    lcd.begin(16,2);
    Serial.begin(9600); 
    attachInterrupt(0, debounceInterrupt, FALLING);
    digitalWrite(2,HIGH);
}

void loop()
{
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(lap);
    delay(200);
}

void debounceInterrupt() {
   if((long)(micros() - last_micros) >= debouncing_time * 1000) {
     chrono();
     last_micros = micros();
   }
 }

void chrono()
{  
noInterrupts();
sw_millis.stop();
lap=sw_millis.elapsed();
sw_millis.reset();
sw_millis.start();
interrupts();
}
StopWatch sw_millis;    // MILLIS (default)
StopWatch sw_micros(StopWatch::MICROS);
StopWatch sw_secs(StopWatch::SECONDS);

Why do you have 3 stop watches going?

If you don't know what unit of measure to use, you are doomed.

void chrono()
{ 
noInterrupts();

It's pointless to disable interrupts, since they are already disabled when an interrupt service routine is called.

Here is my code (but not working, it displays mad values for every single interrupt) :

It is likely that whatever is generating the interrupt isn't doing it cleanly/properly.

I tried several things and forgot to remove what I tested. I will try to run millis, once it works I will go further... Yet I use a push button to simulate my reed contactor so that's why I used a debounce function.

Yet I use a push button to simulate my reed contactor so that's why I used a debounce function.

Interrupts are for things that must happen RIGHT NOW!. Reading the state of a switch is NOT something that needs to happen the nanosecond the switch is pressed.

Since loop() is doing nothing but updating the LCD and stuffing it's head in the sand, poll the switch and get rid of the stupid delay().

You can update the LCD in the body of the if statement that determines that the switch has become pressed. The time required to do that will eliminate the need to debounce the switch.

PaulS:
Interrupts are for things that must happen RIGHT NOW!. Reading the state of a switch is NOT something that needs to happen the nanosecond the switch is pressed.

Since loop() is doing nothing but updating the LCD and stuffing it’s head in the sand, poll the switch and get rid of the stupid delay().

You can update the LCD in the body of the if statement that determines that the switch has become pressed. The time required to do that will eliminate the need to debounce the switch.

The delay was there to avoid the lcd to blink at very high frequency. But yes I could get rid of it by replacing it in the if statement. I get a value as I press the button AND as I release it, why ? It is supposed to be triggered only by a falling edge.

I get a value as I press the button AND as I release it, why ? It is supposed to be triggered only by a falling edge.

The switch bounces on make as well as on break.

Thanks for your help ! I added a capacitor to make an hardware debounce, it's better ! Yet I've got another problem, sometimes it works and sometimes the ounter resets to zero...

inryjo: Thanks for your help ! I added a capacitor to make an hardware debounce, it's better ! Yet I've got another problem, sometimes it works and sometimes the ounter resets to zero...

I hate when my ounter resets...

Presumably you've made some code changes. Therefore, if we are all to be on the same page, you need to post the new code.