JCA34F:
For 1 revolution? Please post the code that produced that.
#include <FreqCount.h>
void setup() {
pinMode(5, INPUT_PULLUP);
Serial.begin(57600);
FreqCount.begin(1000);
}
void loop() {
if (FreqCount.available()) {
unsigned long count = FreqCount.read();
Serial.println(count);
}
}
IN serial monitor, I got:
641
639
641
640
641
643
646
642
644
644
649
then cut the power to the motor and got:
321
1
1
0
0
0
0
cattledog:
If you are using the library example with a one second gate time, you are seeing 635-638 pulses in one second.
Because the library uses Timer2 to gate the interval, I'm not clear how to get the interval to start and stop with with the object sensor. You can certainly manipulate the timer/counter without the library, but there is a work around with the library as it stands with the fixed gate interval.
First, calculate the belt speed
636/600 = 1.06 rev/second = 1.06*28.699 = 30.421 inches/second
Now you need the time that the object blocks the sensor. For example, if the sensor is blocked for 250 ms as the object goes by, then you know that the object is 7.61 inches long.
Maybe we're a little off-topic here.
I guess don't understand why it would be better to count the "time" rather than just count the "pulses" when I already know that
1 Pulse = 0.0478 inches.
I guess don't understand why it would be better to count the "time" rather than just count the "pulses" when I already know that
How do you know when to start and stop counting? How will you communicate that to the library functions?
The "photo-eye" (light barrier?) inidcates the moments when the starting and ending count has to be taken.
cattledog:
How do you know when to start and stop counting? How will you communicate that to the library functions?
DrDietterich is correct. Using the "photo eye" (an IR photoelectric sensor), I know at what "encoder pulse" to start counting and when to stop counting; then multiply the number of "pulses" to get the length.
I apologize for leading you down this path, but I didn't know where we were going to end up. What I was [originally] wanting was the most effective way to utilize the encoder with a Photo-eye. I felt there might be a better method than just a "digitalRead" on BOTH the encoder and the "photo eye" every time the sketch looped. Using the timer to count "pulses" from the encoder caught my attention as a possible "alternative."
I apologize for leading you down this path, but I didn't know where we were going to end up.
No problem. Forget about using the FreqCount.h library with the Timer2 gate.
Use these functions instead
Call this in setup to initialize the timer1 and get it ready to use as an external clock source counter.
void counter_setup()
{
TCCR1B = 0;
TCCR1A = 0;
TCNT1 = 0;
TIFR1 = 0;
TIMSK1 = 0;
}
Call these start and stop functions with your photo eye
void counter_start()
{
TCNT1 = 0;
//external clock source rising edge
TCCR1B = (1<<CS12) | (1<<CS11) | (1<<CS10);
}
void counter_stop(void)
{
TCCR1B = 0;
}
Call this function to read the timer. It will return the count
uint16_t counter_read(void)
{
return TCNT1;
}
I'm trying to determine the linear inches a chain has traveled by counting the rotations of it's drive sprocket. It this case, the sprocket is roughly has an 11" "pitch diameter," so one revolution means that the cargo ON the chain has moved 34.54" away from the sensor. The sensor will be measuring length, but my accuracy can be +/- 3-inches, so dividing the sprocket rotation into 600 "steps" gives me a theoretical resolution of > 1/8 inch
my accuracy can be +/- 3-inches
In that case I'd suggest you start by using an external binary divider. Your sensor gives 3/0.125 = 24 pulses in that range. A divider with a 1:16 ratio such as SN74LS191N 4-stage Binary Counter would do that.
Of course you CAN save £0.80 and do the division on the arduino - it depends on your plans for the rest of the program.
An equivalent division by 16 is a shift by 4 in software, not a really time intensive operation. An external prescaler mainly reduces the pulse frequency, in case that matters.
Adding another piece of hardware is not my intention. if I am to simply decide by 4, then I will either spend another $30 on a ~150:1 Rotary encoder or drive it with a chain/belt at a 4:1 ratio. I need this to have as few components as possible since the friend who will be using it is NOT "arduino savvy."
Cattledog: unfortunately, you have moved to code which is above my skill-set. I recognize that as Arduino's internal timers, but have NO idea how to implement it.
I'll play with this code and see if I can implement... apparently this is important to know.