HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite Tutorial Table of Contents

Pascalite Programming: Second program

This version of this tutorial is fine tuned for a Pascalite, or the free software Pascalite emulation. Please send me an email with "complaints" if something doesn't work!

If you have the open source FPC, aka Free Pascal or Borland's Turbo Pascal (TP hereafter), then there's a separate tutorial for you, covering much of the ground as presented here for the Pascalite crowd.



Welcome. Here we go...

Fire up your Pascalite software. Enter the following program....
program second;
begin
write(lcd,255);
repeat
write(lcd,'Hi');
until 4=5;
end.
... and start the simulator and (click green arrow, remember?) run the program. You should see HiHiHi... appear on the simulated LCD. Before the screen gets very full, try clicking on the black arrows either side of the speed indicator, which is probably saying "Med" (for medium) at the moment.

The write(lcd,255) line clears the LCD screen. If you said write(lcd,65), an A would appear on the screen, 65 being the ASCII code for "A". Notice there are no apostrophes around the 65. If you wrote write(lcd,'65'), then you would get 65 on the LCD. 255 is what programmers call a rogue value. It is special; it isn't treated like other numbers. Its meaning to the LCD unit is "Clear the screen". (It isn't a rogue value in general, only when sent to the LCD).

The word Repeat is one of the rare instances of something that is not followed by a semicolon, like Begin, discussed in the first tutorial. "Repeat" is always paired with an Until. The Until is always followed by something which is either true or false. The "something" is called a condition, in this case "4=5". 4 doesn't equal 5, of course, so the condition is false. You will soon meet more complex (and sensible!) conditions. (People with some experience of programming may wonder why I didn't say "until false". Pascalite won't accept this, even though "false" is acceptable in other circumstances.)

Our program, because of the "repeat/ until" pair will perform the "write(lcd,'Hi');" instruction over and over again. This is called an infinite loop. Loops are powerful and useful, though you rarely make infinite loops... on purpose! Some bugs are based on inadvertent infinite loops.

Modify the program so that it says
program second;
begin
repeat
write(lcd,255);
write(lcd,'Hi');
until 4=5;
end.
(The word "repeat" has been moved up one line. You can do this by obvious means, but you can also do it by selecting the text, and then dragging it to where you want it!)

When you've made the change, and re-run the program, it almost looks as though "nothing" is happening. All right, the "Hi" appears and disappears.... but that's not much, is it? None-the-less, the program is doing write(lcd,255); and write(lcd,'Hi'); over and over again. Notice the big effect on the results that arises from moving the "repeat" just one line. You should be able to see why the program behaves differently....
Pascalite, being written for a specific piece of hardware, has a few special features not found in more general Pascals. As the hardware is a microcontroller, the usual way you give it input is more basic than is usual on a PC.... but don't worry! We'll talk more about input later, and our first example of Pascalite input is actually easier to understand than typical PC input. This is also the moment that you learn about the if... then... structure.

Change the line in your program that says....
write(lcd,'Hi');
... so that it says...
if b0 then write(lcd,'b0 true');
N.B.: That's b-zero, not b-oh.

Add, just after the previous...
if not(b0) then write(lcd,'b0 false');

... and run the program again. (F2 and click green arrow).

Once the program is running, click on the virtual toggle switch labelled b0. It is near the middle of the simulator panel. The message that is flashing on the LCD panel should be "b0 true" when the switch is down, and "b0 false" when the switch is off.

If you are lucky enough to have a real Pascalite, the LCD panel is an optional extra. We'll deal with getting around the need of it in a minute. The real equivalent of the virtual switch b0 is very, very simple: An ordinary toggle switch. The wire from one side goes to one place on the Pascalite, the other side gets a wire and a resistor connected. That wire connects to a second point on the Pascalite, and the other side of the resistor to a third point on the Pascalite. Obviously, you may need a little more detail, but I hope that persuades you that the hardware is simple.

By the way.. a little aside: the word "then" has at least two meanings in English. It can be a matter of sequence: I put on my socks, THEN I put on my shoes. Or it can be a matter of consequence: If you eat three pizzas, THEN you will be sick. While time comes into that, too, I hope you'll see the more significant "consequence" element. In programming, "then" is used in the consequence sense.

There's nothing "wrong" with our program as it stands, but programmers often want to choose between two alternatives, and have been given the following shortcut. The two "if..." lines can be replaced with....
if b0 then write(lcd,'b0 true') else write(lcd,'b0 false');
Did the author of Pascal anticipate the internet? I ask that because I don't know how the line above appears on your screen.... and, I'm glad to say, it doesn't matter! If your browser window isn't narrow, from "if..." to "...false');" is perhaps on a single line, and it is logical to write it thus in the Pascalite editor. However, it is also acceptable to write it as two lines.... (make your browser window wide enough to reduce the following to two lines)...
if b0 then write(lcd,'b0 true')
   else write(lcd,'b0 false');
Note how I have indented the second line. It is optional, but it makes reading through your program easier, because it emphasizes the fact that the "else..." line is just a continuation of what started on the previous line. Everything from the "if..." to the "...false');" is ONE statement. The concept of what is a statement is important in Pascal, and gets subtle in more complex cases. The big thing to notice here is that there is no semicolon before the semicolon. The concept of statements is a little like the concept of sentences in everyday English. Both of the following are valid (regardless of where you break the lines, as in Pascal), and comparable to single statements in Pascal:

I will go to town.
I will go to town if it is raining.

It is incorrect to write:

I will go to town. If it is raining.

Pascal's semicolon is a bit like English's period (full stop). Because so many lines in Pascal end with a semicolon, and because putting one before an "else" causes problems, and because if...then...else statements are usually split across two (or more) lines, I still alter similar lines in my programs as follows:
if b0 then write(lcd,'b0 true') {no ; here}
   else write(lcd,'b0 false');
The text between the curly brackets is ignored by Pascalite. The chance to add comments thus can be a real help to the programmer. Comments are also called remarks, or rems. In other dialects of Pascalite, comments can be included thus...
(*This is a rem*)
... or
//All of the rest of this line is a rem

Just before I end this tutorial, let's lift our eyes from the mud in front of our feet on this trek towards programming proficiency, and gaze at more interesting projects in the middle distance. Suppose you had your Pascalite wired to your six disc CD player, and you wanted to be able to use switches (like b0) to select which CD will play. Of course, you could simply use six switches... but with a little cleverness a mere three will do. I the following, a 0 stands for "switch in off position", and a 1 stands for "switch in on position" Each group of 3 digits shows the settings of the three switches.


000 could stand for "play CD1"
001 could stand for "play CD2"
010 could stand for "play CD3"
011 could stand for "play CD4"
100 could stand for "play CD5"
101 could stand for "play CD6"

... and you's still have 2 codes spare! If you want to switch between 256 options, a mere 8 switches is enough. Cool?




The work so far may have seemed "boring", but you have to walk before you run. If you're not determined (stubborn?) enough to slog through this dull stuff, you're probably not temperamentally suited to programming, anyway. But that's okay: Higher wages for those of us who are! Even if you aren't looking for wages, hang in there? It does get fun!




Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers. The Google advertisers pay me. I know nothing about them or their services, of course.)



Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....