Timer and Counting

Is it possible to use an internal timer count at 120Hz when a digital input pin is high? I want to be able to count to a long number (20,000,000) at 120Hz as long as a digital input pin is high. If the digital input pin goes low, I need to stop counting and resume once the pin returns high.

Is there a way to write the long number to the eeprom say every 100,000 cycles. Counting so quickly, and so high, I don't want to store every last number in the eeprom in case of a power interruption (I'll at least be able to resume from the last stored number).

I have tried taping off a the 60Hz square wave I am generating "tone(8,60)", connecting the tap to an analog input pin, and counting that way, which uses an extra pin and is proving to be quite unreliable so far (counting is sporadic and even continues if the wire is disconnected on the analog input pin). If counting from the timer isn't feasible, I'll follow up this post with the tone counting code for help to hopefully make it work a little better for my application.

Thank you,
Scott

I might try setting Timer2 for CTC mode, set the prescaler to CLK/1024, and OCR2A to 129. Set OCF2A, to enable an interrupt every time the counter reaches the top value (129). The interrupt frequency (assuming a 16MHz system clock) will then be 16e6/(1024 * 130) = 120.19Hz.

The ISR can check the digital input line, and if it's high, increment a counter.

The main loop can check the counter value and determine when to write to EEPROM. I might consider an EEPROM wear-leveling technique, e.g. http://www.atmel.com/dyn/resources/prod_documents/doc2526.pdf

I want to be able to count to a long number (20,000,000) at 120Hz

Why?

Pete

el_supremo:
Why?

I'm building a test fixture for a transducer which is operating at 60Hz. As long as proper test conditions are met, I want to count the cycles (two per 60Hz cycle) of the transducer. I have to prove the transducer can operate in a specific environment for 20 million cycles. The Arduino is responsible for producing the signal that moves the transducer, controlling a heater, measuring the temperature via TC of both the transducer and the environment. If the required test conditions are not met, the Arduino mutes the signal going to the amplifier, stopping the transducer until conditions are right again.

I do have a few other sensors that are making sure the transducer excursion meets the requirements, but counting the noisy signal from these sensors is a bit troublesome as well.

Scott

Thank you Jack. I'll give that a try after a bit more searching and reading... Thanks!

Scott

Just starting to read into this. It appears that the tone() function uses timer2. Will following Jack's direction above cause me to lose the tone function that I also need? I need to output a 60Hz squarewave, currently tone(8,60).

thanks.

Scott R.

Opps, yep better stay away from Timer2 then. My assumption was the use of tone() was only to generate the interrupt, but if it's still needed, then go with Timer1. I have a project that uses tone() and also sets Timer1 up for regular interrupts, and that works fine.

Of course, Timer1 is also used for PWM on Arduino pins 9 and 10, so I hope that's not needed as well :astonished: