picaxe sessions 1 a - pandprr.com sessions 1 b.pdf · picaxe examples: objective: • gain an...

21
PicAxe examples: Objective: Gain an insight into how a single chip might be used in a multiple task environment. Observe the components common to all the circuits. Identify the program/pin0 jumper. Identify the programming jack. Locate the power and ground solder pads. Identify the current limiting resistors. Locate the two resistors that MUST be included in all PicAxe projects. Locate the capacitor that reduces outside electronic noise. Activity: Operate and examine the following already prepared circuits Traffic light (PicAxe 08M (2)) o Observe the three LED connections to the PicAxe o Check the reverse side to see where the 0 volts connection is made. Chaser (PicAxe 08M (2)) o Four LEDs o Notice that pin 3 is skipped Welder (PicAxe 08M (2)) o Very simple wiring Servo (PicAxe 08M (2)) o The servo has three wires. Identify where they are connected. Lighthouse (PicAxe 08M (2)) ! Simple wiring Motor (PicAxe 08M (2)) o Notice where the motor is connected o Check what pins connect to the two buttons. Dice (PicAxe 08M (2)) o Commercially produced as a kit ! It doesn’t include the program ! It controls 7 LEDs with 4 output pins Huummm. Chip tester A (PicAxe 08M (2)) o Uses lights to check both input and output pins Chip tester B (PicAxe 08M (2)) o Variation on a theme Audio (PicAxe 08M (2)) o Chip tester that plays a built-in song Count up/down (PicAxe 18M (2)) o Uses a larger PicAxe chip for more output options.

Upload: lecong

Post on 01-Jul-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

PicAxe examples: Objective:

• Gain an insight into how a single chip might be used in a multiple task environment.

• Observe the components common to all the circuits. • Identify the program/pin0 jumper. • Identify the programming jack. • Locate the power and ground solder pads. • Identify the current limiting resistors. • Locate the two resistors that MUST be included in all PicAxe projects. • Locate the capacitor that reduces outside electronic noise.

Activity: Operate and examine the following already prepared circuits

• Traffic light (PicAxe 08M (2)) o Observe the three LED connections to the PicAxe o Check the reverse side to see where the 0 volts connection is made.

• Chaser (PicAxe 08M (2)) o Four LEDs o Notice that pin 3 is skipped

• Welder (PicAxe 08M (2)) o Very simple wiring

• Servo (PicAxe 08M (2)) o The servo has three wires. Identify where they are connected.

• Lighthouse (PicAxe 08M (2)) ! Simple wiring

• Motor (PicAxe 08M (2)) o Notice where the motor is connected o Check what pins connect to the two buttons.

• Dice (PicAxe 08M (2)) o Commercially produced as a kit

! It doesn’t include the program ! It controls 7 LEDs with 4 output pins Huummm.

• Chip tester A (PicAxe 08M (2)) o Uses lights to check both input and output pins

• Chip tester B (PicAxe 08M (2)) o Variation on a theme

• Audio (PicAxe 08M (2)) o Chip tester that plays a built-in song

• Count up/down (PicAxe 18M (2)) o Uses a larger PicAxe chip for more output options.

Page 2: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

The basic programming process.

Objective: Topics:

• Introduce the PicAxe 08M (2) chip and define the difference between legs and pins.

• Identify the role of each pin on the 08M(2). • Emphasize the importance of using no more than 5 VDC with the PicAxe. • Evaluate the values needed for current limiting resistors when using LEDs.

o Ohms law o An LED works on 2.5 to 3.5 volts and no more than 20 milliamps.

• Introduction of the programming language: o Using program segmentation

! main: is used as a beginning statement in a program ! HIGH and LOW are used to turn pins on and off ! Pause xxx is used to slow the program so we can see the results

• The xxx is in milliseconds ! Goto main will send the program back to the label main:

• Introduce the symbol command o This goes before the body of the program and makes program

