Meassure time between pulses
Hello everyone,
I'm working with my sensor which outputs logical 0 and 1, I can detect the pulses with my code below.
But now I want to messure the time between 2 pulses, how should i program this. I'v read the Timer class but don't understand how i should implement it, particularly i can't understand how I should retreive the time between them. Or is it not possible with the Timer class?
Thanks·Johnny Kuiper
The Netherlands
I'm working with my sensor which outputs logical 0 and 1, I can detect the pulses with my code below.
But now I want to messure the time between 2 pulses, how should i program this. I'v read the Timer class but don't understand how i should implement it, particularly i can't understand how I should retreive the time between them. Or is it not possible with the Timer class?
Thanks·Johnny Kuiper
The Netherlands
Post Edited (PlaneTeeR) : 5/8/2006 6:10:46 PM GMTjavacode
import stamp.core.*;
public class ReadPulseTest{
·static final int pulsePin = CPU.pin0;
·static boolean pulseDetect;
·static int pulses =0;
·static void main() {
··· while (true) {
······· pulseDetect=CPU.readPin(pulsePin);
······· if(pulseDetect) pulses++;
······· System.out.println("Pulses: " + pulses);
······· }
········//other code here
···· }
}
Comments
milliseconds that have passed since the last call to mark().
So, call mark() when pin goes low, call passedMS() when pin goes high.
static void main() {
· int passed;
· Timer t = new Timer();
· while (!CPU.readPin(CPU.pin0)) ; //wait for pin high
· while (true) {
··· while (CPU.readPin(CPU.pin0)) ; //wait for pin low
··· t.mark();
·· ·while (!CPU.readPin(CPU.pin0)) ; //wait for pin high
··· passed = t.passedMS();
··· System.out.println(passed);
· }
}
regards peter
passedMS() is not a method of class Timer.
Look:
But i don't get these methods, what should i use, and where did you get the passedMS() from?
Thanks Johnny
regards peter
Thanks Johnny
and that is enough for your application.
regards peter
I've tested this program in a situation.
I've got an old motor and I attached the magnet to it, i created a claw to hold the hall-sensor close to the magnet.
The motor has 2 settings low and high, I set it on high and run the program.
It seems that the time between the pulses is around 45 -46 ms, but between those numbers there are higher numbers, what is the cause of this?
The sensor didn't move or something.
Thanks Johnny
The passedMS() returns the number of milliseconds
since the last mark() call, which also must occur
in your code. It may be that mark() is called
in a irregular fashion.
regards peter
So there must be an issue with CPU.carry() when subtracting.
I will investigate this.
regards peter
I the program you gave as a sample
static void main() {
· int passed;
· Timer t = new Timer();
· while (!CPU.readPin(CPU.pin0)) ; //wait for pin high
· while (true) {
··· while (CPU.readPin(CPU.pin0)) ; //wait for pin low
··· t.mark();
·· ·while (!CPU.readPin(CPU.pin0)) ; //wait for pin high
··· passed = t.passedMS();
··· System.out.println(passed);
· }
}
will you get back on this here in this thread?
Thanks·Johnny
Please use this class and passedMS2() instead of passedMS()
and let me know the result.
regards peter
This is corrected in the attached file.
Please try passedMS2() and post the result.
regards peter
I've tested the new passedMS and it works.
It gives a constant value of 50 - 51 ms between the pulses.
What did you do different in this code?
Johnny
The important part is
····· l = l - startLo;
····· if (!CPU.carry()) h--;
Apparently there is no carry when startLo > l when you think of startLo and l as unsigned.
Something I found out recently also when comparing two integers that are to be
treated as unsigned (0x8000 unsigned > 1 but 0x8000 signed < 1)
The attached class has now the updated passedMS().
passedMS2() has been removed.
Thanks for spotting the error.
regards peter