Please check and see if this is correct.

RexLan

Senior Member
Updated w/video Please check and see if this is correct.

Yes ... this is for my motorcycle ... LOL.

This is the final code ... working and this is a link to a demo video on YouTube:
http://www.youtube.com/watch?v=2R_BpnwrI-0
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 500
  	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 
	


Turn_Off:		
	
	Do
	  Low FlashEnable					'Turn off the signal module
	
		For Counter = 0 to 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

I have the following conditions and am using a small FET with a pull-up on the Picaxe for the signal input and the brake input. I am using a logic level FEAT to switch ground to a flasher module for the turn signal. I built the circuit and it works manually but I do not know if it will work with this code ... please set me straight ... I am quite poor at this!

Power up the Picaxe signal line = 0
Signal activated Picaxe signal line = 1 then it alternates 10101010 50% duty cycle
When the Picaxe times out, ground is removed and the Picaxe Signal line =1 steady
When the switch is manually reset the Picaxe signal line = 0 and needs to be ready for the next event.

My idea is to basically start a timer that will turn them off in XX time unless it is reset by activating the brake switch (instead of clutch). If the user cancels the turn signal manually then it ends the event and waits for another one. If the Picaxe times out then the user is required to manually reset the switch for another event to occur. (this is actually done when the next turn is signaled because of the way the switch is made).

Here is my code.
Code:
#picaxe 08M
#rem
####################################################
#   Automatic Turn Signal Cancellation Code:       #
#                                                  #
#   Code Version       :1A                         #
#   Date               :August 2010                #
#   PICAXE Type        :8M                         #
#   Firmware           :> A.2                      #
#   Editor Software    :5.2.0                      #
####################################################
#endrem

 
'############################################################################
'#    INITIALIZE                                                            #
'############################################################################


'Set 8MHz clock source.
Setfreq m8

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

Symbol Signal			= Input3 	'(pin4)
Symbol Ground			= 0 		'(pin7)
Symbol Brake			= Input2 	'(pin5)
Symbol Timer			= 15
Symbol Debounce			= 25 		'25 ms @ 8MHz.This is the time to allow switch contacts to settle.

Symbol Time				= b0
Symbol Count_Total		= b1
Symbol Counter			= b2
Symbol Flash_Time			= b3



Ready:
	Do						'Wait for turn signal activation
		Counter = 0
		High Ground
		If Signal = 1  then Start	'Signal has been activated.
		Pause Debounce
	Loop
			
Start:
		 		
		Reset:
			
			For Flash_Time = 0 to Timer    	'Start a counter to run 10 seconds
				If Brake = 0 Then	Reset		'Brake has been activated
					Count 4, 500, b2
						Count_total = Counter + Count_total
						if Counter > Count_Total then Ready  	'Signal has been canceled by user
			Next
			
Turn_Off:		
	Do
			Low Ground		'Turn off the signal
	Loop while Signal = 1 		'Wait until switch is reset by user
			Goto Ready
 
Last edited:

hippy

Technical Support
Staff member
Count 4, 500, b2

What's input pin 4 connected to, what's it counting, and why ? Not that it's wrong, just difficult to tell exactly what the code is or should be doing without all the information.
 

westaust55

Moderator
Over and above the comemnt made by hippy, you code has a few other areas than need attention.

1. You have the line:
PAUSE debounce​
but as soon as the turn activated is detected you branch into the start code so no actual input contact debounce occurs and the PAUSE is just a 12.5ms (ay 8MHz) loop delay. Remember that pause times are half the actual value at 8MHz.

2. You have the lines:
Count_total = Counter + Count_total
if Counter > Count_Total then Ready​

however counter is only cleared at the start under the label "Ready:"
so I think that counter willnever be greater than Count_Tttal, at best they may be equal if the are no input pulses from the "something" attached to logic pin 4.

3. You have the FOR loop starting:
For Flash_Time = 0 to Timer 'Start a counter to run 10 seconds​
and Timer is defined as 15.
But there are no pauses other than the delay by the Count command timing for quarter of a second (250ms) so do not see how you are achiveing the 10 second period before auto cancellation.

Have you tired to run you program in the programming Edito Simulate mode to see how to works?

As a suggestion, use SYMBOLs a litle more and be consistent in the sue of the alias names given to variables.
you have the line:
Symbol Counter = b2​
but then use:
Count 4, 500, b2​
whereas this would be more explanatory
Count something, 500, Counter​

You mention a signal giving a 50% duty cycle. Which signal? How?
Sorry but unclear at the moment.

Here is some code with a few more SYMBOL statements and some tweaks.
Suspect it could all be better written if a full written plain english description were available.

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

 
'############################################################################
'#    INITIALIZE                                                            #
'############################################################################


'Set 8MHz clock source.
Setfreq m8

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

