Tutorial 01 – The Layout of a Program

Writing a program in the PICAXE Programming Editor is easy and there are only a few guidelines to ensure that your code is easily readable. 

A Program start at the top and progresses downward as the instructions are executed. Normally the following program layout are followed:

    • Initialising
    • Start
    • Program Body
    • End
    • Sub Procedures


In the Initialising part of the program all the preparation is done before the program actually starts. This includes the definition of variables, assigning values to constants, setting up ports and any other initialising that may be required. The initialising block can be identified with a Label “init:” so that we can jump to this part of the code when we need to do the initialising again. 

         symbol RED_LED=0                                      ;Rename port 0 to RED_LED

To indicate the starting position of a program a Label (“main:”) is used as a marker. The Label is entered as far left in the editor as possible followed by a colon (:), defining it as a Label. Labelling the start position of the program enables us to jump to the beginning of the program in code.

main:                                                                          ;This is a label called main

Program Body
The Program Body is just the normal program code space where the main program resides in.  

           high RED_LED                                             ;Switch on the red LED
           pause 500                                                     ;Pause 0.5 seconds (500ms)
           low RED_LED                                              ;Switch off the red LED
           pause 1000                                                   ;Pause 1 second (1000ms)
           goto main                                                      ;Loopback to the beginning

To end a program the “End” command is used. Sometimes it is not necessary to use an End command but is always good practice to do it. Normally the End command is used to stop program execution before the sub procedure code space preventing accidental execution of a sub procedure.

          End                                                                 ;The end of the main program

Sub Procedures
It is good practice to add sub procedures at the end of the main program. This insures better program flow and it is also easier to keep track of sub procedures and it eliminates accidental execution of sub procedures.

         End                                                                  ; stop accidentally falling into sub

         for b0 = 1 to b2                                             ; define loop for b2 times
                high 1                                                       ; switch on output 1
                pause 500                                               ; wait 0.5 seconds
                low 1                                                         ; switch off output 1
                pause 500                                               ; wait 0.5 seconds
         next b0                                                           ; end of loop
         return                                                             ; return from sub-procedure

To make the program more readable used enough empty lines between code blocks. If a piece of code belongs together don’t use empty lines in the block. It is also good practice to put enough tabs in after the code line before you add a comment. This separates the program code from comments and makes it easier to read. Also ensure that the comments are added in a vertical line and not in a zigzag format, it just makes it easier to read.

It is also good practice to indent code to indicate different levels in the code. An example is the for-next-loop as shown above where the code inside the for-next-loop is indented to make it easier to read.