modification more fun. o Terms used for symbols are restricted to non-program terms. Examples

! Goto ! Pause ! Wait ! High ! Low

o Use copy and paste to avoid typos when using symbols

Page 3: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Objective:

• At the end of the first session the participant will have constructed and demonstrated a circuit containing a PicAxe 08M(2), current limiting resistors and three (3) LEDs, then use it to model a traffic light.

Activity:

• Using pre-constructed processing boards build a circuit • Breadboard a circuit to light an LED.

o Components needed:

! Breadboard ! PicAxe 08M(2) ! LED ! Resistor for LED ! Resistor(s) for PicAxe pull down of pin 2 ! Programming connector

• Write the program that blinks one of the LEDs (any one) • Program the PicAxe using the programming cable

o Remember the program/pin0 pin must be in the program position. • Fix it so it works. • Expand the circuit to three LEDs • Modify the program to turn three LEDs on and off • Vary the pause to get a realistic looking traffic light • Modify the pattern to get a blinking orange before solid orange • Mess around with the program.

Page 4: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Here is the first program!

Page 5: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Future Topics: (Note: ALWAYS reference the topic using the PicAxe manuals!!!) Manual #3 Microcontroller Interfacing Circuits is the PicAxe bible for how to really use the chip! It is divided into two segments, input and output. And that’s essentially all the chip does. When stuff is input it massages the input and then outputs something.

• Controlling a motor o The PicAxe can drive very small motors directly. Current cannot exceed

20 ma for any one pin and 100 ma for the entire chip. o All motors need a capacitor across the power connections to the motor o Reversing a motor needs special handling.

• Controlling an RC servo o They run on 5 volts

• Using transistors o Put a 1K resistor from the PicAxe pin to the Base of the transistor o A 2N2222 works really well as a switch, and they are cheap.

• Using switches o Voltage divider o Switch bounce

• Adding a relay o Drive with a transistor o Put a diode across the power pins

• Photocells • PWM (Pulse Width Modulation) to control the speed of a motor

Page 6: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Servos Model airplanes use a special kind of motor to move the control surfaces and control engine speed. Servos come in all sizes and prices from about $10 to $ LOTS. We will be using the first variety. Generally, the servomotor moves through an arc much as windshield wipers do. A limited number (I have never seen one) can do continuous rotation. What PicAxe can do is “talk” to the servo and tell it where in its arc it should be at any given time. All PicAxe chips know how to control a servo using a command called “servo”. It looks like this: servo pin, position Any output pin can be used and the position should be a number between 75 and 220. Like this: Servo 4, 100 When this line is encountered in the program PicAxe will send a command on pin #4 to tell the servo to go to a position designated as 100. Where that is physically is up to the tinkerer to determine. The manufacturer of the servo determines how long it takes for the servo to get to the position, but from one extreme to the other the travel takes less than a second. When writing a program for a servo, include a pause to allow time for the servo to get to the position. Like this: main: servo 4, 75 pause 2000 servo 4, 150 pause 2000 goto main This program will result in a windshield wiper action.

• Change the pause numbers to check it out • Change the position numbers and see what happens

o Keep the numbers between 75 and 220 o You can use numbers out of this range but too large or too small will

result in the servo hitting the stops which causes it to buzz. Sort of like

Page 7: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

turning the steering wheel to the lock and holding it there. Lots of noise and no action.