SYMBOL something			= Input4  '(pin3)
Symbol Signal			= Input3 	'(pin4)
Symbol FlashEnable			= 0 	'(pin7)
Symbol Brake			= Input2 	'(pin5)

Symbol Timer			= 10	'number of 1 second cycles until indicator auto cancelled
Symbol Debounce			= 25 	'25 ms @ 4MHz of 12.5ms at 8MHz.This is the time to allow switch contacts to settle.

Symbol Time			= b0
Symbol Count_Total			= b1
Symbol Counter			= b2
Symbol Flash_Time			= b3

SYMBOL TurnActive 		= 1
SYMBOL TurnOff	 		= 0
SYMBOL BrakeApplied 		= 0 


Ready:
	Do				'Wait for turn signal activation
	  Counter = 0
	  High FlashEnable
	  If Signal = TurnActive  then Start	
	  Pause Debounce
	Loop UNTIL Signal = TurnActive	'Loop until the Turn Signal has been activated.
			
Start:
		 		
Reset:
	DO			    	'Start a counter to run 10 seconds unless brak applied or signal manulaly cancelled

; do we need the next 3 lines ?????
;	  Count something, 500, Counter	'Count pulses in 250 ms (= 500/2 for 8MHz)
;	  Count_total = Counter + Count_total
;	  if Counter > Count_Total then Ready  	'Signal has been cancelled by user

	  PAUSE 2000			'A delay of 1 seconds at 8MHz
	LOOP Until Flash_Time = Timer OR Brake = BrakeApplied OR Signal = TurnOff 
			
Turn_Off:		
	Do
	  Low FlashEnable			'Turn off the signal
	Loop while Signal = TurnActive 	'Wait until switch is reset by user
	Goto Ready
try it in the PE simulation mode.
Does it do what you want.

I note that you are using 8MHz clock speed. For the applciation would 4MHz not be sufficient. Then values for delays are actual ms valued not halved by faster clock speed.

recognise that you are new to this and hope the above is helpful in organising your thoughts.
 
Last edited:

RexLan

Senior Member
Thanks for the help ... I will clarify and work on this today.

What happens is that the Signal line (input pin 4) sits low. This is from the users switch.
When it is activated by the user it goes high (at the Picaxe input) and then goes low, then high, then low so the Picaxe sees a steady 101010101 at 50%. This tells me that the signal has been activated and still activated.

I need to keep the turn signal signal on (for 10 seconds) until that line stops pulsing. The only way I could figure to do that was with the count (which should be increasing). When it stops increasing then the user has turned the switch off so I want to cancel and go back to a ready state. As long as it is increasing, meaning the user wants the signal on, then I want it to continue to flash the the turn light until the 10 second time has expired.

If the time expires I am stopping the flash module but the signal line will stop and the Picaxe input will stop and sit high until the user manually resets the switch. When they do, then the code should go back to ready for the next event.

I tried the simulator and admittedly don't have much understanding of the code so I don't know how to use it effectively.

I will post some new code with the suggestions/corrections.
 

RexLan

Senior Member
I think I have it doing what it needs to with this code.
I have to use a pin number for the count command - it would not accept a symbol name.
I let it run at 4 MHz instead of 8

My only concern is checking the brake line for activation. With the count delay and pause it could be missed. I would like to check it constantly but could not figure how to make a loop to do that. I should probably debounce the brake input but not sure where to put that either.

Thanks again


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

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

Symbol Signal			= Input3 	'(pin4)
Symbol Brake			= Input2 	'(pin5)

Symbol FlashEnable		= 0 		'(pin7)
Symbol Horn			= 1 		'(pin6)

Symbol Timer			= 10	'number of 1 second cycles until indicator auto cancelled
Symbol Debounce			= 50 	'25 ms @ 4MHz oR 12.5ms at 8MHz.This is the time to allow switch contacts to settle.

Symbol Time			= b0
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
	  Counter = 0
	  High FlashEnable
	  If Signal = Turn_Active  then Start	
	  Pause Debounce
	Loop UNTIL Signal = Turn_Active	'Loop until the Turn Signal has been activated.
			
Start:		 		
	Flash_Time = 0		'Reset the Timer to zero

	DO			    	'Start a counter to run 12 seconds unless brake is applied or signal manulaly cancelled
	   	Counter = 0				
	   Count 4, 750, Counter		'Count pulses in 750 ms 
	  	If Counter <2 then Ready  	'Signal has been cancelled by user
	  	If Brake = Brake_Applied then Start
	  	Inc Flash_Time
	     	PAUSE 500  				'A delay of 1/2 second
	     	 
	LOOP UNTIL Flash_Time = Timer OR Signal = TurnOff 
			
Turn_Off:		
	Do
	  Low FlashEnable					'Turn off the signal
	Loop while Signal = Turn_Active 		'Wait until switch is reset by user
	Goto Ready
 
