Help with a small section of code needed

RexLan

Senior Member
I need some help with one section of my code. It works perfectly; however, it is probably time sensitive when checking to see if the Signal Line is toggling (CheckForFlasherToggling). I used hippy's idea to watch that line and it works, but when/if the battery voltage changes it may not because the flasher module will change its rate. This is the same type unit in your car ... on the motorcycle and just a bi-metal switch inside.

I tried to use the COUNT command but could not get it to work. I may not know how to write the line ... LOL. I tried this thinking it would be simple and verified that the signal was pulsing at the Picaxe. I changed the time variable from 250 to 3000 with no success.

Counter = 0
Count 4, 750, Counter
If Counter <2 then Ready

Any help will be appreciated so that the code is not dependent on my one flasher module at a set working voltage. The only other issue is that I have to be able to watch the brake line to reset the timer, so as a practical matter I can't tie up the Picaxe for more than 1 second maximum.

I also made a prototype PCB and installed it on the motorcycle for testing ... it works great.

Thanks.

Code:
#picaxe 08M
#rem
####################################################
#   Automatic Turn Signal Cancellation Code:       #
#                                                  #
#   Code Version       :1C                         #
#   Date               :August 2010                #
#   PICAXE Type        :8M                         #
#   Firmware           :> 9.2                      #
#   Editor Software    :5.2.0                      #
####################################################
#endrem

 
'############################################################################
'#    MAIN ROUTINE                                                          #
'############################################################################

Symbol Signal			= Pin3 	'(pin4)
Symbol Brake			= Pin4 	'(pin3)

Symbol FlashEnable		= 0 		'(pin7)
Symbol Warning			= 2 		'(pin5)

Symbol Flash_ON_Timer		= 25		'number of ~1/2 second cycles until indicator is auto cancelled
Symbol Debounce			= 25 		'20 ms @ 4MHz. This is the time to allow switch contacts to settle.

Symbol Last_Flashing_Signal   = bit0
Symbol Counter			= b1
Symbol Flash_Time			= b2

SYMBOL Turn_Active 		= 1
SYMBOL TurnOff	 		= 0
SYMBOL Brake_Applied 		= 0 


Ready:

	Do							'Wait for turn signal activation
	  Last_Flashing_Signal = 0
	  Flash_Time = 0
	  High FlashEnable				'Enable the flasher module
	  If Signal = Turn_Active then Start	'Signal switch has been activated	
	  Pause Debounce
	Loop UNTIL Signal = Turn_Active		'Loop until the Turn Signal has been activated.


Start:

CheckForFlasherToggling:
  
  DO
  	Pause 350
  	If Brake = Brake_Applied then 		'User has applied the brakes
  	Pause Debounce
  	Flash_Time=0
  	Endif
  	If Signal <> Last_Flashing_Signal Then	' Check to see if the Signal line is toggling
    		Last_Flashing_Signal = Last_Flashing_Signal ^ 1 
    	End If
  	Inc Flash_Time					'Increment the Flash ON time counter
LOOP UNTIL Flash_Time = Flash_ON_Timer		'Continue to flash until the timer expires 
	Pause 100


Turn_Off:		
	
	Do
	  Low FlashEnable					'Turn off the signal module
	
		For Counter = 0 to 5'15
		  If Signal = Turn_Active then	'Wait 20 seconds then send a warning signal
			Pause 1000
			Next
			Tune 0, 4,($1A,$50,$1A,$50,$1A,$50)	'Sound warning
   		    Else Goto ready			' User has reset the manual switch
		  Endif 
	
	Loop while Signal = Turn_Active 		'Wait until switch is reset by user
	Goto Ready
 

hippy

Technical Support
Staff member
I used hippy's idea to watch that line and it works, but when/if the battery voltage changes it may not because the flasher module will change its rate.
I don't have the code to hand but it worked on a variable incrementing at a timed rate. All that would need to be done is increase the timeout point. Anything flashing ( change of input state ) will reset the variable to zero, anything taking longer won't and it will timeout, be considered as not flashing.

What are the longest flash-on and flash-off times you are going to encounter when flashing ? Set the timeout to the highest of those plus a bit more for a safety margin.
 

RexLan

Senior Member
I am not sure what the rate will be and admittedly don't understand the function you used ... but it works as it is now. I am guessing that the signal flashes on about once every 3/4 second, but that is just a guess. It does appear to have a 50% duty cycle however.

I also know that the rate will change with the bulb that is being used due to the resistance and change in current through the module. Like your flasher will go fast when there is a burnt out lamp.

Is there a reason the count command did not work ... did I try to do it wrong?
 

Technical

Technical Support
Staff member
counter is set to a b1 byte variable, so the maximum it can handle is 255 and so you may be getting overflow. Use a word e.g. w6 variable instead.

Also did you use count 4... or count brake...
The latter is wrong as it would be count pin4 using your current symbol. This is not the same as count 4.
 
Last edited:

hippy

Technical Support
Staff member
It's hard to say why COUNT didn't work as we don't have an exact copy of source code using that COUNT.

The COUNT is likely counting flashes ( though not if 'brake' or input pin 4 which brake is on as Technical notes ) but there's more to it than that. If it flashes on every 3/4 of a second it's unlikely you are going to get more than a count of one if you run COUNT for 750ms.

With an elapsing timeout, reset every flash, you only have to worry about flashing slowing down, the period between flash being longer than the timeout period. Faster flashing will simply reset the timeout more often. Think of the timeout as a sawtooth wave, reset to zero on flash then rising of its own accord, when it reaches a particular level a timeout occurs; there has been no flash for however long the timeout period is.

You can model that with pen and paper. Draw two horizontal lines a couple of inches apart. From the bottom line far left move your pen slowly right and up as you tap out the flashing on every tap move your pen back to the bottom line. The line will never break the top line. When you stop tapping the line will keep increasing, eventually break through the top line - the point where the software knows you have stopped flashing.
 
Last edited:

RexLan

Senior Member
I measured the signal today and it is on 380 ms and off the same. I will try to set the code for 425 ms. It is currently set at 400 and seems to work.

Occasionally it appears to miss something and it jumps back and starts over. I can actually see it in the signal and hear it click. It flashes the light 14 times then does a double click noise and flashes it again for 12 times. This happens only occasionally. It does not do that on the simulator however and I can't find it. It is just running on battery so I am sure it is not a dirt signal and it looks crisp on the scope.

I will try to put the entire project up in the finished section with the drawings and PCB as well.

Thanks for the help.
 
Top