Technical Article

Writing PICAXE BASIC Code - Part 3

September 10, 2015 by Charles R. Hampton

Part 3 introduces the if...then, endif, gosub, and return PICAXE BASIC commands. It is the third article in a multi-part series on writing PICAXE code.

Recommended Level



Writing PICAXE BASIC Code - Part 1

Writing PICAXE BASIC Code - Part 2

This is the third article in a multi-part series on writing PICAXE BASIC code. It introduces the if...then, endif, gosub, and return commands. Part 1 introduced the high, low, pause, and goto commands, the #picaxe directive, and the concept of labels. Part 2 introduced the, wait, and symbol commands, general purpose variables, and the #no_data directive.

Prior to continuing with this article, completion of part 1 of this series is required. Part 1 provided complete details for the construction of the PA-08M2 Coding Test Circuit, which is essential to completion of this article. The schematic diagram is shown below for reference purposes.

Pushbutton Switches

Switch SW1 is a pushbutton momentary action switch, which means that the switch is operated for only as long as the operating button is held pressed. It is also a normally open (N.O.) switch, which means that it is open until the button is pressed, at which time the contacts close, allowing current to flow. To a certain extent, the symbol for SW1 in the schematic diagram reflects these characteristics.

The function of SW1 is to connect pinC.3 of the PICAXE 08M2 to ground while the button is pressed; the function of R6 is to allow a small amount of current to flow from the +5V supply to pinC.3. If R6 were not in the circuit, pinC.3 might operate correctly...or not. This is because, without R6, pinC.3 would not be connected unless SW1 was operated, but rather would be left "floating--" that is an undetermined logic state. Thus, the µC could interpret pinC.3 to be at a logic high or logic low, depending upon whatever unintended voltage might be on pinC3. And, in case you were wondering, VR1 plays no part in the operation of SW1 despite the fact that it is nearby in the schematic diagram. VR1 will be discussed in the next article in this series.

So, the net result of all this is that when SW1 is pressed, pinC.3 will be at a logic low or 0 (zero,) and when SW1 is released, pinC.3 will be at a logic high or 1.

Lighting an LED by Pushing a Button - Program Analysis


This short program is very easy to understand. In fact, lines 15, 17, and 21 are the only three lines about which you should have any questions; every other line has been throughly explained in part 1 or part 2 of this series of articles. If you are at all unclear about any of them, you should stop and review previous lessons.

Line 15 is just another symbol command with one slight difference; the symbol "p_button" will be used in a what is called an "if...then" command, as you can see in line 17. The comment very well descibes what the command means: if p_button (that is pinC.3) is low, the microcontroller executes lines 18 and following. If pinC.3 is not low, the program moves to the endif command (skipping lines 18, 19, and 20) and line 22 sends it back to "main:" to run again.

But that doesn't really explain why assigning a symbol (p_button, in this case) to pinC.3 requires the use of the long designation instead of just C.3. The PICAXE manual says, "When using inputs the input variable (pinC.1, pinC.2 etc) must be used (not the actual pin name C.1, C.2 etc.) i.e. the line must read ‘if pinC.1 = 1 then...’, not ‘if 1 = 1 then...’," but that explanation still explains only the "what" and not the "why."

Asking the question at the PICAXE forum produced a more complete answer: "...the pin designations such as C.3 are pre-defined constants. The test in an IF...THEN command must test a variable against a constant or another variable." Don't be bothered if you don't totally get the "why:" just remember to do the "what" as it's shown in the code. Here's the file for you to download and run.

Toggling Between Two LEDs by Pushing a Button - Program Analysis


Lines 1 thorugh 15 contain nothing new, and should be completely understandable without additional explanation.

Line 16 is the standard if...then statement; its purpose is simply to monitor the status of SW1. When the switch is pressed, a ground (logic low) is applied to pinC.3, and the microcontroller recognizes that as a logic 0 (zero.) The result is that bit 16 is incremented by 1 in line 17, and because a single bit can have only two states, each time bit16 is incremented, its value toggles between 0 (zero) and 1. Line 18 contains a pause command which stops the µC for a period of 100 milliseconds to allow the contacts of SW1 to stop bouncing and come to rest.

For an excellent explanation of switch bounce and how to deal with it, read this article by Jens Christoffersen. There, you will find that introducing a short delay is not the best way to overcome bouncing switch contacts, but it is the simplest, and is sufficient for this program. Line 17 is the endif command for the if...then command in line16.

Line 20 begins another if...then command by examining the state of bit16, and line 21 introduces the gosub command; gosub is short for go subroutine. If bit16 is at a count of 1, then the gosub command sends program execution to the subroutine labeled "green:" that begins at line 26. Lines 27 through 29 turn the green led on, the yellow led off, and pause for 100 milliseconds, after which the return command in line 30 sends program execution back to the first line of code following the gosub green command.

Line 22 is the first use of the else command, which indicates what the microcontroller should do if the previous if...then statement was not true. It is followed in this case by another gosub command, which sends the program to the subroutine labeled "yellow:" in line 31. Lines 32 through 34 turn the yellow led on, the green led off, and pause 100 milliseconds, after which another return command sends the program execution back to line 24, which is the endif command for the if...then command in line 16. At this point, goto main: in line 25 starts the entire SW1 checking sequence again.

At this point, you should be able to visualize what will happen as this code runs and the button is pushed and released or pushed and held down; think it through, then download the code file, run it and see if you were correct.

Final Thoughts

In the next article in this series, you will make use of the potentiometer, VR1, to control an LED chaser. The readadc command will be introduced and& explained, and the select, case, and endselect commands will be covered. In the meanwhile, write some code of your own; you have the knowledge and the skills...use them.

Next Article in Series: Writing PICAXE BASIC Code - Part 4