Flashing Police Lights and Siren - beginner project for 08M2

MaryT

New Member
#1
Hi Everyone,
I haven't quite thought of anything USEFUL yet to do with my Picaxe, so I'm just having a little fun. This is my second attempt at programming the 08M2. It's flashing blue and red police lights and a siren. An all too common sight in the low-rent district I live in!

Code:
' Flashing Police Lights and Siren
'connect 2 blue LEDs in parallel (anode to anode, both cathodes to gnd), 
'connect one of these anodes thru 470 ohm resistor to output 1 on 08M2
'likewise, connect 2 red LED's to output 4
'connect one side of piezo speaker to output 2, other side to gnd

symbol bluelights = 1
symbol redlights = 4
symbol siren = 2

start0:
	high bluelights		'flash blue lights
	low redlights
	pause 70
	low bluelights
	high redlights		'flash red lights
	pause 70
	goto start0
	
start1:					'sound siren simultaneously	
	for b0 = 40 to 105
		sound siren, (b0, 2)
	next b0
	for b1 = 105 to 40 step -1
		sound siren, (b1, 2)
	next b1
	goto start1
 

Attachments

hippy

Technical Support
Staff member
#2
A belated welcome to the PICAXE forum.

You are certainly doing well so far. The best way to learn how to use the PICAXE is to start with smaller projects such as you are doing, try new things and learn new commands. Before you know it you will have a good grasp of what you can do and ideas for what you want to do and try.

Traffic light sequencing is another good one, and especially as you have a board set up with LED's on it. Once you have done that with HIGH and LOW commands you can also look at using 'dirsC' and 'pinsC' commands to achieve the same.
 

MaryT

New Member
#3
Thanks for the welcome and the traffic light suggestion. I will try that, perhaps with a pushbutton for a pedestrian walk. If I have enough outputs-- I'll have to think about that.
 
#4
Thanks for the welcome and the traffic light suggestion. I will try that, perhaps with a pushbutton for a pedestrian walk. If I have enough outputs-- I'll have to think about that.
If you don't have any larger PICAXE chips (yet),
how about linking together two 08M2 chips.

More useful learning ahead. :)

e
 

MaryT

New Member
#5
traffic light

Here is what I came up with for the traffic light program. It was more difficult to do than I thought it would be! At first, I thought I would use parallel processing again, like the example of the bike lock in User Manual 1, but I couldn't figure it out that way. Then I thought I'd have to do it using an interrupt, and this is that program:

Code:
' Traffic Light with pushbutton for Pedestrian Walklight
' red LED is connected thru 220 ohm resistor to output 0 (anode), cathode to gnd
' white LED for pedestrian light is also connected to output 0 thru 1K resistor
' yellow LED for traffic light thru 470 ohm resistor to output 1
' green LED for traffic light thru 470 ohm resistor to output 2
' red LED for pedestrian light thru 470 ohm resistor to output 4
' one side of pushbutton is connected to input3 and also thru 10K resistor to gnd,
' other side to +V 

let dirs = %00010111
setint %00001000,%00001000	' yes if button pushed
main:
	let pins = %00010100		' green light on, walk red
	wait 18
	let pins = %00010010		' yellow light on, walk red
	for b0 = 1 to 15			' spread out 3 sec delay so light doesn't instantly change if PB pushed
	pause 200
	next b0
	let pins = %00000001		' red light on, walk ok
	for b1 = 1 to 100			' spread out 20 sec delay
	pause 200
	next b1
	goto main
	
walklight:
	wait 2					' wait a bit before changing light
	let pins = %00010010		' yellow light on, walk still red
	wait 3
	let pins = %00000001		' red light on, walk ok
	wait 20
	let pins = %00010100		'turn green light back on
	wait 18
	return
	
interrupt:
	if outpin2 = 1 then gosub walklight	' if light green, change lights
	if pin3 = 1  then interrupt		' if light is not green do nothing
	setint %00001000,%00001000
	return
It works, but it has a problem where if someone were to press and hold the pushbutton when the traffic light was either red or yellow, the light would never change. That would be a traffic disaster! So then I completely rewrote the entire program using a simpler approach. Here is my second version:

Code:
' Traffic Light with pushbutton for Pedestrian Walklight
' red LED is connected thru 220 ohm resistor to output 0 (anode), cathode to gnd
' white LED for pedestrian light is also connected to output 0 thru 1K resistor
' yellow LED for traffic light thru 470 ohm resistor to output 1
' green LED for traffic light thru 470 ohm resistor to output 2
' red LED for pedestrian light thru 470 ohm resistor to output 4
' one side of pushbutton is connected to input3 and also thru 10K resistor to gnd,
' other side to +V 

let dirs = %00010111			' set pin 0, 1, 2, 4 to ouput
symbol check = b2			' flag to disallow repeated button presses

main:
	let pins = %00010100		' green light on, walk red
	let check = 0				' clear flag for if button just pressed
	for b0 = 1 to 180
		if pin3 = 1 and check = 0 then gosub walklight	'check if button pushed
		pause 100
	next b0
	let pins = %00010010		' yellow light on, walk red
	wait 3
	let pins = %00000001		' red light on, walk ok
	wait 20
	goto main
	
walklight:
	let check = 1				' mark that button pressed this cycle
	wait 1					' wait a bit
	let pins = %00010010		' yellow light on, walk still red
	wait 3
	let pins = %00000001		' red light on, walk ok
	wait 20
	for b1 = 1 to b0			' redo beginning of green cycle
		let pins = %00010100
		pause 100
	next b1
	return					' go back to regular traffic light cycle
This one just ignores any button presses if the walklight is already on, or about to go on because the traffic light is red or yellow. You do have to hold the PB for up to 1/10 second, but I think that's okay. If I had had another output, I would have made the white walklight flash a few times before it goes to red (at least that's what they do in my town). (Also, I made the lights stay on a much shorter time than a real traffic light, because it's rather boring to watch LED's doing nothing.) I do actually have a 14M2 and a couple more loose 08M2's, and even a 20X2 on order, but I kind of like the challenge of trying to come up with something interesting for the little 08M2 for right now. Making two Picaxes's talk to each other does sound like something fun to learn, though, eclectic.
 

Attachments

#6
These little light programs (and siren) are great for someone who needs model railroading scenes. I did a welding light (blue and white flicker) base on a program published here for my son in laws HO rr setup. I also did a train signal lights with red, yellow and green signals, tripped by reed switches under the tracks and various time delays using 08m2.
Find someone with train layouts and you will have an endless source of projects.
 
#7
train layouts - an endless source of projects

Find someone with train layouts and you will have an endless source of projects.
True :eek:

My hetic 3 years of work is now at an end and presently in holiday mode using some of my accumulated annual leave.
Currently (pre another significant family event mid Feb2016 ) working on a new significant project for DCC based model railways.
So far:
- a few days using a PICAXE to "sniff" the signals and fully/better understand the control protocol of a commercial model railway DCC control system.
- a day sorting a prototyping board layout and constructing some hardware
- a day proving the bidirectional comms is possible at the speeds required to match the target commercial system
- today spend writing the front end software.

still to do:
- merge the front end software and comms code
- mount all (PICAXE based prototyping board, LCD and 4x4 keypad in a handheld enclosure
- draw a decent schematic in DIPTRACE
- do a write up
- post the information in the Finished Project section of the PICAXE forum for all the lurking Model Railway folks :eek:
 
Top