# SparkFun Forums

### my timer

Your source for all things Atmel.

### my timer#47710

By a.mlw.walker
#47710
Hi everyone, I have made a function that does the follwing
1. Wait until button 1 pressed
2. When button 1 pressed start timing
3. Wait until button 1 pressed again
4. When B1 pressed again, record time between B1 press 1 and B1 press2
5.Continue timing
6, When B1 pressed again record time bewteen B1 press 2 and B1 press 3

Its so that I can calculate the acceleration of an object, the code is below that does this. But i think it could be more accurate with interupts, could anyone show me how it would be done more accurately?

Thanks
Code: Select all
``````int timing()
{
while (PINC&(1<<PC5))  // loop here until first button press
;

_delay_ms(10);  // delay for 10 ms to debounce the press

while (!(PINC&(1<<PC5)))  // loop here until button is released
;
_delay_ms(10);  // delay for 10 ms to debounce button release

time_ms1 = 0;  // here is where we start timing
time_ms2 = 0;
TCNT1 = 0;

while (PINC&(1<<PC5))  // loop here until second button press
{

// while waiting for button press, keep track of elapsed time
//  if system clock is 1 MHz, 1000 TCNT1 ticks = 1 ms
//  if system clock is 8 MHz, 8000 TCNT1 ticks = 1 ms
if (TCNT1 >= 1000)
{
time_ms1++;
TCNT1 = 10;
}
}

while (!(PINC&(1<<PC5)))  // loop here until button is released
;
_delay_ms(10);  // delay for 10 ms to debounce button release

TCNT1 = 10;
while (PINC&(1<<PC5))  // loop here until 3rd button press
{

// while waiting for button press, keep track of elapsed time
//  if system clock is 1 MHz, 1000 TCNT1 ticks = 1 ms
//  if system clock is 8 MHz, 8000 TCNT1 ticks = 1 ms
if (TCNT1 >= 1000)
{
time_ms2++;
TCNT1 = 10;
}
}
lcd_line1();// display the times
lcd_time(time_ms1);
lcd_line2();
lcd_time(time_ms2);

}
``````
###### long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

###### Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

###### Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

###### Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]