2014 Amherst Railroad Hobby Show

Post on 19-Mar-2016

49 views 1 download

description

2014 Amherst Railroad Hobby Show. Dave Bodnar January 24, 2014 Amherst, MA. This presentation is available on-line at: www.trainelectronics.com or www.davebodnar.com. Revised 01-24-14. Introduction - What is a PICAXE?. History Developed in Great Britain Designed for use in schools - PowerPoint PPT Presentation

Transcript of 2014 Amherst Railroad Hobby Show

11

PICAXE PIC1

2014 Amherst Railroad2014 Amherst RailroadHobby ShowHobby Show

Dave BodnarDave BodnarJanuary 24, 2014January 24, 2014Amherst, MAAmherst, MA

PICAXE WorkshopEnhancing your Railroad

with Microcontrollers

This presentation isavailable on-line at:

www.trainelectronics.com or www.davebodnar.com

Revised 01-24-14

22

PICAXE PIC2

Introduction - What is a PICAXE?Introduction - What is a PICAXE?

• History– Developed in

Great Britain– Designed for

use in schools– Financed by

energy companies

33

PICAXE PIC3

Introduction – What are its Introduction – What are its Capabilities?Capabilities?

• Has all of the capabilities of a computer– Input– Output– Memory– Control

44

PICAXE PIC4

Introduction - What are its Introduction - What are its Limitations?Limitations?

• Voltage must be controlled

• Requires a computer for initial programming

• Expects correct syntax in programs

55

PICAXE PIC5

What is needed to use a PICAXE? What is needed to use a PICAXE?

• Hardware – circuit, PICAXE• Software – free from PICAXE.com• USB programmer• Computer – Windows, Mac or Linux• Power – 3-5 volts from batteries or plug in

adapter

66

PICAXE PIC6

How can it be used in a model How can it be used in a model railroading environment? –railroading environment? –

• Lights – usually LEDs but others, too• Motors – control speed & direction of rotation• Animations – only limited by your imagination!• Servos – for turnouts, animations, signals• Sound – controls external sound boards• Control – can operate a train, switches, signals,

etc.

77

PICAXE PIC7

Hardware – what we needHardware – what we need

• PICAXE – many types available – using 14M2 for this workshop

• Components to complete a basic circuit – two resistors, voltage regulator, programming header, power supply (battery or plug-in)

• Windows computer – operating system may be as old as XP (not Windows ME!)

• Programmer – USB to 3 pin servo plug

88

PICAXE PIC8

Software – what we needSoftware – what we need

• PICAXE software from http://picaxe.com• Includes editor, simulator and programming

software

• Manuals available under Help

99

PICAXE PIC9

Pre-Wired PICAXE Circuit BoardPre-Wired PICAXE Circuit Board• This schematic shows the circuit’s parts

1010

PICAXE PIC10

Voltage RegulatorVoltage Regulator

• A 7805 voltage regulator and filter capacitor are used to convert higher voltages to 5 volts

1212

PICAXE PIC12

Circuit BoardCircuit Board

• Components:– up to 6 LEDs– 2 trigger switches – 2 potentiometers– 6 power transistors– 5 volt regulator

1313

PICAXE PIC13

Other ComponentsOther Components

• Two programming resistors and a 3 pin connection to the programmer

• Pin 1 helps you to orient the chip in its socket

• What do the current limiting 470 ohm resistors do?

• You must properly orient LEDs & 2N2222 (NPN) transistors

1414

PICAXE PIC14

ManualsManuals

• There are three manuals that are accessible from the programming environment

• Click Help to show them

1515

PICAXE PIC15

Install 1 LEDInstall 1 LED

• Plug one red/black lead to the pins labeled as LED1

• Red wire to +

1616

PICAXE PIC16

First ProgramFirst Program

#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11

SERTXD (13,10,"One Flashing LED - d. bodnar 8-14-2013")

Top:TOGGLE LED1 'if it is on turn off, if off turn onPAUSE 500 'wait for 1/2 secondGOTO Top:

• How can we change the speed of flashing?• How can we change which LED flashes?

1717

PICAXE PIC17

Simulation is Built In!Simulation is Built In!

1818

PICAXE PIC18

Program #2 – add a potentiometerProgram #2 – add a potentiometer#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11SYMBOL Pot1 = pinb.1 'pin 12SYMBOL Rate = w13 'word variable to store flash rateSERTXD (13,10,"One Flashing LED - rate set by pot - d. bodnar 8-14-2013")

Top:TOGGLE LED1 ' if it is on turn off, if off turn onREADADC b.1, RateSERTXD ("Rate = ", #Rate, 13,10) 'show rate reading on terminal Rate = Rate * 10 'make Rate 10 times largerPAUSE Rate GOTO Top:

What is the range READADC returns?Why is SYMBOL using W13, a word variable?

2020

PICAXE PIC20

Program #3 – add a triggerProgram #3 – add a trigger#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11SYMBOL Pot1 = pinb.1 'pin 12SYMBOL Trigger1 = pinc.4 'pin 3SYMBOL Rate = w13 'word variable to store flash rateSERTXD (13,10,"One Flashing LED - rate set by pot - d. bodnar 8-14-2013")

Top:IF Trigger1 = 1 THEN

SERTXD ("Waiting...",13,10)GOTO Top:

ENDIFTOGGLE LED1 ' if it is on turn off, if off turn onREADADC b.1, RateSERTXD ("Rate = ", #Rate, 13,10) 'show rate reading on terminal Rate = Rate * 10 'make Rate 10 times largerPAUSE Rate GOTO Top:

What is the advantage of having “Waiting” sent to the terminal while no trigger is seen?

What is the disadvantage?

2121

PICAXE PIC21

Change to 2 Red LEDsChange to 2 Red LEDs

• Pry green LEDs from 2 white sockets• Install two red LEDs

– Be sure to align longer LED lead (anode) with red wire

– Shorter LED lead (cathode) to back wire• Install on LED1 and LED2

2222

PICAXE PIC22

Program #4 – two LEDsProgram #4 – two LEDs#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11SYMBOL LED2 = b.4 'pin 9 SERTXD (13,10,"Simple Crossing Signal - d. bodnar 8-14-2013")

Initialize:HIGH LED1 ' LED1 ONLOW LED2 ' LED2 OFF

Top: ' just a LABEL TOGGLE LED1 ' if LED1 is on turn it off, if off turn onTOGGLE LED2 ' same for LED2PAUSE 500 ' PAUSE for 1/2 second GOTO Top: 'Do it again

2323

PICAXE PIC23

Program #5 – add a timerProgram #5 – add a timer#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11SYMBOL LED2 = b.4 'pin 9

SERTXD (13,10,"Timed Crossing Signal - d. bodnar 8-13-2013",13,10)

Initialize:HIGH LED1 ' LED1 ONLOW LED2 ' LED2 OFF

FOR B1 = 1 TO 20 'repeat things between here and NEXT B1 20 times SERTXD ("B1 = ",#b1,13,10) ' display value of B1 on terminal TOGGLE LED1 TOGGLE LED2 PAUSE 500 NEXT B1 'go back and get the NEXT B1 until it equals 20SERTXD ("Pausing for 10 seconds",13,10)LOW LED1 'LED1 offLOW LED2 'LED2 off FOR B1 = 1 TO 10 SERTXD ("PAUSE = ",#b1,13,10) PAUSE 1000 'PAUSE for 1 secondNEXT B1GOTO Initialize: 'do it again

2424

PICAXE PIC24

Program #6 – Start on TriggerProgram #6 – Start on Trigger#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chip being usedSYMBOL LED1 = b.2 'pin 11SYMBOL LED2 = b.4 'pin 9 SYMBOL Trigger1 = pinc.4 'pin 3

SERTXD (13,10,"Crossing Signal start with trigger - d. bodnar 8-13-2013",13,10)

StayHere: SERTXD ("No Trigger Seen",13,10)IF Trigger1 = 1 THEN StayHere: 'no button - keep looking

Initialize:HIGH LED1 ' LED1 ONLOW LED2 ' LED2 OFF

FOR B1 = 1 TO 20 'repeat things between here and NEXT B1 20 times SERTXD ("B1 = ",#b1,13,10) ' display value of B1 on terminal TOGGLE LED1 TOGGLE LED2 PAUSE 500 NEXT B1 'go back and get the NEXT B1 until it equals 20LOW LED1 'LED1 offLOW LED2 'LED2 off GOTO StayHere: 'do it again

2525

PICAXE PIC25

Program #7 – 2 TriggersProgram #7 – 2 Triggers#TERMINAL 4800 'start terminal at 4800 baud#NO_DATA 'don't download data - speeds up programming#PICAXE 14M2 'identify the chipSYMBOL LED1 = b.2 'pin 11SYMBOL LED2 = b.4 'pin 9SYMBOL Trigger2 = pinc.3 'pin 4SYMBOL Trigger1 = pinc.4 'pin 3

