Technical Article

Writing PICAXE BASIC Code - Part 2

September 08, 2015 by Charles R. Hampton

This is the second article in a multi-part series on writing PICAXE BASIC code. It introduces the for...next command, the wait command, general purpose variables, the symbol command, and the #no_data directive.

Recommended Level

Beginner

Prerequisites

Writing PICAXE BASIC Code - Part 1

This article introduces the for...next command, the wait command, general purpose variables, the symbol command, and the #no_data directive. Part 1 introduced the high, low, pause, and goto commands, the #picaxe directive, and the concept of labels. 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.

In addition, there is a newer version of PICAXE Editor 6; as of 3 September, 2015, the latest version is 6.0.8.1. This version still is in beta testing and corrects some issues in the previous version. You should download and install it before continuing: follow the instructions here.

Traffic Light Challenge

At the end of part 1, you were offered the challenge of taking the information in that article and using it to write code to make the LEDs follow the lighting pattern on one side of a traffic light as described below.

• Green flashes on/off 10 times for 10 seconds.

• Green on for 20 seconds, then off.

• Yellow on for 3 seconds, then off.

• Red on for 27 seconds, then off.

• Repeat continuously.

You were advised that no hardware changes to the PA-08M2 Coding Test Circuit were required.

If you succeeded in the challenge, you are hereby commended; if not, don't fret, just download the code below, program the PICAXE 08M2 in your coding test circuit, and watch it run. If you have a problem, check all the following possibilities.

• Your programming cable is connected from the PC to the coding test circuit.

• The correct COM port is selected in the Workplace Explorer Settings pane of PE6.

• PICAXE-08M2 is selected in the Workplace Explorer Settings pane of PE6.

• Power is connected to the coding test circuit breadboard.

Simple_Traffic_Light_1.zip

Program Analysis

The code you wrote may bear some similarities to the downloaded program, which, although long, is still quite simple. All text color references that follow are based on the defaults used in PE6.

Lines 1 through 13, reproduced below, consist entirely of comments, and are in green text. As you know, comments are never downloaded to the PICAXE, and are included purely for the benefit of humans who are reading the code. Taken in their entirety, these lines form a "header" that provides details about the code. The header in your code will be structured according to your preferences, and should include most of the same information.

Line 14 is a directive that instructs the compiler that the circuit contains an 08M2 µC, and is in blue text. Line 15 begins with a label; note that the label "main:" is used to identify a particlular place in the code, and labels are always in black text. Lines 16 through 19 use the high, low, and pause commands (in light blue text) to turn the green LED on for 1/2 second and off for 1/2 second.

Lines 20 through 55 repeat the same sequence as lines 16 through 19 nine more times, thus flashing the green LED on and off a total of ten times in ten seconds.

Lines 56 through 58 turn the green LED on for 20 seconds, and then turn it off. Lines 59 through 61 turn the yellow LED on for 3 seconds, and then turn it off. Lines 62 through 64 turn the red LED on for 27 seconds, and then turn it off. Line 65 uses the goto command to start the whole process over at line 15.

By now, you get the picture: the code is simply telling the microcontroller what to do one step after another. And that's the first mark of good code...step-by-step instructions in logical order.

"But," you might say, "surely, there's a better way than repeating lines 16 through 19 over and over. And isn't there some way that I don't have to remember that the green LED is on pinC.2? And why does the code take so long to download?"

All are good questions, and they have answers. Just read on.

For...Next Command and General Purpose Variables

Repetitive actions in code are quite common, and there is more than one way to handle them. One way is with the for...next command, and the best way to understand it is by way of an example, shown below.

As you probably guessed (from the header,) this code is another way to achieve the Traffic Light Challenge. The first 15 lines are unchanged from the previous code, but line 16 is the first half of a for...next command, sometimes called a "for...next loop." It essentially says, "for 10 iterations, do what follows," and what follows are the four lines of code that turn the green LED on for 1/2 second, and off for 1/2 second. In operation, lines 17 through 20 are executed and are followed by the last half of the for...next command. The counter is incremented by 1 count, from 0 to 1, and "next" sends the microcontroller back to the preceding "for."  The process is repeated, and again the counter is incremented by 1 count, this time from 1 to 2. After 8 more cycles through the loop, the counter is at 10, which completes the for...next command, and execution of the code moves to line 22.

Lines 22 through 24 turn the green LED on for 20 seconds, and then turn it off. Likewise, lines 25 through 27 control the yellow LED, lines 28 through 30 control the red LED, and line 31 sends the code back to line 15. Simple, huh?

But what about that counter that's keeping up with how many times the for...next loop has been executed? Where exactly is it?

All PICAXE microcontrollers have some memory set aside for use during program execution; those memory locations are called "general purpose variables." The PICAXE 08M2 has a total of 28 bytes of memory for general purpose variables, and the bytes are designated b0 through b27. In lines 16 and 21 of the code shown above, b4 is identified as the byte where the number of for...next loops is stored. Purple text is used for general purpose variables.

Note that the 28 bytes may be used in combination to form up to 14 words, and the first four bytes (b0 through b3) may each be addressed one bit at a time. But, however the memory is allocated, only 28 bytes in total are available for use as general purpose variables in a PICAXE 08M2. A future article will go into general purpose variables in greater detail.

Symbol Command, #No_Data Directive, and Wait Command

The code below is the third variant of the Traffic Light Challenge program. It has been modified in order to make use of the symbol command, the #no_data directive, and the wait command.

Lines 1 through 14 should be familiar to you and require no further explanation.

Line 15 uses a new directive: #no_data. If you have been watching the programming progress carefully, you may have noticed that there are actually two stages in the process. The first is "Downloading program" and the second is "Downloading data." You can see that in the Compiler tab of the Workspace Explorer pane in PE6, and in the lower right of the the PE6 window under the Memory pane. In programs which do not contain any data, you can eliminate the "Downloading data" step by using the #no_data directive, thus significantly reducing the time it takes to download code. To see the difference, put an apostrophe in front of the #no_data directive in line 15 (which will turn it into a comment,) and program the PICAXE. Then, restore the functionality of the #no_data directive by deleting the apostrophe, and program the PICAXE again. You will see the difference in the programming time required. Note that there are times when the #no_data directive must not be used, and those occasions will be discussed in a future article.

Lines 16 through 21 demonstrate some different ways that symbols can be assigned. In everyday life, symbols are used to represent other things, and likewise, symbols can also be used to good advantage in PICAXE BASIC programs. In line 16, g_led is assigned to represent pinC.2, which is where the green LED is connected. Likewise, in lines 17 and 18, y_led and r_led are assigned to represent the PICAXE pins where the yellow and red LEDs are connected. Lines 19 and 20 assign symbols to represent numbers. In this case, both symbols (dark and light) represent the same number (500,) but different numbers could just as well be used. Line 21 demonstrates how a symbol can be used to represent a general purpose variable: counter for b4.

Symbols can make code easier to write, read, and to edit. Having a word or a mnemonic to represent a number is a time-tested way of improving recall. In addition, when there are multiple appearances of the same item, modifying the symbol definition can save searching through every line of code. Perhaps best of all, you can choose your own symbols. Except for a few "reserved" words, your imagination is the only limit.

Lastly, a new command is introduced in line 30: wait. Like pause, wait stops the microcontroller for a certain period of time, but whereas "pause" is denominated in milliseconds, "wait" uses full seconds.

Download the Code

The finished code is available for download below. Program your PICAXE with it, watch it run a few times, and then see what improvements you can make.

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

Simple_Traffic_Light_3.zip