Interrupt/ Debounce advice

Hi all, before i get into it I want to thank anyone who contributes to this discussion.

So ive been developing a system that acts as a timer, data-logger, and sensor feedback. The data from the sensor and time are displayed on a seven segment display.

There are three buttons being implemented:

  1. External reset
  2. Timer stop/start
  3. Switches LCD display between timer and sensor feedback.

Currently i am having a bit of trouble with the buttons, it appears that they only react when the program is looking for a state change thus the buttons will not act as they are intended unless activated at a specific moment. This is where I believe an interrupt would be useful but I would need to couple this with a hardware debounce (capacitor and inverse schmidt trigger) from what I have seen.

My questions is, am i on the right track with this line of thinking? Is there a better way of handling such tasks?

I want to be certain this is the right direction before placing a parts order.

Please note that the code below is for the timer only but there is almost no difference between this and the integrated version.

#define LED 10
#define button_1 8

int val = 0; 
float startTime; 
float stopTime;


void setup() {
  
  pinMode(button_1, INPUT);
  pinMode(LED, OUTPUT);
  
  Serial.begin(9600);
 
}



void loop() {

  startTimer();
  stopTimer();
  readTime();

}


void readTime(){  
    while (digitalRead(button_1) == LOW && val == 1){
    Serial.println(stopTime/1000);
    delay(20); 
    return elapsedTime();
    }
  }
   
  
void startTimer(){
   if (digitalRead(button_1) == HIGH && val == 0){
    digitalWrite(LED, HIGH);
    delay(10); 
    digitalWrite(LED, LOW);
    val = 1;  
    startTime = millis();
    }
  }
  
  
void stopTimer(){
   if (digitalRead(button_1) == HIGH && val == 1){
    digitalWrite(LED, HIGH);
    delay(10); 
    digitalWrite(LED, LOW); 
    val = 0;
    }
  } 
 
 
void elapsedTime(){
  stopTime -= ((millis() - startTime));  
  startTime = millis(); 
  }

Do you use more delay() in the other code ?
If you would have a sketch without delay(), it is easier to read buttons without the use of an interrupt.

The Bounce2_Master library could be used for buttons : Arduino Playground - Bounce

The full code contains only two more delays for the "switch mode" button. This delay is used to light the LED temporarily.

You could replace the delay with millis().

If most delays are used to show the led, that is not so hard to convert it for millis().
Have you used millis() before for delays or intervals ?

boolean ledDelay = false;    // indicate that the delay for the led is active.
unsigned long ledPreviousMillis;

...

void loop()
{
  if( ledDelay)
  {
    if( millis() - ledPreviousMillis >= 50UL)         // 50ms delay, 'UL' = "unsigned long"
    {
      digitalWrite (LED, LOW);             // turn off led after the delay
      ledDelay = false;                         // stop led delay
    }
  }

  ...
  
    digitalWrite( LED, HIGH);           // led on
    ledPreviousMillis = millis();         // timestamp this moment
    ledDelay = true;                  // start delay for the led

}

The demo several things at a time illustrates how to use millis() to manage timing without blocking.

...R

BaronCorvo:
Currently i am having a bit of trouble with the buttons, it appears that they only react when the program is looking for a state change thus the buttons will not act as they are intended unless activated at a specific moment. This is where I believe an interrupt would be useful but I would need to couple this with a hardware debounce (capacitor and inverse Schmidt trigger) from what I have seen.

My questions is, am I on the right track with this line of thinking? Is there a better way of handling such tasks?

Nope. And most certainly yes.

Properly written, your main loop will cycle through thousands of times a second, "polling" the buttons.

Take a look at - try out - this code and study the components - button events including debounce, timer events, toggles and such.