SERTXD (13,10,"Two Trigger Crossing Lights - one starts, other stops - d. bodnar 8-14-2013")PauseBeforeStart: 'prevents detecting slow button push or restartLOW LED1LOW LED2PAUSE 1000CheckButtons:SERTXD ("No Trigger Seen",13,10)b1=0:b2=0 'clear variablesIF Trigger1 = 1 AND Trigger2 = 1 THEN CheckButtons ' no button hitif Trigger1 = 0 THEN SERTXD ("Trigger 1 hit first",13,10) b1=1 'save which button hit first GOTO Flash 'skip ahead and start blinkingENDIF SERTXD ("Trigger 2 hit first",13,10)b2=1 'save which button hit firstFlash: HIGH LED1LOW LED2 FlashAgain:FOR w3= 1 TO 100 'check for button before changing lights IF b2=1 AND Trigger1=0 THEN PauseBeforeStart IF b1=1 AND Trigger2=0 THEN PauseBeforeStartNEXT w3SERTXD ("toggling!",13,10)TOGGLE LED1:TOGGLE LED2 GOTO FlashAgain:

Explain “FlashAgain” routine

How can you slow flashing?

2727

PICAXE PIC27

Program #8Program #8 – Morse Code – Morse Code

Change message

Don’t forget the !

2828

PICAXE PIC28

Single White LEDSingle White LED

• Install a white LED (clear body) • Connect to LED1 on board

2929

PICAXE PIC29

Program #9 - LighthouseProgram #9 - Lighthouse#TERMINAL 38400 ' faster due to oscillator change SETFREQ#NO_DATA 'speeds up programming#PICAXE 14M2 'identIFy the chipSETFREQ m32 'speed it up to 32 MHzSYMBOL Loopie = b2 'label variable b2 as LoopieSYMBOL Dlay = w10 SYMBOL LED1 = b.2 'Pin 11 SERTXD (13,10,"New Lighthouse - 8-14-13 - d. bodnar",13,10)PWMOUT LED1,150,150Start: Dlay=20000FOR Loopie = 0 TO 150