The pole climber on the layout near the digger uses a servo. Can you make a program that would work there? Just think of what you see as he climbs and descends. It would be a long program using the above method since every step both up and down would need two lines. One for the position and one for the pause between steps. There’s a better way! (Not the only way, of course) We could make program with a climb portion and a descend portion using a for/next loop. To use a loop the PicAxe needs to be able to count and store numbers. Memory locations in PicAxe are divided into byte and word locations. (Manual 2. Variables) There are 14 (or more) general-purpose byte variables. These byte variables are labeled b0, b1 etc... Byte variables can store integer numbers between 0 and 255 inclusive. Byte variables cannot use negative numbers or fractions, and will ‘overflow’ without warning if you exceed the 0 or 255 boundary values (e.g. 254 + 3 = 1) (2 - 3 = 255) However for larger numbers two byte variables can be combined to create a word variable, which is capable of storing integer numbers between 0 and 65535 inclusive. These word variables are labeled w0, w1, w2 etc... and are constructed as follows: w0 = b1 : b0 w1 = b3 : b2 w2 = b5 : b4 w3 = b7 : b6 etc... Therefore the most significant byte of w0 is b1, and the least significant byte of w0 is b0. (Manual 2. For Next Loops) for...next Syntax: FOR variable = start TO end {STEP {-}increment} (other program lines) NEXT {variable} - Variable will be used as the loop counter - Start is the initial value of variable

Page 8: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

- End is the finish value of variable - Increment is an optional value that overrides the default counter value of +1. If Increment is preceded by a ‘-’, it will be assumed that Start is greater than End, and therefore increment will be subtracted (rather than added) on each loop. Lets try an example using a servo. The goal is to make the pole climber “step” up the pole, pause a little while at the top, then rappel down the pole. main: ' first climb the pole for b0 = 70 to 220 step 10 ' store a number in "b0" that limits the servo to from 70

to 220 and move 10 steps at a time servo 4, b0 ' move the servo attached to pin 4 to the position

indicated by b0 pause 500 ' give the servo time to get to the next position next b0 ' repeats the loop until b0 = 220 wait 4 ' waits for the number of seconds listed ' now climb back down using bigger steps. for b0 = 220 to 70 step -30 ' the minus sign says the servo is to go the other way. the

30 makes the steps larger servo 4, b0 ' move the servo attached to pin 4 to the position

indicated by b0 pause 500 ' give the servo time to get to the next position next b0 ' repeats the loop until b0 = 70 wait 4 ' waits for the number of seconds listed goto main ' do it again

Page 9: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Transistors Reference page 6 of the PicAxe manual #3, Interfacing Circuits. In our sessions we will be using a 2N2222 transistor. It is inexpensive and seems to do the job. This is the transistor that is used a lot at the P&P. The Darlington Pair described in the manual does the same thing but requires less current from the PicAxe. Here are the details about the 2N2222 that you will need to identify the pin designations.

Notice that the legs of the transistor are called Emitter, Base, and Collector. The inside arrow points to the negative voltage connection. (For reasons of their own, electrical engineers refer to the flow of electricity from positive to negative, when the rest of us think of electricity as the flow of electrons. It seems reasonable that the electrons should be flowing from where they ARE toward where they are not. The negative terminal is where they are the positive is where they are going to).

Page 10: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

This is a typical circuit used for LOTS of things!

As an example, refer to page 12 of manual #3 and construct the circuit below on a breadboard. (parts are available at the club).

The diagram shows a solar motor, but any small motor will work. If you have a power supply that reads amps, (like the clubs unit) test the motor on 5 volts DC (the diagram shows 6 volts, but that’s another discussion) and read the current. Anything less than 0.20 amps will work fine.

Page 11: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Write a short program to turn the motor on for five seconds and off for two seconds. Use HIGH and LOW to turn the motor on and off. Remember to insert a pause after each so you can see the motor start and stop. main: high 1 pause 5000 low 1 pause 2000 goto main The motor goes one way only! Reversing is for another day! As an exercise, try other pins and timing. You can use “wait” in place of “pause” just look it up in Manual #2 Basic Commands. Questions you should answer about each:

• What are the numerical limits? • Why use one rather than the other?

The PicAxe is just counting to itself during a pause or wait. All other non-automatic operations are suspended until the time is up.

• Some non-automatic operations might include: o A servo moving to its assigned position o An LED stays on o A motor keeps running o A motor runs at an assigned speed

