Shop OBEX P1 Docs P2 Docs Learn Events
Sanity check w.r.t counters — Parallax Forums

Sanity check w.r.t counters

HughHugh Posts: 362
edited 2014-01-18 13:42 in Propeller 1
I have about 12 turns of wire wrapped around the lead of one spark-plug of my boat's outboard engine. This is connected to an IR LED (with a 3V3 zener protecting it and with the LEDs grounded to the engine). The light from this LED runs down some fibre-optic to a receiver that is connected to a pin of the Prop.

If with each pulse I increment the value of a variable and then display that value on an LCD display the count increases happily as the engine rotates.

It goes awry if I try to calculate the RPM based on the difference in the counter value between consecutive pulses. I'd be grateful for a sanity check on my code: I think that either I have a problem because of some misunderstanding of the counter or the result is too big for the variable, RPM (a long).

It must be obvious and simple, but it has me foxed.

dira[PinRPM]~

    repeat                                                                      ' Repeat ad infintum...      
      waitpeq(|<PinRPM, |< PinRPM, 0)                                           ' Check the pin to which the sensor is connected. Wait until it is HIGH
      
      cntRPM    := cnt                                                          ' Make a note of the counter value at this transition
      rpmDiff   := cnt - cntRPMold                                              ' Work out the number of clock cycles between this transition and the last
      cntRPMold := cntRPM                                                       ' The counter value at the last transition is updated to this transition
      
      waitpne(|< PinRPM, |< PinRPM, 0)                                          ' Wait until the pin has changed state again.
      
      RPM := (60 * 80_000_000 )/ rpmDiff




Any observations gratefully received.

(Why this way? Fibre optic doesn't corrode, doesn't leak RF, doesn't get affected by RF and is easy to use)

Comments

  • edited 2014-01-18 11:48
    Hugh wrote: »
    RPM := (60 * 80_000_000 )/ rpmDiff
    

    Your RPM formula exceeds the maximum value of a long variable.

    60 * 80_000_000 will equal 4_800_000_000. In Spin, the values for long range from -2_147_483_648 to +2_147_483_647.

    Sandy
  • evanhevanh Posts: 15,932
    edited 2014-01-18 12:54
    Another minor improvement is always use the latched copy (cntRPM) of cnt within a single calculation cycle.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2014-01-18 13:16
    Try this:
    RPM := 80_000_000 / rpmDiff * 60
    
    It is still possible to overrun.
    So try this as its harder to overrun:
    RPM := 1_600,000_000 / rpmDiff * 3
    

    Duane J
  • HughHugh Posts: 362
    edited 2014-01-18 13:31
    Thanks all - much appreciated. :smile:
  • skylightskylight Posts: 1,915
    edited 2014-01-18 13:42
    Could you divide by 1000 to stop the overrun and then the display shows result multiplied by 1000 to convert back to normal?
Sign In or Register to comment.