Timing and counting
I'm trying to use the BS2 to count pulses and sync them in real time with a 555 timer. Can I use the COUNT command twice to count clock pulses at the same time as input pulses or will it execute one then the other? Any other suggestions on how I can do this?
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
to comand Count itself is already an counter over a period of time .
Amaral
That would actually be very tricky on a Stamp w/o some external reference that is slow enough for the Stamp to check regularly. Otherwise you have to measure actual time through your "main" loop and see how long it takes. And every code change will prob. change the time through the loop. And that's not even accounting for cases where the actual code executed is longer or shorter (due to if/thens, select/case etc).
Let's say your external timer (555 pulses) is 20hz (every 50ms). "Scanning" for pulses might go something like this:
P.S. -- at this kind of resolution, the Word Timer will "rollover" at about every 27 minutes...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
-Phil
How would/could he get that time on a Stamp w/o an external reference or timing/measuring instruction execution time? RCTIME occasionally to count some kind of average "low" time between pulses?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
-Phil
Zoot- a section of code like we're talking about takes a fixed amount of time to execute a loop. As you said, each time you modify the loop code it affects the final result of the counter nested in the loop. However, when you are finished and happy with your program, you just put a line in (outside the counting loop) to calibrate the counter with reality.
Zoot, how does your code work to catch a rising edge? I'm a PBASIC newbie (this is my first forum thread too) and I'm not exactly sure what your sample code is doing. Specifically "TimerPin ^ Timer.BIT0 = 1".
And Phil, once polish my loop off, how do I measure its execution time?
jmalaysia: How would I include the calibrating code INSIDE the loop so I can continually update the time and speed as I go?
By the way, thank you all for your input thus far!
In a XOR, the result is "1" only if the two bits ARE different:
0 : 0 = 0
0 : 1 = 1
1 : 0 = 1
1 : 1 = 0
It's a way of having the counter count up only when the state of the input (the pulse edge) changes, even if it gets sampled more often than it changes. But this kind of schema wouldn't necessarily be relevant if you "manually" time your main loop instead, as PhiPi suggests.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
-Phil
For example:
EX1:
time=0
DO
time=time+1
IF sensor=1 then count=time
LOOP
EX2:
DO
time=time+1
IF... THEN
IF... THEN
IF... THEN
IF sensor=1 then count=time
LOOP
(the IF... THEN's are just for example)
If you ran both loops for and equal period of time EX1 would result in a higher "count" because EX2 has more going on inside the loop, making it take longer to execute.
Let's say we ran each loop for 1 second (simulating 60rpm), with EX1 giving a count of 500 and EX2 giving a count of 400. You have to convert that to speed for it to be useful, right? So a count of 500 in EX1 would be 60rpm, whereas in EX2 a count of 400 would be 60rpm. You just make a little math equation after the loop to convert it to speed. Since you're the only one that knows the wheel size I can't do that.
This will give you the instantaneous speed of the previous wheel revolution. It would update pretty fast so because the wheel is spnning faster than I originally thought, somewhere over 400rpm for 35mph. I think it is something like 12 rpm per mph (for a 27"), so it might be stable enough to read on a display. What I'm trying to say id that if you are rolling right on the threshold of the next mph digit your display might be switching back and forth between digits faster than you can read it. At 35mph (427rpm) it would get a pulse every 140ms. I use 35mph because that is the fastest I would go on my bike before the fear of imbedding road in my face took over at that point!
You're right about the instaneous speed thing, and a calibrated loop would work fine for that. But he also wants to know total elapsed (i.e. "trip") time. That involves much more than just timing one or two loops and is better left to an external timer.
-Phil
Now that I think about it, the one I had on my bike started automatically and shut down after a minute or so of inactivity. I don't think it kept up with elapsed time, but it did retain total distance travelled, which was a nice feature.