Page 12: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

PWM (Pulse Width Modulation) (Manual 2 / Basic Commands, page 165) PWM is used to control a motor speed or a lamp brightness. It functions by turning the power on and off very quickly where the amount of time that the power is on determines the motor speed or the lamp brightness. Imagine a motor that can be turned on by pressing a momentary switch and it only runs while the switch is pressed down. Holding the switch down makes the motor run continuously, but jiggling the switch quickly will cause the motor to rapidly start and stop. For us humans that would be a very jerky motion, but the PicAxe chip’s clock is running 4 million times faster than ours, so the periods come along quite quickly, giving the illusion (to us humans) of continuous power. The command has two variables, period, and duty. The period is the length of time in a cycle and the duty is how long the power is on during that cycle. Think of the period as a space, and duty as the “on” time.

Syntax: PWMOUT pin, period, duty cycles Pin is a variable/constant which specifies the i/o pin to use.

• (Note that the pwmout pin is not always a default output pin – see the pinout diagram for the PicAxe chip you are using).

- Period is a variable/constant (0-255) which sets the PWM period • (period is the length of 1 on/off cycle i.e. the total mark:space time). - Duty is a variable/constant (0-1023), which sets the PWM duty cycle. • (duty cycle is the mark or ‘on time’ )

Function: Generate a continuous pwm output using the microcontroller’s internal pwm module. Also see the HPWM command, which can produce the equivalent of pwmout on different output pins. Information: This command is different to most other BASIC commands in that the pwmout runs continuously (in the background) until another pwmout command is sent. Therefore it can be used, for instance, to continuously drive a motor at varying speeds. To stop pwmout issue a ‘pwmout pin, off’ (=pwmout pin,0,0) command.

Page 13: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

The PWM period = (period + 1) x 4 x resonator speed (resonator speed for 4MHz = 1/4000000) The PWM duty cycle = (duty) x resonator speed Here’s an Example

- use the same wiring as in a simple motor. - Pin 2 is the PicAxe output through a 1k resistor to the transistor Base - 2n2222 transistor

o emitter to 0 volts o collector to +5 volts

Symbol Duty = 700 'establish duty as a variable PWMOut 2, 249, Duty ' start with medium speed main: for w1 = 400 to 1020 step 20 ' start at a slow speed and accelerate 20 steps at a time PWMOut 2, 249,w1 ' the motor duty is the value of w1 in the for/next loop pause 200 ' delay so the speed change is noticed next w1 ' repeat the loop until w1=1020 for w2 = 1020 to 300 step -20 ' Slow down 20 steps at a time PWMOut 2, 249, w2 ' the motor duty is the value of w2 in the for/next loop pause 500 'delay for observation next w2 ' repeat the loop PWMOut 2, 0, Duty ' stop the motor pause 4000 goto main Just for fun, change the 1020 in the first loop (w1) to something not evenly divisible by 20, like 1025, and try it again. What did you observe? Hummm. Interesting!

Page 14: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Reversing an Electric Motor: Reversing a DC motor is easy! Just reverse the power leads so that the electricity runs the opposite direction, and the motor will follow. OK, that will work for a really brute force method, but it’s not really very practical. If the motor is to be switched manually it can be wired to a DPDT (Double Pole Double Throw) switch like this:

This elegant solution has stood the test of time but requires a person to move the switch lever. OK, maybe some “thing” could run into the switch and make it move, but that is inelegant! Using a PicAxe we could have the PicAxe turn a relay on and off to do the job. A relay uses an electromagnet to throw the switch just as a hand would do it. It looks like this:

Page 15: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

