Calculate time between button pushes

I am trying to finish a school assignment, where I am to program a "time quessing game" where I have to use button and calculate the time between buttton pushes using interrupts and store the time to an array.

The program is supposed ot print the following

1
2
3
4
5
Goal time was 20sec. Your time was 8sec.
the avrage time between pushes was 2.00sec.

So far I have tried the following:

int buttonPush = 0;
long start;
long halt;
byte timer;
long duration;

void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
attachInterrupt(2, painallus, FALLING);
  
}


void loop(){
  
}

void painallus(){
Serial.println(buttonPush);
start = millis();
timer = 1;
if(buttonPush <= 4){  
  buttonPush++;
if (buttonPush == 5){
  halt=millis();
  duration = halt - start;
  Serial.println("lopeta");
  Serial.println(duration);
}
}
}

Hi,

  1. Format your code before posting.
    By doing so you avoid missing braces or parentheses errors.
  2. Interrupt routines should be as simple as possible.
  3. Do not use millis() inside interrupt routine.
  4. How accurate is the time to be measured?
1 Like

You are breaking essentially all of the rules for using interrupts, and interrupts are never necessary for reading a button, so it would be better to just start over.

In the Arduino IDE, Files>Examples>Digital>Button would be a useful starting point. The State Change example would be another.

Do not use millis() inside interrupt routine

It is perfectly fine to read millis() in an interrupt but NEVER attempt serial I/O in an interrupt.

2 Likes

I am trying to calculate the time in seconds. If I need to use interrupts for the button, can I call another function from inside the interrupt function?

The best use of an interrupt is to simply notify the main program that an event happened. Set a "flag variable" to 1 and let the main program do all the calculations.

Other rules: variables shared with interrupt routines

  1. must be declared volatile.

  2. must be protected from modification by the interrupt, while being accessed by main

To time button pushes there is no need and no good reason to use an interrupt.

1 Like

I would use:

pinMode(2, INPUT_PULLUP);
1 Like

Does your assignment REQUIRE you to use interrupts? If so I would structure the code this way and do everything in loop() as @jremington said:

Use this code to verify you are actually registering the button push and eliminate wiring issues.

const int buttonPin = 2;
volatile bool buttonPushed = false;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING);
}


void loop() 
{
  if (buttonPushed)
  {
    delay(10);  // debounce time
    buttonPushed = false;
    
    // TBD: Button push is detected.  Do the rest of your stuff here

   Serial.println("Button Pushed!!!");
  }
}

void buttonISR()
{
  buttonPushed = true;
}
1 Like

Thanks @ToddL1962 for your help, this will get me get forward.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.