780 likes | 1.09k Views
2014 NMRA Convention. Dave Bodnar July 17, 2014. This presentation is available on-line at: www.trainelectronics.com or www.davebodnar.com. Revised 07-17-14. PICAXE - What Is It?. A small microcontroller (is that redundant?) that can be programmed to do many tasks
E N D
2014 NMRA Convention Dave Bodnar July 17, 2014 This presentation isavailable on-line at:www.trainelectronics.com or www.davebodnar.com Revised 07-17-14
PICAXE - What Is It? • A small microcontroller (is that redundant?) that can be programmed to do many tasks • A device that is so inexpensive that it can be dedicated to a single task • A device that is programmed from a PC with free software • A device that is programmed in the BASIC programming language (great for beginners!)
PICAXE - What You Need • Computer with serial port or … USB to serial adapter or … USB programming cable • Windows computer running free PICAXE software (Mac & Linux versions available, too) • Power source • 3 @ AA cells = 4.5 volts • 4 @ NiCads = 4.8 volts • 5 volt power supply (from retired cell phone)
Programmers USB to Serial Adapter SparkFun.com sells PICAXE cable – part # PGM-08312 for about $26.00 Or Spark Fun’s USB to Serial – part # PGM-09260for about $15.00 Details on use at www.trainelectronics.com
Flashing Crossing Lights Remember: Don’t use a 9 volt battery or 4 fresh 1.5 volt AA cells! 5.5 volts maximum!
Flashing Crossing Lights Radio Shack # 276-002 $15.00
“Dead Bug” Style • Built on an 8 pin socket • Can be doneto match theschematic
Tip - Soldering • A good iron makes you a better solderer! • MPJA.com • Item #ZD 929C • $46.95 • Digital temperature readout
Tip Soldering • A clean tip makes all the difference. • MPJA.comitem # 31241 TL$2.95
Flashing Crossing Lights Program 1: Notes: • “start” is just a label telling the “goto” where to go • the program remains in the chip’s memory until it is manually erased or overwritten. high 1 ‘turn on LED 1 - pin out1 low 2 ‘turn off LED 2 - pin out2 start: pause 500 ‘wait ½ second toggle 1 ‘switch LED 1 on pin out1 toggle 2 ‘switch LED 2 on pin out2 goto start: ‘do it again
Flashing Crossing Lights Program 2: modified to flash for 10 seconds and turn off for 10 seconds start: high 1 ‘turn on LED 1 low 2 ‘turn off LED 2 For b0=1 to 20 ‘repeat 20 times pause 500 ‘wait ½ second toggle 1 ‘switch LED 1 toggle 2 ‘switch LED 2 next b0 ‘back to loop low 1 ‘turn LED 1 off low 2 ‘turn LED 2 off pause 10000 ‘stay here 10 seconds goto start: ‘do it again
Flashing Crossing Lights • Program simulation is built in!
Flashing Crossing Lights Hardware modification for button or reed switch activation:
TipSalvage Magnets from old Hard Drives • Virtually all computer hard drives have strong (rare earth) magnets in them • Remove case & carefully remove platters and other parts that are in the way • Caution: some platters are GLASS! Fragile and easily converted to SHARP pieces!
Flashing Crossing Lights Program 3: modified to flash 5 seconds on each button push start: low 1 ‘turn off LED 1 low 2 ‘turn off LED 2 if pin3 = 1 then start: ‘if switch off go to top flash: high 1 ‘LED 1 on low 2 ‘LED 2 off for b0= 1 to 10 ‘do this 10 times pause 500 ‘wait ½ second toggle 1:toggle 2 ‘switch lights next b0 ‘do next of 10 loops goto start: ‘start from very top
Two Sensors for Crossing • Add a second sensor so that the light goes on when it passes one sensor… …and goes off when it hits the other sensor. Sensor 2 Sensor 1
Two Sensors for Crossing PauseBeforeStart: 'prevents detecting slow button push low 1 ‘turn off LED 1 low 2 ‘turn off LED 2 pause 1000 'pause 1 second CheckButtons: b1=0:b2=0 if pin3 = 1 and pin4 = 1 then CheckButtons if pin3=0 then b1=1 ‘save which button hit first goto flash endif b2=1 ‘save which button hit first Flash: high 1 ‘LED 1 on low 2 ‘LED 2 off FlashAgain: for w3= 1 to 250 'check for button before changing lights if b2=1 and pin3=0 then PauseBeforeStart if b1=1 and pin4=0 then PauseBeforeStart next w3 toggle 1:toggle 2 ‘switch lights goto FlashAgain: 'continue checking buttons Program 4:
Flashing Crossing Lights on Steroids! Symbol Tone = 100 'sets the tone frequency ( range 20 -127 ) Symbol Quiet = 0 'set quiet tone Symbol Dit_length = 7 'set length of a dot (7 milliseconds)- yields 10wpm Symbol Dah_length = 21 'set length of a dash (21 mS = 3 dots long) Symbol Wrd_Length = 86 'set space between words (43 mS = 2 dashes, 6 dots) Symbol Character = b0 'set register for ch. Symbol Index1 = b6 'loaded with number of chs. in message Symbol Index2 = b2 'counts the number of elements Symbol Elements = b4 'set register for number of elements in ch. Start: Identify: ' routine to lookup ch.& put its value into the ch. register for Index1 = 0 to 29 'cycle through lookup for times = number of ch. in message lookup Index1,(0,1,0,1,0,0,100,0,2,0,129,0,129,0,3,0,132,0,35,0,67,0,195,0,4,0,0,1,0,1),Character gosub Morse 'go to the ch. generation routine next 'loop back to get next ch. and load it goto Start 'return to start to wait for next input Morse: let Elements = Character & %00000111 'look at 3 LS digits and load into Elements register if Elements = 0 then Word_sp ' % means binary Bang_Key: for Index2 = 1 to elements 'loop through correct no. of times for number of elements if Character >= 128 then Dah 'test MS digit of ch. If it is 1 goto the Dah sub routine goto Dit 'if it is 0 goto the Dit sub routine Reenter: let Character = Character * 2 'do a left shift on all the bits in ch. next 'loop back to get the next element gosub Char_sp 'go to sub routine to put in inter-ch. space return 'return to Identify routine to get next ch. to send Dit: sound 2,(Tone,Dit_Length) 'sound tone for dit length sound 2,(Quiet,Dit_Length) 'silence for dit length goto Reenter 'return to look at next element of ch. Dah: sound 2,(Tone,Dah_Length) 'sound tone for dah length sound 2,(Quiet,Dit_Length) 'silence for dit length goto Reenter 'return to look at next element of ch. Char_sp: sound 2,(Quiet,Dah_Length) 'send silence for dah length after ch.completely sent return 'return to get next character Word_sp: sound 2,(Quiet,wrd_length) 'send silence for break between words return 'return to get next ch. Program 5:
Lighthouse or Airport Beacon • Design objectives • Gradually brighten a bulb or LED to near full brightness • Momentarily flash to full brightness • Gradually dim until off • Delay for a set time • Repeat • Able to use LEDs or incandescent bulbs
Lighthouse Beacon • Parts • Adds one resistor and one transistor to the original flasher circuit so that the PICAXE can control a higher power bulb or LED • The most significant changes are to the software
Lighthouse Beacon Something about this should be bothering you!? 10% power Pulsed Width Modulation How does a pin that is normally either ON or OFF provide variable voltage? 50% power 90% power
Lighthouse Beacon symbol bulb = 2 symbol some = 110 start: pwmout bulb,0,0 b2=1 for b1=255 to 0 step -b2 b2=b2+3 pause some pwmout bulb,b1,b2 next b1 b2=1 pwmout bulb, 255,1022 :pause 140 for b1=1 to 255 step b2 b2=b2+3 pwmout bulb,b1,b2 pause some next b1 pwmout bulb,0,0 pause 2000 goto start: Program #6
Lighthouse Beacon • The transistor makes it possible to drive a very bright bulb • Substitute a motor for the bulb and you can control a train’s DC motor! • The size of motor is only limited by the size of the transistor
Garden Railways PICAXE Article April 2013 Issuepages 54-59 Contains a detailed introduction to the PICAXE and three single LED projects
The Projects All Use the Same Hardware • PICAXE 08M2 (order from PHanderson.com - $2.65 each) • 4-5 volt battery • One LED and one current limiting resistor • Two resistors for programming
Proto Board & Power Supply • BangGood.com sells a wireless prototyping board with jumpers & a power supply for less than $6 delivered! • Excellent for experimentation and design
Animation with a Single LED Symbol LED1 = c.2'pin 5 Symbol Dlay = 12 top: high led1 pause Dlay low led1 pause 100 high led1 pause Dlay low led1 pause 400 high led1 pause Dlay low led1 pause 100 high led1 pause Dlay low led1 pause 1000 goto top: • School Bus Strobe
Animation with a Single LED ShortFlash: 'a label For Loopie = 0 to 100 step 1 '100 steps from off to bright brightness=255-loopie pwmout c.2, brightness, loopie 'light the LED pause Dlay next loopie for loopie = 100 to 0 step -1 'repeat above backwards brightness=255-loopie pwmout c.2, brightness,loopie sertxd (#loopie, " ") pause Dlay next loopie return • Mars Light #NO_DATA 'speeds up programming #PICAXE 08M2 'identify the chip SETFREQ m32 'speed it up to 32 MHz SYMBOL Loopie = b2 'label variable b2 as Loopie SYMBOL Brightness = b1 'label b1 as Brightness SYMBOL Dlay = 2 'set constant Dlay to 2 Start: Gosub ShortFlash 'do the shorter flash routine Gosub LongFlash 'do the longer flash routine GOTO Start: 'repeat LongFlash: 'do the longer flash For Loopie = 0 to 255 step 1 '255 steps from off to bright brightness=255-loopie pwmout c.2, brightness, loopie pause Dlay next loopie for loopie = 255 to 0 step -1 'repeat above backwards brightness=255-loopie pwmout c.2, brightness,loopie pause Dlay next loopie return
Tip - Adhesive Great for quick temporary or permanent connections • Similar to the “goo” on the back of a new credit card • I use it in place of hot melt glue • DOTS brand • Michaels (don’t forget the40% off coupon!)
Tip - Connectors We are always looking for ways to connect electrical devices together • Low voltage: • Deal Extreme item # 15234 • DX.com • $2.87 for 10pair includingshipping!
Tip – Heat Shrink Tubing • Excellent for insulating joints & making repairs • Soldering wires first is recommended but not mandatory • Available in many colors – great trick for identifying wires • Available in many diameters, too • Fry’s has an excellent supply, BangGood, too!
Animation with a Single LED • Fred (Flashing Rear End Device) • You could use a PICAXE but DON’T • It is much simpler & less expensive to use a flashing LED! • Search eBay for: flashing led 3mm red diffused
Tip - Testing LEDs White LEDs are OK for long use at 3+ volts, Red, Green, Amber are not • Just about any LED can be tested with a 3 volt lithium battery 2032, 2025, etc • Touch the anode (longer wire) to the + side of the battery • Touch the cathode (wire by notch in LED) to the - side
Garden Railways PICAXE Article 2 August 2014 Issuepages 44-47 Multiple LED projects
Animation with Multiple LEDs • Crossing Signal with 2 LEDs • The LEDs flash “back & forth” • Starts when power is applied • Fixed ½ second rate
Custom Circuit Board for up to 6 LEDs • A custom multi-LED board was designed and fabricated • Supports: • up to 6 LEDs • two trigger switches • two potentiometers • option for power transistors for each LED
Crossing Signal with Variable Rate • A Potentiometer can be read by the PICAXE • Its setting can be used to change the speed of flashing or how long it flashes or any other variable that changes its behavior
Crossing Signal with Variable Rate • When the pot is rotated to one extreme the wiper on the pot shows 5 volts • At the other extreme it is 0 volts • With proportional voltages in between • This translates to a value between 0-255 in the PICAXE with the READADC command
Crossing Signal with Variable Rate Symbol LED1 = b.2 'pin 11 Symbol LED2 = b.4 'pin 9 Symbol Pot1 = pinb.1 'pin 12 Symbol PotValue = b2 Symbol Dlay =w3 sertxd (13,10,"Simple Crossing Signal - d. bodnar 4-16-2012") Initialize: ' do this only once High LED1 ' LED1 is ON Low LED2 ' LED2 is OFF Start: ' LABEL so the GOTO knows where to go! readadc b.1, PotValue sertxd ("pot=",#PotValue,13,10) Toggle LED1 ' if LED1 is on turn it off, if off turn on Toggle LED2 ' same for LED2 Dlay=PotValue*2 ‘double the pot reading pause Dlay' pause based on pot reading goto start: ' go back to Start and do it again The pause time between flashes is based on Pot reading
Emergency vehicle lights • Only uses 5 of 6 LEDs • Simple set of flashing lights, pairs and single in center
Welder SETFREQ m32 'speed it up to 32 MHz Symbol LED1 = c.1 ' var GPIO.2 'pin 6 WHITE Symbol LED2 = c.2 ' var GPIO.1 'pin 5 BLUE Symbol MinNumber =b1 Symbol RNDRange =b2 Symbol RNDResult =w8 Symbol RndNum =w7 Symbol Divisor =w6 Symbol Temp0 =w9 Symbol Temp1 =w10 top: gosub getrndinrange Temp0=rndresult/2:temp1=rndresult/12 pwmout c.2, temp0, Temp1'loopie 'use PWM to brighten the LED gosub getrndinrange: pwm c.1, rndresult, 50 Temp0=rndresult/7 pause temp0 'rndresult /7 goto top GetRndInRange: RndRange =120 'random range --- gives a range from 12 to 62 MinNumber =1 'min number Random RndNum Divisor = 65535 / RndRange rndResult= RndNum / Divisor rndresult = rndresult + MinNumber return • Two LEDssimulate awelder
Six LEDs Can Make a Traffic Light • A pair of traffic lights can be controlled with the six LED circuit board • Each light can be addressed individually • The two triggers can be used cycle the lights • The pots can be used to vary cycle time or “yellow on time”
PICAXE Train Controller • Model Railroad Hobbyist magazine • June 2014 • Free Subscriptionhttp://model-railroad-hobbyist.com/
Speed Controller • The PICAXE-based controller operates a simple layout using the IR remote control to vary track power • It can be used as a basic speed / direction controller • …or it can be programmed to change direction based on time (easy point-to-point controller!)