Shop OBEX P1 Docs P2 Docs Learn Events
Meassure time between pulses — Parallax Forums

Meassure time between pulses

PlaneTeeRPlaneTeeR Posts: 100
edited 2006-05-16 21:14 in General Discussion
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
javacode

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
···· }
}
Post Edited (PlaneTeeR) : 5/8/2006 6:10:46 PM GMT

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-08 18:23
    The Timer class has a method passedMS() that returns the number of
    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
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-08 18:39
    Dear Peter,

    passedMS() is not a method of class Timer.

    Look:

    attachment.php?attachmentid=73955·<!-- -->

    But i don't get these methods, what should i use, and where did you get the passedMS() from?



    Thanks Johnny
    728 x 410 - 16K
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-08 18:43
    Sorry, it is in my adapted Timer class.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-08 19:19
    Thanks peter, it works, but is it accurate? It's for the bikecomputer!

    Thanks Johnny
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-08 19:34
    It is as accurate as the clocksource of the javelin,
    and that is enough for your application.
    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-16 18:43
    Dear 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


    sensor log - time between pulses

    46
    46
    1184
    46
    47
    47
    46
    46
    47
    47
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    45
    45
    45
    45
    45
    45
    45
    45
    45
    45
    45
    45
    1183
    45
    45
    45
    45
    45
    45
    45
    45
    45
    1183
    46
    45
    46
    46
    46
    46
    46
    46
    46
    1184
    46
    46
    46
    46
    46
    46
    46
    46
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-16 19:38
    Can't really say without you supplying your code.
    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-16 19:44
    I noticed 569+569+46 = 1184
    So there must be an issue with CPU.carry() when subtracting.
    I will investigate this.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-16 19:54
    Indeed your right about that!

    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-16 19:56
    I added a method passedMS2() to the attached class.
    Please use this class and passedMS2() instead of passedMS()
    and let me know the result.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-16 20:19
    Errata: the·resulting low word must be treated as unsigned for the final divide.
    This is corrected in the attached file.
    Please try passedMS2() and post the result.

    regards peter
  • PlaneTeeRPlaneTeeR Posts: 100
    edited 2006-05-16 21:06
    Dear 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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-16 21:14
    You can see the difference in the code.
    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
Sign In or Register to comment.