Last edited:

hippy

Technical Support
Staff member
This is my version attached. Seems to work in simulation. Not sure the algorithm is right though as if indicating and you have to brake the indicators will be turned off. Plus a more compact version though much less understandable IMO.
 

Attachments

RexLan

Senior Member
Thanks hippy ... I will go through this tomorrow.

Your code looks complicated for my level of understanding! I struggle with this stuff and you folks make it seem like it is easy ....
Thanks
 

westaust55

Moderator
Concur with hippy and that was my thought also about cancelling the turn indicator when the brakes are applied.

I would think most people other than maybe rally drivers and bikers ? apply the brakes for some period of time when approaching a sharp turn (but not lane chaning on a freewway).

RexLan,
your program header indicates:
# Editor Software :5.2.0 #
This suggests that you may be using a version which came with a CD in a PICAXE kit.

If you go to the "Software & Drivers" link on the orange toolbar at the top of these pages, you can freely download V5.2.11 which will provide a number of improvements and bug fixes.
 

mega

New Member
Not sure I like the brake idea for cancelling of indicators, unless I've got old and read wrong, I'd be happier with hitting the indicator switch again to cancell... what bike is it for???

I ride a Suzuki 1982 1100 Katana, it has a pulse generator in the speedo bit that will cancel the indicators, it will activate the counter bit when the speed is above a certain amount (forgotten how fast though) and when that count has reached a certain number, it cancels the indicator, but I always manually cancel.

I actually designed an indicator system for my brothers Harley around 18 years ago, it used a 555 timer and a couple of nand gate chips,never built it though.....
 

RexLan

Senior Member
The turn indicator switch is a manual sort of thing with a center position (off). It does not automatically reset like the one in a car does. It is a push/pull device and as it passes through the center position the Picaxe can see the change.

The brake input is not to cancel the indicator and it is a very important function. It is used to reset the "on time" for the indicators. As you approach the turn you will likely tap the brake, this will reset the time back to 10 seconds. If you are sitting at the intersection, with the brake on, the time will continue to be reset until it is released. Then the indicators will go off after the 10 seconds expire. 10 seconds may not be the optimum time but that will be easy to change.

In the case of a lane change the brake probably will not be used so the indicator would go off in 10 seconds if the operator failed to cancel it sooner.

I was going to make a simple intervalometer with a 556 chip but thought what a great application for the Picaxe plus a lot more functionality it wanted.

My concern as I said it missing the brake input and letting the timer expire before the turn has completed. Having the pause time in the loop could create this possibility. It would be ideal if the brake line was scanned all the time but this may require an interrupt and getting well beyond my ability to understand!

I will update the editor software today too ... thanks.
 

hippy

Technical Support
Staff member
To keep the timer going after braking, simply reset the timeout to 10 seconds / elapsed timer to zero whenever braking is detected. No matter how far through current timing it will start again from the beginning.
 

RexLan

Senior Member
I changed the code and looked at the brake line to reset the timer as suggested.
I changed the TEN_SECOND_TIMEOUT_MS to = 1000 to get it to run for only 10 seconds but I don't really understand why. Seems OK though.

Question: The indicator switch input and the flashing signal input is the same wire. Not sure if that matters but can those inputs on the Picaxe be connected together since they are both inputs?

Code:
#Picaxe 08M

Symbol flasherEnableOutput          = 0     ' Pin 0, Leg 7
Symbol brakeAppliedInput            = pin2  ' Pin 2, Leg 6
Symbol indicatorSwitchInput         = pin3  ' Pin 3, Leg 2
Symbol indicatorFlashingSignalInput = pin4  ' Pin 4, Leg 3

Symbol INDICATOR_SWITCH_IS_ACTIVE   = 1     ' Active high
Symbol BREAK_IS_APPLIED             = 0     ' Active Low

Symbol PAUSE_TIME_MS                = 100
Symbol FLASH_TIMEOUT_MS             = 1000
Symbol TEN_SECOND_TIMEOUT_MS        = 1000

Symbol lastFlashingSignal           = bit0
Symbol timeout10s                   = w1    ' b3:b2
Symbol flashTimeout                 = w2    ' b5:b4

Do
  Gosub WaitForIndicatorSwitchActive
  Gosub FlasherEnable
  Gosub WaitForTenSecondsOrUntilDone
  Gosub FlasherDisable
  Gosub WaitForIndicatorSwitchInactive
Loop

WaitForIndicatorSwitchActive:
  Do : Loop Until indicatorSwitchInput = INDICATOR_SWITCH_IS_ACTIVE
  Return

WaitForIndicatorSwitchInactive:
  Do : Loop Until indicatorSwitchInput <> INDICATOR_SWITCH_IS_ACTIVE
  Return