That looks more complicated and it still has a switch to throw! True, but a PicAxe can be programmed to throw the switch for you anytime you need the motor reversed. It works! But again, not very elegant. Enter the “H” bridge! Oh, no! Not more complications! Well, it is elegant and really very simple to wire, program, and use. Like the PicAxe, someone has taken an elegant idea and enclosed it in a “Black Box” with little legs for giving the chip instructions and getting results. The thinkers really got this one right as they looked at the problems motors can cause with their sparking and generating unwanted electricity when it isn’t needed, then included the fixes in the chip without our knowing how to do the hard stuff! My kind of thinking. Page 13 of PicAxe Manual #3 Interfacing Circuits goes through the basics of how to use the H Bridge with PicAxe. Here’s their diagram:

Two motors? Yep, in the same chip you can run and control two motors independently and at the same time. In the diagram they indicate 0V, which is the ground or negative terminal, and V2+ which is the voltage going to the motor. It can be any voltage from 3 volts to 25 or 30 volts! Remember, don’t feed the PicAxe any voltage over 5V! Since higher voltage can be used for the motor, the 0V of the power supply to the motor (the H bridge) MUST be connected to the 0V of the power supplied to the PicAxe. PicAxe uses this “ground” connection as a reference to tell the H Bridge what to do. When building the circuit on a breadboard, (there’s slideshow on the PicAxe section of the P&P site.)

Page 16: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

• Connect the motor power supply to the bottom of the right side of the board, plus to red and minus to blue

• Add a 5 V regulator of your choice to the board using the motor power supply as a power source, and connect its output to the left side of the board, plus to red and minus to blue.

• Install the H Bridge straddling the breadboards center trough. This provides two separate sides for the two motors. (for now we will ignore the second motor).

• Connect the plus and minus pins of the PicAxe 08M board to the 5V side of the breadboard (5V)

• Connect pins 1 and 4 of the PicAxe board to the pins labeled “Pin 4” and “Pin 5” of the diagram (legs 2 and 7 of the chip)

• Hook the motor into legs 3 and 6 (it doesn’t matter which wire goes on which pin).

• Finished with the wiring! • But where is the transistor that runs the motor?

o It’s inside the chip along with a lot of other stuff! Look it up on the web. Caveat: If the motor being used is a 5 -12 volt motor make sure that V2+ on the left side is connected to the 5V rail. When you power this puppy ON, it is going to give the motor the full V2+ voltage, and small motors will really SCREAM when connected to higher voltage! Here’s the manual description of how it works: Both inputs low - motor halt First output high, second output low - motor forward First output low, second output high - motor reverse Both inputs high - motor halt Try to write your own program with this in mind:

• Start with the off (halt) • Turn the motor on in forward • Wait for a while to get it running • Stop the motor • Pause a while • Reverse the motor • Wait while it gets running • Stop the motor • Wait • Do it again

A sample program is on the next page if you get stuck.

Page 17: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Main: high 2 ‘Turns the power on to the motor low 1 ‘ Put the motor in neutral low 4 Pause 1000 high 1 ‘ Put the motor in forward low 4 Pause 2000 low 1 ‘ Put the motor in neutral low 4 Pause 1000 low 1 ‘ Put the motor in reverse high 4 Pause 2000 low 1 ‘ Put the motor in neutral low 4 pause 1000 low 2 ‘ Turn the motor off goto main

Page 18: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

By now some of you have Googled the chip number, L293D, just to see what it’s all about. If you did, you may have noticed that pins 1 and 9 are labeled “en+” and wondered about the label. It stands for “enable”, as in turn it on. We had it wired to the + V pin so it was on all the time! That’s why the motor went to full speed when it started. What do you think would happen if the H Bridge pin 1 was connected to a PWM pin of the PicAxe just as we did with the motor connected through the transistor? Introduce the PWMOUT 2, 255, 400 in place of the high 2 line. (the 08M chip will do PWM only on pin 2). You can get fancy by borrowing some lines from the PWM routines you created earlier. Just copy and paste these lines to speed the motor up. for w1 = 400 to 1020 step 20 ' start at a slow speed and accelerate 20 steps at a time PWMOut 2, 249,w1 ' the motor duty is the value of w1 in the for/next loop pause 200 ' delay so the speed change is noticed next w1 ' repeat the loop until w1=1020 You’ll have to figure how to slow it down on your own! (Or you could discuss it with the rest of the group and really get things rolling!)