SERTXD (#loopie, " ")PWMDUTY LED1, loopie 'use PWM to brighten the LEDPAUSEUS Dlay 'PAUSE a bitIF Loopie > 75 THENDlay = Dlay -135 ELSEDlay = Dlay -45ENDIF

NEXT loopie 'get the NEXT item in fir/NEXTSERTXD (13,10, "BRIGHT ")

PWMOUT LED1, 255,1023:PAUSE 1000 'flash to full bright briefly

FOR loopie = 150 to 0 step -1 'repeat backwardsSERTXD (#loopie, " ")PWMDUTY LED1, loopiePAUSEUS Dlay 'PAUSE a bitIF Loopie > 75 THEN

Dlay = Dlay +135 ELSE

Dlay = Dlay +45ENDIF

NEXT loopie

SERTXD (13,10,"pausing",13,10)PWMOUT c.2,255,0LOW c.2PAUSE 15000 'PAUSE a bit with LED offSERTXD ("pausing DONE",13,10)

GOTO start 'do it again!

3030

PICAXE PIC30

How does a pin that is normally either ON or OFF provide variable voltage?

Lighthouse BeaconLighthouse BeaconPulsed Width Modulation

0 volts

1.25 volts

2.5 volts

3.75 volts

5 volts

Something about this should be bothering you!

?

3131

PICAXE PIC31

2 White LEDs2 White LEDs

• Connect a white LED to LED1• Connect another white to LED2

3232

PICAXE PIC32

Program #10 – Ditch LightsProgram #10 – Ditch Lights'd. bodnar 1-16-14 #picaxe 14M2#terminal 4800Symbol LED1 = b.2 'pin 11Symbol LED2 = b.4 'pin 9 SYMBOL minimum = 1SYMBOL maximum = 400SYMBOL speed = b6pwmout LED1, 255, 1000pwmout LED2, 255, 1000top:readadc b.1, speed:speed=speed / 5 min 5for w0= minimum to maximum step speed sertxd (#w0," ",#speed,13,10) pwmduty LED1, w0 w1=maximum-w0+minimum pwmduty LED2, w1next w0readadc b.1, speed:speed=speed / 5 min 5for w0= maximum to minimum step -speed sertxd (#w0," ",#speed,13,10) pwmduty LED1, w0 w1=maximum-w0+minimum pwmduty LED2, w1next w0goto top

3333

PICAXE PIC33

Install 6 LEDsInstall 6 LEDs

• Connect Red to LED1 and LED4• Connect Amber to LED2 and LED5• Connect Green to LED3 and LED6

3434

PICAXE PIC34

Top:'One red and Two greengosub AllOffhigh Red1high Green2w3=0StayHere:w3=w3+1sertxd (#w3," ")if Trigger1 = 1 and w3< 1000 then StayHere:

low Green2high Yellow2pause 4000low Yellow2high Red2low Red1high Green1w3=0ButtonStillOn:w3=w3+1sertxd (#w3," ")if Trigger1 = 1 and w3< 1000 then ButtonStillOn:

StayHere2:if Trigger1 = 0 then StayHere2:

low Green1high Yellow1pause 4000low Yellow1high Red1low Red2high Green2

ButtonStillOn2:if Trigger1 = 0 then ButtonStillOn2:

goto top:AllOff:low Red1:low Yellow1:low Green1low Red2:low Yellow2:low Green2return

#11- Traffic light with 6 LEDs#11- Traffic light with 6 LEDs#NO_DATA#TERMINAL 4800

Symbol Red1 = b.2 'pin 11Symbol Yellow1 = b.4 'pin 9Symbol Green1 = b.5 'pin 8Symbol Red2 = c.0 'pin 7Symbol Yellow2 = c.1 'pin 6Symbol Green2 = c.2 'pin 5Symbol Trigger1 = pinc.4 'pin 3Symbol Trigger2 = pinc.3 'pin 4Symbol Pot1 = pinb.1 'pin 12Symbol Pot2 = pinb.3 'pin 10

sertxd (13,10,"Traffic Light Test - d. bodnar 4-17-2012")

Initialize:for b1= 1 to 1

high Red1pause 300high Yellow1pause 300high Green1pause 300high Red2pause 300high Yellow2pause 300high Green2pause 300

low Red1:low Yellow1:low Green1low Red2:low Yellow2:low Green2

pause 1000next b1

ButtonStillOn2:if Trigger1 = 0 then ButtonStillOn2:

goto top:AllOff:low Red1:low Yellow1:low Green1low Red2:low Yellow2:low Green2return

3535

PICAXE PIC35

#12 - Emergency vehicle lights#12 - Emergency vehicle lights

• Only uses 5 of 6 LEDs• Simple set of flashing lights, pairs and single in

center

3636

PICAXE PIC36

#13 - Movie Marquee Lights#13 - Movie Marquee Lights

• Uses all six LED outputs to drive 6, 12, 18, 24, etc LEDs

• Lights a single LED (or set) at a time• Programs can create many different

effects!

3737

PICAXE PIC37

#14 - Strobe light#14 - Strobe light'School Bus-top Strobe'd. bodnar 3-28-13' looks best when used with very bright' white LED driven by a 2n2222

#NO_DATASymbol LED1 = c.2Symbol LED2 = c.0Symbol Dlay = 5top:

high led1pause Dlaylow led1pause 100high led1pause Dlaylow led1

pause 400

high led1pause Dlaylow led1pause 100high led1pause Dlaylow led1pause 1000

high led2pause Dlaylow led2pause 100high led2pause Dlaylow led2

pause 400

high led2pause Dlaylow led2pause 100high led2pause Dlaylow led2pause 1000goto top:

3838

PICAXE PIC38

High power LEDsHigh power LEDs

• 2N2222 on boards will handle ½ amp for a short time

• Replace with Mosfet or TIP101 for higher current LEDs or bulbs

3939

PICAXE PIC39

#15 – Small Motor Control#15 – Small Motor Control

• Bypass 470 ohm resistor (why?)– Too little current allowed with it to drive motor

• Add diode (why?) – Back EMF will eventually kill the transistor

4040

PICAXE PIC40

AnimationAnimation• By sending precisely timed pulses the

PICAXE can set the servo’s arm to a specific position and hold it there.

4141

PICAXE PIC41

#16 - Servo Control#16 - Servo ControlMust add 3 pin cable (+5, ground, signal)

'd.bodnar 01-19-14 SYMBOL CCWMax = 240SYMBOL CWMin = 60 SYMBOL Middle = 128SYMBOL Cervo= b.2 ‘ Why not call it Servo – why use a misspelled word?SYMBOL CdS = b.4 'pin 9Symbol Light = b4servo Cervo,Middle ; initialize servo pin 8sertxd ("this is a test of the servo unit on new Multi-LED board - LED1 pin",13,10)

again:readadc CdS, Lightfor b2=CWMin to CCWMax

servopos Cervo,b2sertxd ("Servo ",#b2,13,10)pause 50

next b2

for b2=CCWMax to CWMin step -1servopos Cervo,b2sertxd ("Servo ",#b2,13,10)pause 50

next b2GoTo Again:

4242

PICAXE PIC42

#16 - CDs Light Sensor#16 - CDs Light Sensor

• A Cadmium Sulfide Photocell can be attached directly to the trigger

• Use to sense motion or to turn things on/off as ambient light changes

• Also works with a phototransistor (more sensitive)