FlasherEnable:
  High flasherEnableOutput
  Return

FlasherDisable:
  Low flasherEnableOutput
  Return

WaitForTenSecondsOrUntilDone:
  timeout10s = TEN_SECOND_TIMEOUT_MS
  flashTimeout = 0
  Do
    Pause PAUSE_TIME_MS
    timeout10s = timeout10s - PAUSE_TIME_MS
    Gosub CheckForFlasherToggling
    if brakeAppliedInput = 0 then WaitForTenSecondsOrUntilDone:
    
  Loop Until timeout10s = 0 Or flashTimeout > FLASH_TIMEOUT_MS Or indicatorSwitchInput = 0 
  Return

CheckForFlasherToggling:
  flashTimeout = flashTimeout + PAUSE_TIME_MS
  'toggle 4		'Added for Simulation
  If indicatorFlashingSignalInput <> lastFlashingSignal Then
    lastFlashingSignal = lastFlashingSignal ^ 1
    flashTimeout = 0
  End If
  Return
 

westaust55

Moderator
Question: The indicator switch input and the flashing signal input is the same wire. Not sure if that matters but can those inputs on the Picaxe be connected together since they are both inputs?
Then yes you could run the same wire to two inputs.

However, if it is one and the same wire, why not just change all instances refering to the "flashing signal input" to be "indicator switch input" and then just use a single input.
also then remove the SYMBOL statement at the top for "flashing signal input"
 

hippy

Technical Support
Staff member
Question: The indicator switch input and the flashing signal input is the same wire. Not sure if that matters but can those inputs on the Picaxe be connected together since they are both inputs?
That will work but it probably makes a nonsense of the algorithm you and I have.

You really need to specify absolutely clearly what the inputs are and what the program is meant to do. As far as I can tell the specification is as follows -

1) The input signal line goes high when indicating starts. A 10 second timeout should be started. A 'flasher enable' output is set high.

2) That input signal line will toggle high and low ( at what rate ? ) while indication continues.

3) If the brake is applied the 10 second timeout should be restarted.

4) When the input signal line stops toggling or the 10 second timeout expires, set the 'flasher enable' low.

5) Wait until the input signal line stops toggling and remains low.

6) Go to (1), ready for a new signal indication.

Is that correct ?
 

RexLan

Senior Member
That will work but it probably makes a nonsense of the algorithm you and I have.

You really need to specify absolutely clearly what the inputs are and what the program is meant to do. As far as I can tell the specification is as follows -

1) The input signal line goes high when indicating starts. A 10 second timeout should be started. A 'flasher enable' output is set high.

2) That input signal line will toggle high and low ( at what rate ? ) while indication continues.

3) If the brake is applied the 10 second timeout should be restarted.

4) When the input signal line stops toggling or the 10 second timeout expires, set the 'flasher enable' low.

5) Wait until the input signal line stops toggling and remains low.

6) Go to (1), ready for a new signal indication.

Is that correct ?

Essentially this is correct. There is a little variation when the unit times out. I will try to post a schematic today a little later. I will also put the whole project in the finished projects section when done with the Eagle files too.

I do not know the rate of the toggle line as it will probably vary from unit to unit and with the battery voltage. The turn signal flasher works with a bi-metal strip. Gets hot and opens the output. Appears to be ~ 50% duty cycle, but unknown rate. That is why I though of the count command but not sure that will work either.

There is one signal line input
There is one brake line input
There is one flash enable output
Power/ground
(I'm going to ass a horn beep later)

This is the logic at the Picaxe

power up = signal line = low
Flash enable = high

Switch activated for turn = signal line high
Start timer
Signal line will toggle 101010101
if user turns it off the signal line = low (and will stay low)
Brake applied = low input (reset timer)

timer expires (switch is still activated)
Set flash enable low (turns them off)
Signal line will stop toggleing and will be HIGH (will stay there until reset)

Wait for user to reset switch
when done the signal line will = low (user has reset the switch)
set flash enable = high

goto start
 

hippy

Technical Support
Staff member
Essentially this is correct. There is a little variation when the unit times out.
Is it possible to explain what that variation is so everyone knows what the complete and accurate specification is ? Then they can verify that code matches with the specification or not.
 

RexLan

Senior Member
Is it possible to explain what that variation is so everyone knows what the complete and accurate specification is ? Then they can verify that code matches with the specification or not.
What I meant was a little variation with what you had written around step 5.

When the code turns off the Flash_Enable line (Picaxe goes low) the signal line will stop toggling and provide a high signal at the Picaxe (won't go low until switch is reset).

At this point we wait for the user to reset the switch which will be indicated by a low at the Picaxe (from the signal line).

When the switch is reset (Signal line at Picaxe goes low) then the Flash_Enable line is turned back on (goes high and ground is provided back to the flasher module).
Start over
 
Top