Interrupt Timer with digital Input

Hello @ll! I need help :’(

I want to realize a counter with a brand new arduino Diecimila.
First a few thing about it:

I have a switch connected on digital 2 (active high) as start/stop button and a four digit display (XXX.X - 7segment display for a maximum of 999,9 seconds) connected via UART Serial.
Is the switch pressed (high) a timer should count milliseconds an send it via serial strings to the display ten times a second. Is the switch released (low) the timer should stop an the display is frozen (it shows the value from the stop-moment, for example 17,4 seconds).

My questions:
1.) How do I have to modify my code with a “switch-interrupt” so that the timer only runs if the button is pressed. I´ve found something about attachInterrupt(interrupt, function, mode) on the arduino page but i don´t know if this is want I need and how to integrate it into my code.
2.) In a second step I have to change the output function. In my sketch the output function only sends the value to the serial monitor of arduino. But I have to convert the value to a bit schema (specification from the display). There I have a DAB (Destination Adress Byte) to select the digit of the display and DB1 - DBn as Databytes to set the selected digits. Has anybody a n example or a link for me to get my project work?

#include “avr/interrupt.h”
int counter = 0;
int ms = 0;
int ms_old = 0;
int s = 0;
//+++++++++++++++++++++++++++++++++++

ISR(TIMER2_OVF_vect) {
TCNT2 = 6; //Timer Reset
counter += 1;
if (counter == 100) { ms+=1;
if (ms==10) { ms=0;s+=1;}
counter = 0;
}
};

//+++++++++++++++++++++++++++++++++++

void setup() {
Serial.begin(9600);
Serial.println(“Timer started…”);
//Timer2 Settings: Timer Prescaler /64,
TCCR2B |= (1<<CS22);
TCCR2B &= ~((1<<CS21) | (1<<CS20));
// Use normal mode
TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); // turn off WGM21 and WGM20 bits
TCCR2B &= ~(1<<WGM22); // turn off WGM22
// Use internal clock - external clock not used in Arduino
ASSR |= (0<<AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
TCNT2 = 6; //Timer Reset
sei();
}
//+++++++++++++++++++++++++++++++++++
void loop() {
if (ms_old != ms) {
data_out ();
ms_old = ms;}
}
//+++++++++++++++++++++++++++++++++++
void data_out () {
Serial.print(“Zeit: “);
Serial.print(s);
Serial.print(”.”);
Serial.print(ms);
Serial.println(“s”);
}

So that help can be given.....

a) Do you for some reason need to do the "on"/"off" via an interrupt?

b) Do you know about the issues of "switch bounce"

=== I'd build up what you have described in stages...

1) First a "Count 1,2,3...." program, just to be sure that the sending of numbers to the display is all sorted out.

2) Second a simple "count times switch is switched" program... not using interupts. This will check switch state sensing, apart from anything else.

3) Third, something like what you want, but with a much lower display rate, say update display once every 10 passes through the "check state of switch" loop. I suspect your display update frequency hopes may be beyond the system.

Hi and thanks for the fast answer!

a) I thing to handle the switch by an Interrupt makes sense because then the counter is very exact. I have to measure the time in seconds and tenth parts a second (for example 12,3 seconds). When i only read out the state of the input at one time in the program i think the measurement isn´t exact enough. What is when the button is pressed but the program is in another part and needs a little time to reach this point because the data out-function needs to much time?? I really don´t know how many time it will need…

b) I think switch bouncing will be a problem in both versions or not? Won´t this problem be fixed with a short delay between reading out the switch?

So what do you think which version is the best for this job? To have an interrupt routine which is only counting (but exact) and a loop which reads the state of the switch, is calculating the time and puts the data out? Or do i have then the problem that the interrupt routine will “interrupt” the rest of the program and there is not enough time for the rest???

The display has the change very fast, so that you can see how the time flies (every tenth second)…

Your logic re- using an interrupt seems fine. No HARM in using one... I just wondered if it was really worth the modest extra effort.

You DO seem to understand switch bounce, good. I just wanted to be sure that you weren't going to get caught by the newbie trap.

In putting your app together, be sure to consider using millis():

"Returns the number of milliseconds since the Arduino board began running the current program. "