Newbie’s guide into programming - 00000011 - First program and its structure – Picaxe

Teapot.jpg

   So far we’ve been talking theory, but now it is time to make things blink! You might have heard of “Hello World” term in programming. All that “Hello World” program does is output of “Hello World” text to standard output. This is almost the simplest program one can write. In the world of robots and microcontrollers there is its own variant of HW – the “Blink a LED”. Just like HW program for computers, “Blink a LED” does the simplest thing microcontroller can do while retaining all attributes of a program. That is the best opportunity for us to have a look on program structure of an actual code capable of performing some tangible task. From now on, I will split the topic into Picaxe® and Arduino chapters if I consider particular topic easier for the reader to conceive. Though, I recommend reading both variants, as some concepts will be discussed only in one of two chapters. So, as you might already understood from the title, this is the Picaxe® version of our topic. But before we continue, I want you to get hold of your main Picaxe friends: MANUALS! There are three main ones:

  • PICAXE Manual 1: Getting Started.
  • PICAXE Manual 2: BASIC Commands.
  • PICAXE Manual 3: Interfacing Circuits.

You can access them through Programming Editor’s Help menu. You might even want to print them out.

So, on page 6 of manual 1 (Quick Start) you can find following program:

main:

   high 4

   pause 1000

   low 4

   pause 1000

   goto main 

 

 

Before we start stripping this program to pieces and for the rest of our chapters, I’d like to ask you to read the part of the manual 2 dedicated to every new Picaxe keyword we encounter. For some time you will not understand all information given there, but soon enough all puzzle pieces will start to come together into greater picture.

Let’s start from a quick glance on our program. You will note that all text has some kind of structure and that words are of different colors. That is color syntax feature of IDE in work. It helps us to read the code by coloring each “part of speech” of programming language in different color.

Now, finally, let’s go for the program itself. First line we have is:

main:

Here, word “main” is a label. Now is good time to go ahead and read the PICAXE manual 2. For the first time I can even give you a hint, the page you should read about labels is page 5… Ok, you’re back. So, labels are like mile-posts on the road of execution of the program. For example if you need to fuel up and you know that there is a gas station on the 55th kilometer of the road, mile-post 55 is kind of label for gas station. I hope this was somewhat clear example, although I doubt it was. J So, labels let us mark a particular point of program and jump to that point from other place of program. Let’s move on:

high 4

                This is the first real instruction in our program. Now, you must have mentioned that it consists of two parts: the instruction word “high” and a parameter “4”. Instruction can take more than one parameter. In that case they are separated by a comma. Most instructions in PICAXE BASIC require parameters. Parameters used to specify more precisely how exactly instruction should function. Let’s have this example: When you use imaginary function “MakeMyselfASandwich” you probably want a particular sandwich, like cheese, ham, tuna, etc. Say, you want cheese sandwich. “Cheese” here is a parameter. If such instruction would exist in PICAXE BASIC, it would look like: MakeMyselfASandwich cheese.

Same thing you can see in the third line of our program:

pause 1000

Here “pause” is instruction and “1000” is parameter, and you already know what this parameter does. If you kept promise and read the manual, of course. J Next two lines should be easy to figure out. Finally, we come to:

goto main 

“Goto” instruction used to “jump” to another place in program. And you can see our “main” label again here. So, this instruction makes the microcontroller jump to place marked “main” in our program that is very beginning. By using construction

main:

…… 

goto main

you organize what I’d call “operation cycle” of microcontroller. Almost all of your future programs will use this structure. The Idea is that you place your code in the middle of this structure, and by that achieve continuous execution of that code snippet.

goto.png

To summarize: Picaxe Basic programs are always (so far) a single text and executed from beginning of text to the end. So, general Picaxe program would look something like this:

'Initialization instructions

'they are executed only once

'at the point the microcontroller

'is being powered up (or reset).

'....

 

main:

   'here is the operating cycle

   'instructions

   'this part of code will

   'be executed continiuosly

   'until mc is powered down

   goto main

 

See other chapters of this series here.

**Nice work, isotope. **

Nice work, isotope.

 

Simple and easy. thanks.

Simple and easy. thanks.

This is just what I’ve been

This is just what I’ve been looking for. An expansion of the manual

Good work.

A full 2 thumbs up from The Bromz

images.jpg

Thanks Isotope

Thanks for feedback!

I’m glad that this was useful to somebody. Unfortunately work on articles goes somewhat slowly, but more of them are coming :slight_smile:

This is good!

This is good!