Page 19: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

So far we have been looking at Output. Next will be Input On pages 26 and 27 of the PicAxe manual they discuss switches and how to wire them. Here is an excerpt from the manual:

The diagram shows a 1K resistor between one leg of the switch and the PicAxe pin. This is important because the current flow must be very low. The other leg of the switch is connected through a 10K resistor to either +5 or 0. In the top diagram the PicAxe pin is connected through both resistors to 0 VDC. This is referred to as “pulling the pin low”, and the PicAxe will see the pin as LOW. When the switch is pressed the +5 VDC will be seen by the PicAxe and the pin will be “pulled” HI. And yes, when the switch is pressed there is a short circuit between the +5 and 0 but it is through a 10 K resistor so the current will be very small and not a problem. So, get two of each 1K resistors, 10K resistors and momentary switches. P&P has bunches of unused switches in the electronics cabinet across from the control panel in the layout area. And build a circuit pictured on the P&P site (Motor buttons.pdf) that will run the program shown in (Motor buttons software.pdf):

Page 20: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Print that page for reference in this next discussion. Read the program line by line (including the comments) to see how it operates. If you can follow every step of the program, ignore the next section. The first couple lines are familiar to you already. They get the motor running. “Top:” is the designation line “main:” that we have used. It is a place to which the program can be directed. Gosub sends the program to a block of commands called Flash, and when the word “return” is encountered the program will return to the end of the line that sent it on its way. Sort of a round trip ticket. Examine Flash. It simply blinks the LED attached to pin 0 once and then goes back. The program returns and “drops through” to the next line, where questions are asked about the status of the switches (plural!). Remember that the switched were wired to be pulled low when they are not pressed which yields a LOW state for the pin. First if the switch on pin 3 is AND the switch on pin 1 are both pressed they will read HIGH on both and the statement is TRUE, so the command on the rest of the line is executed. i.e. Then Cease which means goto the Cease sub routine and do whatever it says, but don’t come back! The command “goto” is understood in this line and can be omitted. (Programming 204 …. ;-) When we look at cease we can see that it just turns off the PWM at pin 2 by making the period 0. Remember that the period is the size of the frame in which the duty is carried out. No frame, no power to the motor. And then it sends the program back to “Top”. That’s not quite like starting over because it doesn’t include the original motor start that happened with the first run of the program, so the motor remains stopped. The next two lines look for a single button push and react accordingly. If no buttons are pushed the program “falls thru” to the next command which is “GoTo Top” AS you can see the program jumps around a LOT! Every time the LED flashes the program has run all the switch checks and New terms are: Goto and Gosub. These terms direct the program to a block of code that does a specific task, like blink a LED, increase the speed of a motor, etc. Goto is a one-way-trip and once the instructions have been carried out the operation continues to the next set of instructions. If there are none, the program stops!

Page 21: PicAxe Sessions 1 A - pandprr.com Sessions 1 B.pdf · PicAxe examples: Objective: • Gain an insight into how a single chip might be used in a multiple task environment. • Observe

Gosub directs the program to a routine that ends in the command “return”, which send the program back to the line that sent it there and the program continues. IF … THEN is used to test to see if something is true. If the answer is true action described after the word THEN is performed. If the answer is false the instructions after THEN are ignored and the program “drops’ through to the next line. Example: If pin3 = 1 then goto XXXX This checks to see of a switch connected to pin 3 is pressed. When pressed the value is 1 otherwise it is 0. AND between IFs means that all of the test results must be true for the THEN to be executed. (There can be multiple ANDs between IF and THEN).