count, pulsin help

Hi guys still quite new to all this, churning throught the manuals etc. Having troubles with the count and pulsin command. simply want to send pulses, probably no more than 10 per second and record how many in say 5 seconds. Looked at the speedometer posts but I cant get anything to register on the simulation. Any help appreciated cheers
 

hippy

Ex-Staff (retired)
[ Moved to Active Forum ]

COUNT can count pulses in a time period, for example the number of times a magnet on a wheel passes a sensor. PULSIN can measure how long the pulse is as such a magnet passes the sensor.

You can derive the wheel's RPM from COUNT and from knowing the circumference of the wheel determine distance travelled and from that speed.

PULSIN can be used as well but the maths is a little more complicated and I couldn't give that off the top of my head.

Start from first principles, decide what it is you are measuring ( speed ) and then determine how what you can measure can be translated into speed. It's more physics and maths than programming to start with.
 

retepsnikrep

Senior Member
I tried two methods for a Speedo function, the first using count and the second using pulsin. My code is below with the maths documented i hope you can follow it.

This is using the Count Command on my Honda Insight

Code:
#REM
	There are 4550 wheel sensor pulses per mile traveled for the Insight,
we are reading pulses per 1/4 second 

	s = speed in MPH 
	x = pulses per second 

	s = (x*360)/455 

	Should give pulses per hour, divided by pulses per mile, to give you a mile per hour figure
 
	so for 60mph reading would be reading about 76 pulses per second 

	Speed Mph = (X*360)/455 

	The maximum pulses per second the 16 bit speed maths can stand is 180 which equates
to a speed of 142 mph
	If you exceed 142mph the speedo will be incorrect/overflow (read low) during that second

	Now we need Distance in feet travelled in that second
 
	Distance ft per second = (X*528)/455 

	To keep a running total of feet travelled
 
	Distance = Distance + (X*528)/455 

	When Distance = 5280ft (1 mile) increment odometer and start again

	The maximum pulses per second the 16 bit distance maths can stand is 124 which equates 
             to a speed of 98mph
	If you exceed 98mph the distance will be incorrect/overflow (read low) during that second

#ENDREM

CheckSpeed:

	CountW = 0				;Set Local Variable CountW to 0 (Zero)
	
	Count SpeedSensor,500,CountW		;Measure speed by counting VSS pulses for 250ms (Vehicle Speed Sensor)
	
	CountX = CountW * 1440 / PulsePerMile	;Calculate Speed in mph (Speed stored in CountX)
I then moved to the Pulsin command as the Count command always took .25 or .5 second to excute
depending on the cpu speed 4 or 8mhz and I needed that time for my other code.

So this is the pulsin code.

Code:
#REM	*****  Using the Pulsin command to measure the length of a single pulse  instead of the Count command *******

	We could try this: 

	7908 / (pulsin / 10) 

	This will return the following: 

	1.2067 mph = 1.21 
	1.875mph = 1.88 
	3.75mph = 3.75 
	7.50 = 7.50 
	15.00 = 15.00 
	30.00 = 30.00 
	60.00 = 60.00 
	120.00= 120.00 

	We can get remainder using modulo division and then create an integer value from 12 (1.2mph) to 1200 (120.0mph)
	by appropriately scaling the two results. 

	We still need to do the overflow check, which means that the speedo won't read below about 1.2mph.
	
#ENDREM

CheckSpeed:

`	Pulsin command measures the length of a pulse. In no pulse occurs in the timeout period, the result will be 0 

	pulsin SpeedSensor,0,CountW	;Measure length of a VSS pulse in 5us (8mhz) units (Timeout 0.32768s)

	if CountW = 0 then jumpover	;If CountW = 0 means pulsin has timed out and vehicle is moving at < 1.5mph so jumpover calculations
		
	CountW = CountW / 10		;Divide CountW by 10 to enable results to fit into 16 bit Integer Maths

	CountW = 7908 / CountW		;Returns speed in mph accurate to 1 mph 

jumpover:
You will need to substitute your own constants and variables as required. This is just a straight lift from two versions of my Plug In Hybrid Vehicle Battery Management software project.
 
Last edited:
cheers guys, especially retepsnikrep for the help....made progress - kids will be pleased, opted for the count command, sorted out the maths and added odometer too. Much appreciated!
 
Top