void setup() { pinMode(9, INPUT); MsTimer2::start();}//each time through the loop it does a read on the digital port,//nothing happens till the pin goes "LOW" then each time through// it checks to see if the state has changed, if so, then and//only then does it increment the counter.void loop() { signalstate = digitalRead(9); if(signalstate == LOW) { laststate = 1; } if(laststate == 1 && signalstate == HIGH) { counter++; laststate = 0; }

}

tchadwick

So I re-wrote the program again due to my class using the Arduino Mega and the Mstimer2 not working with it. So here is the new revision, which when outputting to the screen at 1/10th of a second, is fairly accurate, but when going above that, it quickly loses accuracy, any ideas why?

void setup() { pinMode(9, INPUT); Serial.begin(9600); timer.setInterval(100, repeatMe);}//each time through the loop it does a read on the digital port,//nothing happens till the pin goes "LOW" then each time through// it checks to see if the state has changed, if so, then and//only then does it increment the counter.void loop() { timer.run(); signalstate = digitalRead(9); if(signalstate == LOW) { laststate = 1; } if(laststate == 1 && signalstate == HIGH) { counter++; laststate = 0; }

AltairLabs

Here is freq counter we use for DCDW, its speed is determined by the incoming freq and the number of digits of precision you specify. It also has code to reject noisy signals. Probably slower than you want and too many features, but you might find the autoscaling approach useful. There is a fundamental limit on precision vs measurement rate and this is one way to approach it.

tchadwick

Thank you for your post, and we will probably give it a try if we can't get the freqcounter.h library to work, which is currently having problems, because it won't work with the arduino mega 2560. (Keeps saying sbi and cbi are not defined, which I don't understand) I'm pulling most of what I'm doing from this forum: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231326297/15Anyone with ideas on how to fix the freqcounter.h for the mega 2560, that would be amazing! I've tried entering the 2560 to the #define section, but no luck. Please help! Thanks ahead of time!

Here is freq counter we use for DCDW, its speed is determined by the incoming freq and the number of digits of precision you specify. It also has code to reject noisy signals. Probably slower than you want and too many features, but you might find the autoscaling approach useful. There is a fundamental limit on precision vs measurement rate and this is one way to approach it.

That code looks interesting but it's not Arduino ready is it, no setup, loop, Serial.begin(), etc.

I tried adding stuff to try and run it, but gave up after two or three fixes. Compilers are so finicky.

Lefty

tchadwick

Also, does anyone know off the top of their head, what the precision and maximum measurable frequency is for the Arduino? (using freqCounter.h)Thanks again for all the great posts, keep them coming! (Oh after we finish this project, I plan on posting everything learned for others so they don't have to struggle like we are! )

tchadwick

No the code above was a sample attempt at programming, however, we have finally achieved success! (With getting RPM) Using the extra Interrupt on the Arduino Mega, it was much easier.So the way it works is by attaching the encoder's output (either A or B or the Index) to pin 18, then attaching the interrupt to interrupt 5which is only triggered when pin 18 goes from low to high. It then calls the incrementit function that does just that, increments the counter.Then after 10, or 100 ms the count is tallied.(Thus far tested to 17Khz or 1900+ RPM)