Return to Basics

73
Return to Basic Reference Manual Gordon Henderson [email protected] c 2012 January 27, 2013

description

programing manual

Transcript of Return to Basics

Page 1: Return to Basics

Return to BasicReference Manual

Gordon [email protected]

c©2012

January 27, 2013

Page 2: Return to Basics

Preface

Return to Basic (RTB) is a project to re-create a modern version of the BASIC inter-preter and programming environment popular in the late 1970s through the 1980s onthe popular 8-bit microprocessor systems such as the Apple ][ series, BBC Micro andCommodore PET as well as the various Sinclair systems and a whole host of otherstoo numerous to mention. The intention is to present an easy to use environment foryoung (and old) people to learn to write computer programs using an interactive systemthat’s quick and easy to use.

BASIC (Beginners All-purpose Symbolic Instruction Code) is regarded as some-what old-fashioned in todays world (c2012), but I believe it still has a place in theteaching and learning of computer programming. It arguably has many faults and hasbeen criticised for encouraging bad programming practices, however RTB is a newimplementation, capable of using modern structured programing techniques includingnamed functions and procedures (which support recursion, if desired) and structuredlooping constructs. Also, while this manual demonstrates the use of the GOTO instruc-tion it does discourage it and quickly moves on to demonstrating newer techniques.You can even write RTB programs without line numbers and merge library files of pro-cedures and functions together.

RTB is not intended to be used as a serious programming system – While it hasthe capabilities to write a large financial package in, I do not expect people to writethe many, varied and large packages that were commonly written in BASIC in thoseearly days, but rather to be used as a way to introduce programming in an easy tounderstand manner.

RTB features several graphical systems that were popular in those early micropro-cessor years – both a low and high resolution colour graphics system as well as “turtle”graphics with simplified colour and angle handling. It is more than capable of beingused to write simple games and animations in.

I would like to think that pre-teen children can be introduced to programming usingRTB – the only prerequisite is a desire to learn and the ability to type some simpleinstructions into a computer. It doesn’t even need a mouse!

This manual is intended to be used as a reference manual for RTB. It lists all themajor programming constructs with a small number of examples.

Gordon Henderson, February 2012

i

Page 3: Return to Basics

Trademarks and Acknowledgements

Raspberry Pi is a registered trademark of the Raspberry Pi Foundation

Warranty and Copying

* This document is part of RTB:

* Return To Basic

* http://projects.drogon.net/return-to-basic

*

* RTB is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* RTB is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with RTB. If not, see <http://www.gnu.org/licenses/>.

ii

Page 4: Return to Basics

Contents

Preface i

Trademarks and Acknowledgements ii

Warranty and Copying ii

1 Introduction 1

1.1 Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Conventions Used in this Manual . . . . . . . . . . . . . . . . . . . . . . 1

2 The RTB environment 3

2.1 A quick introduction to BASIC . . . . . . . . . . . . . . . . . . . . . . . . 4

2.2 Immediate mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.3 Run mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.4 Stored program mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.5 BASIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 The command-line 7

3.1 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.3 Editing program lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4 Immediate mode commands 9

4.1 File names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

iii

Page 5: Return to Basics

5 Comments 11

6 Flow Control 12

6.1 GOTO and GOSUB – The great controversy . . . . . . . . . . . . . . . . 13

7 Variables 14

7.1 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

7.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

7.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

7.4 Numeric operators and Precedence . . . . . . . . . . . . . . . . . . . . 15

7.5 String Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

7.6 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

7.7 Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

8 Input and Output 17

8.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

8.2 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

9 Data, Read and Restore 20

10 Built-in Variables and Constants 23

10.1 Read only built-in variables . . . . . . . . . . . . . . . . . . . . . . . . . 23

10.2 Constants representing colours . . . . . . . . . . . . . . . . . . . . . . . 24

10.3 Constants representing Keyboard Keys . . . . . . . . . . . . . . . . . . 24

10.4 Read/Write built-in variables . . . . . . . . . . . . . . . . . . . . . . . . . 24

11 Conditionals: IF . . . THEN . . . 26

11.1 Multiple lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

11.2 IF . . . THEN . . . ELSE . . . ENDIF . . . . . . . . . . . . . . . . . . . . . . . 27

12 Conditionals: SWITCH and CASE. . . 29

iv

Page 6: Return to Basics

13 Looping the Loop 32

13.1 CYCLE . . . REPEAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

13.1.1 UNTIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

13.1.2 WHILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

13.2 DO . . . REPEAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

13.3 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

13.4 Breaking and continuing the loop . . . . . . . . . . . . . . . . . . . . . . 36

14 Built-in Procedures and Functions 37

15 Numerical Functions 38

16 String Functions 40

17 Graphical Procedures and Functions 41

17.1 System Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

17.2 General Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

17.3 Cartesian Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

17.4 Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

17.5 Sprite Procedures and Functions . . . . . . . . . . . . . . . . . . . . . . 44

18 Writing your own Procedures and Functions 45

18.1 User defined Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 45

18.2 User defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

18.3 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

18.4 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

A Colours 50

A.1 A Text Colour Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

B Numbering Names and Formats 52

v

Page 7: Return to Basics

C File Handling functions 53

C.1 Sequential Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

C.2 Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

C.3 RTB Instructions and functions . . . . . . . . . . . . . . . . . . . . . . . 54

D Serial Port Programming 56

D.1 SOPEN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

D.2 SCLOSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

D.3 SGET, SGET$ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

D.4 SPUT, SPUT$ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

D.5 SREADY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

E Raspberry Pi - GPIO Programming 58

E.1 PinMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

E.2 DigitalRead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

E.3 DigitalWrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

E.4 PwmWrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

F DRC - Drogon Remote Control Programming 60

F.1 DrcOpen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

F.2 DrcClose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

F.3 DrcPinMode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

F.4 DrcDigitalRead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

F.5 DrcDigitalWrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

F.6 DrcAnalogRead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

F.7 DrcPwmWrite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

vi

Page 8: Return to Basics

Chapter 1

Introduction

1.1 Audience

This manual is aimed at people who may already have a familiarity with BASIC or otherhigh-level languages. It’s mainly a reference manual for the language, but that are afew examples and tutorials.

The manual will assume that you understand most common principles of computingand know concepts like volatile and non-volatile storage and so on and how to typesome commands into your computer.

And although it’s a reference manual, there are one or two little examples and tuto-rials along the way for you to try out.

1.2 Conventions Used in this Manual

Keeping things simple is the aim here, so there are only really 3 things to look outfor1. The first is that anything you may need to type into a computer running RTB, oranything it prints will be in this typewriter style font. The second is that anything ofsome importance, such as a name of an algorithm is emphasised like this.

The final thing to look out for is a warning, or just something that may be important!to remember. It’s represented by a warning exclamation point in a box to the side ofthe text. As demonstrated in this paragraph.

1OK, Possibly four as there may be the occasional footnote at the bottom of a page, so look out forthem too.

1

Page 9: Return to Basics

CHAPTER 1. INTRODUCTION 2

If you know what you’re doing, here are some brief differences between RTB and a“classic” BASIC:

• RTB does not allow multiple statements on one line. One statement per line only.

• Variable names can be of almost any length and upper/lower case is significant.

• IF statements must have THEN (So no IF... num = 4, or IF... GOTO 10, it mustbe the full IF... THEN GOTO 10, or IF... THEN a = 7, etc.)

• In FOR loops, there is no NEXT instruction – it’s replaced by the CYCLE...REPEAT

construct.

• Named procedures and functions. (Which can be called recursively)

• Local variables inside user-defined procedures and functions.

• A single looping construct (CYCLE...REPEAT) which can be modified with FOR,WHILE and UNTIL constructs.

• BREAK and CONTINUE as part of the looping construct.

• Arrays start at zero and go up to and include the number in the DIM statement. ie.the DIM statement specifies the size of the array plus one.

Additionally (where supported) the graphical systems may well be new or different toyou. RTB incorporates simple block/line graphics as well as turtle graphics using avariety of angle modes (degrees, radians and clock).

Page 10: Return to Basics

Chapter 2

The RTB environment

RTB is primarily designed to run on computers running Linux. In the fullness of time,both MS windows and Apple Mac versions may be made available, but for now it’sLinux only.

On the Raspberry Pi, there are some additional features to make use of the Pi’sGPIO facility.

Starting RTB may vary from one Linux system another, however opening up a text(or command/shell) terminal and typing

rtb

at the prompt will usually get things going for you.

Whatever the system, once RTB has started it should look the same. There willbe a keyboard to type commands into, this is connected to the main computer whichshould be connected to a screen or display of some sort. A Raspberry Pi may even beconnected to your home TV set.

When it’s setup and ready, the screen should be clear apart from some introductorytext and a prompt. It will probably look something like this:

Return to Basic version 1.0

Ready

>

The “>” symbol is the prompt. It’s presence means that RTB is ready to accept com-mands and program lines typed into it.

3

Page 11: Return to Basics

CHAPTER 2. THE RTB ENVIRONMENT 4

2.1 A quick introduction to BASIC

(You may wish to skip this if you are familiar with BASIC)

BASIC and therefore RTB has three basic modes of operation: Immediate mode,program store mode and program run mode.

2.2 Immediate mode

This is when commands you type into the system are executed immediately. You canuse this mode to perform simple calculations, examine the state of variables and doother “housekeeping” tasks such as saving and loading your programs, and so on.

A few examples:

PRINT "Hello"

PRINT "My name is: Gordon Henderson"

Calculator:

PRINT 1+2

PRINT 1+2*3

A quick note about the calculator and symbols used. There isn’t a proper divide!symbol on your keyboard: ÷ so we use the forward-slash character instead: / andsimilarly rather than use the multiply character: × we use the asterisk: *.

2.3 Run mode

This is simply when a program is running. To start a stored program, use the RUN

command. The program will stop running when it encounters a STOP or END instruction,or when an error is detected. You can stop a running program at any time by using theESC1 key at any time.

1Short for Escape

Page 12: Return to Basics

CHAPTER 2. THE RTB ENVIRONMENT 5

2.4 Stored program mode

This mode allows us to store a program in the computers memory which we can thenexecute over and over again. We differentiate immediate mode from stored programmode by giving each line we type a line number at the start of the line.

To enter a simple program:

10 PRINT 1 + 2 * 3

20 END

Remember to press the ENTER key after every line.

To insert a line of code between two existing lines, you can use a line-number in-between the existing numbers, so using the example above, to insert a line we cantype:

5 PRINT "The answer to 1 + 2 * 3 is:"

Verify it with the LIST command – and note that the computer has added line 5 beforeline 10, which is what you expect because computers are boringly good at counting.Run the program again and note the output.

Add this line:

15 PRINT "Six times seven is: "; 6 * 7

list and run the program again.

A few final notes here: If you enter a new line with the same number as one alreadyentered, it will delete the one stored and replace it with the one you entered, or if youenter a line number and nothing else (just press ENTER after the line number, then it willdelete any line with that number.

It is also good practice to regularly save your work. Imagine the frustrations of!having a power cut which will lose all your work so-far. . .

2.5 BASIC

This is the essence of BASIC programming. You can enter commands to be executedimmediately, or enter program lines (with line numbers) which the computer will store

Page 13: Return to Basics

CHAPTER 2. THE RTB ENVIRONMENT 6

for as long as it’s turned on, or until you delete them by overwriting them, or using theNEW command.

List your programs with the LIST command and run them with the RUN command.

When entering new programs it is suggested to start at line number 10 and go upin 10s. That way, there is plenty of room to enter new lines in-between if required. (Butthere’s always the RENUMBER command if you run out of space)

Page 14: Return to Basics

Chapter 3

The command-line

The command line is the general term for typing commands and program lines into thesystem, however it has a few features to make your life easier.

3.1 Editing

As you type characters into the system, you may make mistakes. To correct them youcan use several differenet keys. The important one is probably the Backspace key.Usually a big key to the top-right of the main keyboard with an arrow pointing to the left.This will erase characters you’ve typed and move the cursor to the left, however thereare more efficient ways to deal with the like you’re typing noted below.

Some of the command characters lsited below you access with the Control key.This is labelled as Ctrl on most keyboards and works like the Shift keys in that youpush it, keep it pushed, then type another character before releasing them both.

Ctrl-A, Ctrl-E: Move the cursor to the start or the end of the line respectively. Youcan also use the Home and End keys if your keyboard has them.

← (Left) and → (Right): arrow keys (not to be confused with the Backspace key)move the cursor one character to the left or right of the typed line.

Ctrl-D: Delete the character under the cursor. You can also use the Del or Delete keyif your keyboard has one.

Backspace: Deletes the character to the Left of the cursor)

Ctrl-S: Swap the character under the cursor with the character immediately to theright. Handy for those who type the as teh like me. . .

7

Page 15: Return to Basics

CHAPTER 3. THE COMMAND-LINE 8

Ctrl-F: Find the next occurance of the next character typed. E.g. Ctrl-F followed byG will make the cursor jump to the next G in the line. Ctrl-F followed by anotherCtrl-F will repeat the last find.

Esc: Abandon entering this entire line.

3.2 History

The RTB command line remembers the past 50 lines that you type in, and you can usethe ↑ (Up) and ↓ (Down) arrow keys on your keyboard to scroll through past things you’vetyped in. This will enable you to quickly fix a mistake in a line already in the system, ora line you typed in which remoted an error after you pressed the Enter key.

3.3 Editing program lines

If you spot a mistake in a program line and you can’t find it in the history, then you canenter the ED command followed by the line number. E.g. ED 10 will then present line10 as if you had re-typed it, but not yet pressed Enter so you can change the line asrequired.

Page 16: Return to Basics

Chapter 4

Immediate mode commands

In immediate mode, as well as executing simple print instructions as well as otherinstructions you can use within a program, there are several commands which canonly be executed in immediate mode.

LIST [[first] last]: This lists the program stored in memory to the screen. You canpause the listing with the space-bar and terminate it with the escape key. If firstis given, then only that line will be listed. If both first and last are given, thenlines in that range will be listed.

SAVE [filename]: Saves your program to the local non-volatile storage. filename isthe name of the file you wish to save and may not contain spaces. If you havealready saved a file, then you can subsequently execute SAVE without the filenameand it will overwrite the last file saved. (This is “safe” as it’s reset when you loada new program or use the NEW command)

SAVENN [filename]: Same as the normal SAVE command, but saves with No line Num-bers. You can only save without line numbers if your program has no GOTO orGOSUB statements and no RESTORE statements with a line-number.

LOAD filename: Loads in a program from the local non-volatile storage. As with SAVE,you need to supply the filename without any quotes.

DIR [directory]: Lists the RTB files in your current working directory, or the givendirectory (without quotes)

NEW: Deletes the program in memory. There is no verification and once it’s gone, it’sgone. Remember to save first!

RUN: Runs the program in memory. You may give a line number and the program willstart from that number rather than the first line in the program. Note that usingRUN will clear all variables.

9

Page 17: Return to Basics

CHAPTER 4. IMMEDIATE MODE COMMANDS 10

CONT: Continues program execution after a STOP instruction. Variables are not cleared.

CLEAR: Clears all variables and deletes all arrays. It also removes any active spritesfrom the screen. Stopped programs may not be continued after a CLEAR com-mand.

TRON: Turns line-number tracing on. As each line is executed, it’s number is printed tothe text console that RTB was started from.

TROFF: Turns line number tracing off.

ED linenumber: Edit the line given.

RENUMBER [start [inc [first [last]]]]: This renumbers your program - by defaultit will start at 100 and go in increments of 10, however you can change this asfollows: The start and inc parameters specify the new starting line number andincrement, the first and last parameters specify the first and last exiting linenumbers to renumber. Using this latter way, you can move lines in the program,but beware of overlaps.

If an overlap does occur, then renumbering will stop at that point and you mayfind your program to be somewhat scrambled. . . Please make sure you save yourprogram before renumbering!

VERSION: Print the current version of RTB.

EXIT: Exit RTB and return to the environment you started RTB in.

4.1 File names

The system you run RTB on may have its own rules about what a filename can looklike, and whether the name is case-sensitive. (ie. UPPER/lower case letters – are theyconsidered the same, or different?) In the Linux environment then case is significant.If you’re not sure, just stick to simple names without spaces. If you’re subsequentlylooking at the files outside the RTB environment note that the filenames will have thecharacters .rtb appended to it.

Page 18: Return to Basics

Chapter 5

Comments

It’s always a good idea to include comments in your programs, and RTB provides 2methods to help you do this.

Firstly, there is the traditional REM statement. Short for “Remark”. Secondly there isthe // statement which is common in many other programming languages.

REM must appear at the start of a program line, but // may appear anywhere in aline - and anything after the // is ignored.

Examples:

100 REM This is a demonstration of comments

110 //

120 REM // on its own can be used to separate program sections.

130 //

140 // The line below is allowed:

150 LET test = 42 // Set test to 42

160 //

170 // But the line below this is not allowed:

180 LET test = 42 REM Set test to 42

11

Page 19: Return to Basics

Chapter 6

Flow Control

Programs are normally executed in line-number order, one after the other. There aremany ways in which we can alter the flow of our programs, but the traditional BASICones are the GOTO and GOSUB instructions.

GOTO as its name implys causes program execution to go to the line number listedafter the GOTO instruction.

120 GOTO 315

GOSUB1 allows a program to temporarily jump to a new point and then, upon execu-tion of the RETURN statement, flow resumes at the statement after the GOSUB instruction.

GOSUB is designed to be used to allow a piece of code to be executed over againfrom different parts of the main program.

Subroutines can call other subroutines and the number of subroutines that canbe called is limited only by the memory capacity of the computer - some of which isrequired to keep track of where to return to.

Here is an example – It’s code to print my name:

500 REM Simple subroutine to print my name

510 PRINT "Gordon Henderson"

580 RETURN

Then, anywhere we need to print my name:

100 PRINT "Hello ";

1Go to Subroutine

12

Page 20: Return to Basics

CHAPTER 6. FLOW CONTROL 13

110 GOSUB 500

120 PRINT "Good to meet you ";

130 GOSUB 500

140 GOTO 100

Subroutines are a good was of saving and re-using code however there are moremodern ways of doing this that removes the need to keep track of line-numbers.

6.1 GOTO and GOSUB – The great controversy

The use of GOTO and GOSUB is highly debated and both GOTO and GOSUB should beconsidered deprecated in their use. It is possible to write fully functional RTB programswithout using either these instructions.

The original version of BASIC did not provide any more means to control the flow ofyour code, however newer programming languages have evolved which do help here,and in some areas the use of the GOTO instruction has been eliminated entirely!

However don’t fix the idea in your head that all GOTOs are bad, we need to balancethings up and note that not everyone thinks that GOTOs are bad – myself included.Very occasionally, a GOTO can get you out of a situation in a more elegant mannerthan some of the constructs you’ll read about next, so don’t be afraid of the GOTO, butinstead respect it, try not to use it, but if you have to use it, then use it well!

Page 21: Return to Basics

Chapter 7

Variables

A variable is an area of computer memory that we can use to store data in. It has aname associated with it as well as the memory locations needed to store the data.

7.1 Names

Variable names must start with a letter but may otherwise contain any number of lettersand digits and underscores. An example of a variable name might be: counter, orheight or numberOfCows and so on.

7.2 Types

RTB Supports two types of variables; Numbers and Strings. These can be scalars orarrays.

String variables are differentiated from numeric variables by having the dollar char-acter appended to them. E.g. name$ or homeTown$ and so on.

A number is any decimal value like 1, 3.14, -5, and so on. Numbers may also beexpressed in scientific notation. and we use the letter “e” to represent the power of 10so 1.234e7 is 1.234× 107, or 12340000. The number range is typically ±10308.

A string is a sequence of characters, e.g. "Hello", "Gordon" and so on. Stringsare always enclosed in double-quotes. The maximum length of a string is limited byavailable memory.

14

Page 22: Return to Basics

CHAPTER 7. VARIABLES 15

7.3 Assignment

We assign values to variables using the LET instruction. e.g.

LET numberOfCows = 0

and we can modify them as follows:

LET numberOfCows = cowsInField + cowsInBarn

The LET statement is optional.!

7.4 Numeric operators and Precedence

There are various arithmetic and logical operators that can be used with numbers.

In order of precedence, they are:

Precedence Operator DescriptionHighest ^ Exponent, or “raise to the power of”

- Unary minusNOT Logical NOT

* / MOD DIV Multiply, Divide, Modulo, Integer Division+ - Addition, Subtraction

| & XOR Logical OR, AND and XOR< <= > >= Conditional tests

= <> Conditional Equals and Not EqualsLowest AND OR Conditional AND and OR

You can always use ()’s to alter the evaluation order, if required, and in some casesthey may help to make the code more readable and obvious.

7.5 String Operators

There is only one string operator - the plus operator which we can use to concatenatestrings.

Page 23: Return to Basics

CHAPTER 7. VARIABLES 16

firstName$ = "Gordon"

lastName$ = "Henderson"

fullName$ = firstName$ + " " + $lastName$

PRINT fullName$

7.6 Arrays

Arrays must be declared before they are first used and we must know the size of itbefore-hand. We declare them with the DIM1 statement.

Arrays can be either numeric or string. They can not hold mixed values.

Arrays can have more than one dimension - the limit to the number of dimensionsand overall size is program memory.

Arays are used as follows:

10 DIM list (4)

20 list(0) = 1

30 list(2) = 4

40 PRINT list(2) + list(0)

50 END

7.7 Associative Arrays

Associative arrays (sometimes called a map) is another way to refer to the individialelements of an array. In the examples above we used a number, however RTB alsoaccepts a string.

10 DIM record$ (10)

20 record$ ("firstName") = "Gordon"

30 record$ ("lastName") = "Henderson"

40 record$ ("county") = "Devon"

50 PRINT "First name is "; record$ ("firstName")

Associative arrays can be multi-dimensional and you can freely mix numbers andstrings for the array indices.

1Short for Dimension.

Page 24: Return to Basics

Chapter 8

Input and Output

8.1 Input

You can read data into your program with the INPUT statement. It works as follows:

100 INPUT test

110 PRINT test

199 END

You can only input one item at a time.

When RTB encounters the INPUT statement, program execution stops, a question-mark (?) is printed and it waits for you to type something.

It then assigns what you typed to the variable.

If you typed in a string when it was expecting a number, then it will assign zero tothe number.

To stop it printing the question mark, you can optionally give it a string to print:

100 INPUT "Give me a number: ", test

110 PRINT test

199 END

INPUT reads a line of text in until you press the ENTER key. To just read in a singlecharacter, you can use the GET instruction to read the key as a number, or the GET$

instruction to read the key as a string.

100 PRINT "Press any key to continue ";

17

Page 25: Return to Basics

CHAPTER 8. INPUT AND OUTPUT 18

110 key$ = GET$

199 END

Finally, it’s sometimes handy to be able to see if a key has been pressed withoutstopping the program running - e.g. in games and simulations. To do this, we can usethe INKEY command. This will return the ASCII value of the key presses, or -1 if no keyhas been pressed.

100 REM Simple reaction timer

110 WAIT (2)

120 WHILE INKEY <> -1 CYCLE

130 // Do nothing, just make sure no keys have been pushed

140 REPEAT

150 start = TIME

160 PRINT "Go!"

170 WHILE INKEY = -1 CYCLE

180 // Do nothing

190 REPEAT

200 etime = TIME

210 PRINT "Your reaction time is "; etime - start; " milliseconds"

220 END

8.2 Output

Outputting text to the screen is done via the PRINT command. The PRINT command isquite versatile and will print any combination of numbers and strings separated by thesemicolon (;)

100 INPUT "Give me a number: ", test

110 PRINT "The number you gave me was "; test

199 END

Normally the PRINT command will move to the next line of output at the end of theprogram statement, but you can suppress this with a trailing semicolon.

100 PRINT "This is on a line on its own"

110 PRINT "This is at the start ";

120 PRINT "and this is at the end of the same line"

199 END

The PRINT command may be abbreviated with a question mark (?)

Page 26: Return to Basics

CHAPTER 8. INPUT AND OUTPUT 19

100 ? "Hello, world"

but if you type LIST it will be converted back into the full PRINT instruction however the? way is handy for immediate mode calculations.

You can affect the way numbers are printed using the NUMFORMAT procedure. Thistakes 2 arguments, the first specifying the total number of characters to print and the2nd the number of characters after the decimal point.

100 NUMFORMAT (6,4)

110 PRINT PI

199 END

will print: 3.146

Numbers printed this way are right-justified with leading spaces inserted if required.

NUMFORMAT (0,0) restores the output to the general purpose format used by default.

Page 27: Return to Basics

Chapter 9

Data, Read and Restore

Sometimes you need to initialise variables with a predetermined set of data – numbersor strings. BASIC and RTB has a relatively easy, if somewhat old-fashioned1 way to dothis.

There are three keywords to control this. This first is the DATA instruction. Thisdoesn’t do anything in its own right, but serves as a marker or placeholder for thedata. After the DATA instruction, you list data values, numbers or strings separated bycommas, as this example demonstrates:

1000 DATA 1, 2, 17.4

1010 DATA "Monday", "Tuesday", "Wednesday, "Thursday"

1020 DATA "Friday,", "Saturday", "Sunday", 7

Note that you can mix numbers and strings on the same line.

To get data into your program variables, we use the READ instruction. We can readone, or many items of data at a time.

100 READ start, end, number

110 READ day1$, day2$

and so on.

RTB remembers the location of the last item of data read, so that the next readcontinues from where it left off. If you try to read more data than there is defined thenyou’ll get an error message and the program will stop running.

1Old-fashioned in that hardly any other modern languages use this method, but it’s still useful in thisenvironment

20

Page 28: Return to Basics

CHAPTER 9. DATA, READ AND RESTORE 21

You can reset RTBs data pointer with the RESTORE command. On its own, it willreset it to the very first DATA statement, but you can give it a line number to set the datapointer to the first data item on that line.

100 RESTORE 1010

110 READ day$

120 PRINT day$

130 RESTORE 1010

140 READ day$

150 PRINT day$

160 END

With the above set of data statements, this will print Monday twice.

Data statements can be anywhere in your program – they are ignored by the inter-!preter and only used for READ instructions, however this does mean that we need totrack line numbers to remember where our data is if using the RESTORE instruction. . .I recommend that you place your data at the very end of your program with high linenumbers to make it easier to work out where the data is and to keep track of it.

Here is an example which calculates the average of a set of numbers:

100 REM Calculate the average of a set of numbers and print

110 REM out numbers below, equal to or above average

120 REM

130 READ numNumbers

140 DIM table (numNumbers)

150 FOR i = 0 to numNumbers - 1 CYCLE

160 READ table (i)

170 REPEAT

180 //

190 REM Average

200 //

210 average = 0

220 FOR i = 0 to numNumbers - 1 CYCLE

230 average = average + table (i)

240 REPEAT

250 average = average / numNumbers

260 PRINT "The average is "; average

270 //

280 // Print all numbers

290 //

300 FOR i = 0 to numNumbers - 1 CYCLE

310 PRINT "Number "; i + 1; " = "; table (i); " ";

Page 29: Return to Basics

CHAPTER 9. DATA, READ AND RESTORE 22

320 IF table (i) < average THEN PRINT "below"

330 IF table (i) > average THEN PRINT "above"

340 IF table (i) = average THEN PRINT "average"

350 REPEAT

360 END

1000 DATA 10

1010 DATA 1,2,3,4,5,6,7,8,9,0

Page 30: Return to Basics

Chapter 10

Built-in Variables and Constants

RTB has several variables and constants that are built-in. Some of these variables areread-only1, and some can be assigned to.

10.1 Read only built-in variables

DATE$ This returns a string with the current date in the following format: YYYY-MM-DD.For example: 2012-02-14.

TIME$ This returns a string with the current time in the following format: HH:MM:SS. Forexample: 18:05:45.

PI, PI2 These return the approximate value of π and 1

2π respectively.

TIME This returns a number which represents the time that your program has beenrunning in milliseconds.

GET This pauses program execution and waits for you to type a single character onthe keyboard, then returns the value of the key pressed as a numeric variable.(ASCII)

GET$ This pauses program execution and waits for you to type a single character onthe keyboard, then returns the key as a string variable.

INKEY This is similar to GET except that program execution is not paused; If no key ispressed, then -1 is returned.

TRUE Represents the logical “true” value.

1You may be forgiven for thinking that a read-only variable should be called a constant, but some ofthem change!

23

Page 31: Return to Basics

CHAPTER 10. BUILT-IN VARIABLES AND CONSTANTS 24

FALSE Represents the logical “false” value.

TWIDTH The width in characters of the display.

TTHEIGHT The height in characters of the display.

GWIDTH The width in graphical pixels of the display.

GHEIGHT The hight in graphical pixels of the display.

10.2 Constants representing colours

Black, Navy, Green, Teal, Maroon, Purple, Olive, Silver, Grey, (or Gray), Blue, Lime,Aqua, (or Cyan), Red, Pink, (or Magenta or Fuchsia), Yellow, White.

10.3 Constants representing Keyboard Keys

KeyUp, KeyDown, KeyLeft, KeyRight, KeyIns, KeyHome, KeyDel, KeyEnd, KeyPgUp, KeyPgDn,KeyF1, KeyF2, KeyF3, KeyF4, KeyF5, KeyF6, KeyF7, KeyF8, KeyF9, KeyF10, KeyF11, KeyF12.

10.4 Read/Write built-in variables

SEED This can be assigned to to initialise the random number generator, or you canread it to find the current seed.

TCOLOUR Set/Read the current text foreground colour.

BCOLOUR Set/Read the current text background colour.

HTAB Set/Read the current text cursor horizontal position.

VTAB Set/Read the current text cursor vertical position.

COLOUR Set/Read the current graphics plot colour.

TANGLE Set/Read the current turtle angle.

TCOLOR Alias for TCOLOUR

BCOLOR Alias for BCOLOUR

Page 32: Return to Basics

CHAPTER 10. BUILT-IN VARIABLES AND CONSTANTS 25

COLOR Alias for COLOUR

A brief example:

10 REM Check some built-in Variables

20 today$ = DATE$

30 PRINT "Today’s date is: "; today$

40 END

Page 33: Return to Basics

Chapter 11

Conditionals: IF . . . THEN . . .

RTB has a comprehensive IF statement, but unlike some BASICs, you must use thecorresponding THEN statement with it.

100 REM IF Statement example

110 dummy = 42

120 IF dummy < 100 THEN GOTO 200

130 PRINT "it’s >= 100"

140 END

200 IF dummy = 42 THEN dummy = dummy + 1

210 PRINT "Dummy is "; dummy

220 END

The expression inside the IF ... THEN statement is evaluated as if it’s an assign-ment expression. If the result if 0, then its treated as FALSE, anything else is treatedas TRUE. See the section on operator precedence in the variables chapter for moredetails.

11.1 Multiple lines

We can extend the IF statement over multiple lines, if required. The way you do thisis by making sure there is nothing after the THEN statement and ending it all with theENDIF statement as this short example demonstrates:

100 REM Program fragment to demonstrate multi-line IF

110 test = 42

120 IF test = 15 THEN

26

Page 34: Return to Basics

CHAPTER 11. CONDITIONALS: IF . . . THEN . . . 27

130 PRINT "Test was not 15"

140 PRINT "We will end now"

150 END

160 ENDIF

170 PRINT "Test was 15"

180 PRINT "We can go home now"

190 END

11.2 IF . . . THEN . . . ELSE . . . ENDIF

We can modify the IF...THEN construct with additional keywords; ELSE and ENDIF Theycan provide an alternative set of instructions to follow without using a GOTO instructionand can help to make your programs more readable.

The following example demonstrates:

100 REM Guess

110 secret = 42

120 INPUT "Guess the number? ", guess

130 IF guess = secret THEN GOTO 500

140 IF guess < secret THEN

150 PRINT "Too low"

160 ELSE

170 PRINT "Too high"

180 ENDIF

190 GOTO 120

500 PRINT "Well done"

510 END

There are a few simple rules to observe when using multi-line IF...THEN...ELSE state-ments.

• IF, THEN and ELSE must be on separate lines, with nothing after the THEN state-ment (Some programming languages allow you to put them on the same lines,RTB doesn’t)

• The IF, ELSE and ENDIF statements must be at the start of a line. ie. you can nothave them after a THEN statement.

• If you are executing a GOTO instruction after a THEN instruction then you need touse both THEN and GOTO. Some variants of BASIC allow just one or the other, butnot RTB. These do not work in RTB:

Page 35: Return to Basics

CHAPTER 11. CONDITIONALS: IF . . . THEN . . . 28

100 IF a = 5 THEN 120

120 IF b = 7 GOTO 155

Page 36: Return to Basics

Chapter 12

Conditionals: SWITCH and CASE. . .

There is another form of conditionals commonly known as the switch statement al-though it’s actual representation varys from one programming language to another.

Essentially, the SWITCH instruction allows you to test a value against many differ-ent values, and execute different code. It can often simplify the writing of multipleIF. . . THEN. . . ELSE statements.

It’s probably easier to explain by example:

100 INPUT a

110 SWITCH (a)

120 CASE 1, 2

130 PRINT "You entered 1 or 2"

140 ENDCASE

150 //

160 CASE 7

170 PRINT "You entered 7"

180 ENDCASE

190 //

200 DEFAULT

210 PRINT "You entered something else"

220 ENDCASE

230 ENDSWITCH

240 END

Here, line 110 starts it off. We can put any expression or variable inside the brackets.What follows is lines of CASE statements and each CASE has one or more constant num-bers after it. These numbers are matched against the value of the SWITCH instructionand if there is a match then the code after the CASE is executed up to the ENDCASE

instruction and at that point, control is transfered to the statement after the matching

29

Page 37: Return to Basics

CHAPTER 12. CONDITIONALS: SWITCH AND CASE. . . 30

ENDSWITCH statement. Optionally you can use the instruction DEFAULT and this sectionwill be executed if there are no other matches.

Like IF. . . THEN. . . ELSE, SWITCH has a few simple rules:

• Every SWITCH must have a matching ENDSWITCH statement.

• Every CASE or DEFAULT statement must have a matching ENDCASE statement.

• Statements after a CASE statement must not run-into another CASE statement.(Some programming languages do allow this, but not RTB)

• The constants after the CASE statement (and the expression in the SWITCH state-ment) can be either numbers or strings, but you can’t mix both.

Finally, if you are reading some older BASIC programs, you may see a constructlike ON. . . GOTO. . . Our SWITCH statement is the modern way to handle code like this andif you are converting older programs it should be relatively simple to use the newerSWITCH instructions to help eliminate the issues keeping track of line numbers. Anexample:

Old Way:

100 ON a GOTO 200,300,400

120 PRINT "a was NOT 1, 2 or 3"

130 STOP

200 PRINT "a was 1"

210 GOTO 500

300 PRINT "a was 2"

310 GOTO 500

400 PRINT "a was 3"

410 GOTO 500

500 PRINT "Do something else now"

Page 38: Return to Basics

CHAPTER 12. CONDITIONALS: SWITCH AND CASE. . . 31

New way:

100 SWITCH (a)

110 CASE 1

120 PRINT "a was 1"

130 ENDCASE

140 CASE 2

150 PRINT "a was 2"

160 ENDCASE

170 CASE 3

180 PRINT "a was 3"

190 ENDCASE

200 DEFAULT

210 PRINT "a was NOT 1, 2 or 3"

220 STOP

230 ENDCASE

240 ENDSWITCH

250 PRINT "Do something else now"

So the new way is a bit longer and more to type, but it has the advantage of not relyingon line numbers and if we use the indentation as a guide, we can quickly scan down tofind the matching ENDSWITCH statement.

Page 39: Return to Basics

Chapter 13

Looping the Loop

This chapter explains the RTB instructions for handling loops. Using these instructions,you can almost always eliminate the use of the GOTO instruction, and hopefully makeyour program easier to read.

There are three new commands which RTB has to define a block of code which willbe repeated. These are: CYCLE which defines the start of the block and REPEAT whichdefines the end. An alternative form of CYCLE is DO

13.1 CYCLE . . . REPEAT

On their own, these would cause a program to loop between them forever, howeverRTB provides 3 main means to control these loops. Lets start with an example.

10 REM Demonstration of CYCLE...REPEAT

20 count = 1

30 CYCLE

40 PRINT "5 * "; count; " = "; 5 * count

50 count = count + 1

60 REPEAT

70 END

If you RUN this program, it will run forever until you hit the ESC key, so it’s not that usefulfor anything other than to demonstrate CYCLE and REPEAT.

We could use an IF ... THEN GOTO sequence to jump out of the loop, but that’s notvery elegant and we’re trying to not use GOTO anyway. Fortunately there are severalmodifiers.

32

Page 40: Return to Basics

CHAPTER 13. LOOPING THE LOOP 33

13.1.1 UNTIL

Lets modify it slightly – change line 60:

60 REPEAT UNTIL count > 10

and run the program again. We get our 5 times table, as before, but not a GOTO insight. We can see clearly the lines of code executed inside the loop and if we read theprogram it possibly makes more sense. We also don’t need to keep track of any linenumbers either.

13.1.2 WHILE

If we test the condition at the end of the loop (using UNTIL) then the loop will be ex-ecuted at least once. There may be some situations where we don’t want the loopexecuted at all, so to accomplish this we can move the test to the start of the loop,before the CYCLE instruction, but rather than use the UNTIL instruction we now useWHILE.

Note that you can use WHILE or UNTIL interchangeably. So you can re-write the aboveas:

10 REM Demonstration of CYCLE...REPEAT

20 count = 1

30 WHILE COUNT <= 10 CYCLE

40 PRINT "5 * "; count; " = "; 5 * count

50 count = count + 1

60 REPEAT

70 END

or

10 REM Demonstration of CYCLE, REPEAT...WHILE/UNTIL

20 count = 1

30 UNTIL COUNT > 10 CYCLE

40 PRINT "5 * "; count; " = "; 5 * count

50 count = count + 1

60 REPEAT

70 END

Page 41: Return to Basics

CHAPTER 13. LOOPING THE LOOP 34

13.2 DO . . . REPEAT

The DO instruction is very similar to CYCLE however the positioning of the test is re-versed. Writing some of the above examples with DO:

10 REM Demonstration of DO, REPEAT...WHILE/UNTIL

20 count = 1

30 DO WHILE count <= 10

40 PRINT "5 * "; count; " = "; 5 * count

50 count = count + 1

60 REPEAT

70 END

and

10 REM Demonstration of DO...REPEAT and WHILE/UNTIL

20 count = 1

30 DO UNTIL count > 10

40 PRINT "5 * "; count; " = "; 5 * count

50 count = count + 1

60 REPEAT

70 END

Finally (in this section!) note that not all programing languages are as flexible as!this – some only allow WHILE at the top of a loop and UNTIL (or their equivalents) at thebottom.

13.3 For Loops

There is another form of loop which combines a counter and a test in one instruction.This is a standard part of most programming languages in one form or another and isoften called a “for loop”.

Here is our five times table program written with a for loop:

10 REM Demonstration of FOR loop

20 FOR count = 1 TO 10 CYCLE

30 PRINT "5 * "; count; " = "; 5 * count

40 REPEAT

50 END

Page 42: Return to Basics

CHAPTER 13. LOOPING THE LOOP 35

The FOR loop works as follows: It initialises the variable (count in this instance) to thevalue given; 1, then executes the CYCLE...REPEAT loop, adding one to the value of thevariable until the variable is greater than the end value 10.

There is an optional modification to the FOR loop in that allows you to specify howmuch the loop variable is incremented (or decremented!) by at each iteration by usingthe STEP instruction.

10 REM Demonstration of FOR loop with STEP

20 FOR count = 1 TO 10 STEP 0.5 CYCLE

30 PRINT "5 * "; count; " = "; 5 * count

40 REPEAT

50 END

This will print our 5 times table in steps of 0.5.

Note that that the end number (10 in this case) isn’t tested for exactly. The test isactually >= So if you were incrementing by 4 (for example), count would start at 1,then become 5, then 9, then 13 – at which point flow control would jump to the lineafter the REPEAT.

Another way to visualise the FOR loop is like this:

10 REM Demonstration of how a FOR loop works

20 REM FOR count = 1 to 10 STEP 0.5 CYCLE ...

30 count = 1

40 WHILE count <= 10 CYCLE

50 PRINT "5 * "; count; " = "; 5 * count

60 count = count + 0.5

70 REPEAT

80 END

the FOR loop is simply a convenient way to incorporate the three lines controlling count

into one line.

Note that this form of the for loop is slightly different from most BASICs, so RTB!provides the standard method too. Our demonstrations loop above can be written as:

10 REM Demonstration of FOR loop with STEP - Standard BASIC

20 FOR count = 1 TO 10 STEP 0.5

30 PRINT "5 * "; count; " = "; 5 * count

40 NEXT count

50 END

Note the NEXT statement. The variable name after NEXT is optional, but if used thenit must match the variable used in the corresponding FOR statement.

Page 43: Return to Basics

CHAPTER 13. LOOPING THE LOOP 36

13.4 Breaking and continuing the loop

There are two other instructions you can use with CYCLE...REPEAT loops – BREAK whichwill cause program execution to continue on the line after the REPEAT instruction, ie.You break out of the loop, and CONTINUE which will cause the loop to re-start at the linecontaining the CYCLE instruction, continuing a FOR instruction and re-evaluating and anyWHILE or UNTIL instructions.

Page 44: Return to Basics

Chapter 14

Built-in Procedures and Functions

Procedures and functions may take parameters (often called arguments), and thesemust be enclosed in brackets. Functions return a value while procedures don’t.

NUMFORMAT (total, decimals) This affects all future number printing via the PRINT

statement. The numbers will be printed right-justified in a total fields width oftotal with decimals digits after the decimal point.

WAIT (time) This waits for time seconds. This may be a fractional number, but theaccuracy will depend on the computer you are running it on, however delaysdown to 1/100th of a second should be achievable.

STOP Program execution is stopped with an message indicating the current line num-ber.

END Program execution is ended.

CLS The screen is cleared.

HVTAB (x, y) The cursor is moved to the supplied column x and line y. Note that (0,0)is top-left of the text screen.

DEG Switches the internal angle system to degrees. There are 360 degrees in a fullcircle.

RAD Switches the internal angle system to radians. There are 2π radians in a full circle.

CLOCK Switches the internal angle system to clock mode. There are 60 minutes in afull circle.

SWAP (v1, v2) This swaps the value of the 2 variables round. Both arguments mustbe the same type - ie. both numberic or both string.

37

Page 45: Return to Basics

Chapter 15

Numerical Functions

RND (range) This function returns a random number based on the value of range. Ifrange is zero, then the last random number generated is returned, if range is 1,then a random number from 0 to 1 is returned, otherwise a random number from0 up to, but not including range is returned.

SIN (angle) Returns the sine of the given angle.

ASIN (x) Returns the arc sine of the supplied argument x

COS (angle) Returns the cosine of the given angle.

ACOS (x) Returns the arc cosine of the supplied argument x

TAN (angle) Returns the tangent of the given angle.

ATAN (x) Returns the arc tangent of the supplied argument x

ABS (x) Returns the absolute value of the supplied argument x ie. if the argument isnegative, make it positive.

EXP (x) Returns the value of ex

LOG (x) Returns the natural logarithm of x

SQRT (x) Returns the square root of x

SGN (x) Returns -1 if the number is negative, 1 otherwise. (Zero is considered posi-tive)

INT (x) Returns the integer part of x

38

Page 46: Return to Basics

CHAPTER 15. NUMERICAL FUNCTIONS 39

HASH (string, modulo) Returns a hash value based on the string and modulo. Thevalue returned is between 0 and modulo. This function is used internally in RTBfor the associative array indexing. The hash function is based on the function inThe Practice of Programming (HASH TABLES, pg. 57)

MAX (x, y) Returns the larger of x or y

MIN (x, y) Returns the smaller of x or y

VAL (string$) Returns the number represented by string$. e.g. ”1234” would returnthe number 1234.

ASC (string$) Returns the ASCII value represented by the first character of string$.e.g. ”A” would return 65. It is the opposite of the CHR$ function.

LEN (string$) Returns the number of characters in string$.

Page 47: Return to Basics

Chapter 16

String Functions

CHR$ (num) This function returns a string from the ASCII value of num It is the oppositeof the ASC function. e.g. CHR$ (65) returns ”A”.

LEFT$ (string$, len) Returns the leftmost len characters of string$.

RIGHT$ (string$, len) Returns the rightmost len characters of string$.

MID$ (string$, start, len) Returns the middle len characters of string$ startingfrom position start.

SPACE$ (len) Returns a string of length len comprising of spaces.

STR$ (num) Returns a string version of the supplied number num. e.g. STR$ (42.42)

would return "42.24".

40

Page 48: Return to Basics

Chapter 17

Graphical Procedures and Functions

RTB supports two types of graphics; traditional Cartesian (x, y) type graphics with func-tions to plot points, lines, etc. and turtle graphics which can sometimes be easier toteach/demonstrate to younger people.

RTB normally works in the highest graphical resolution available to it, but it also sup-ports two effective resolutions - the high resolution is the native resolution of the displayyou are using - e.g. on a PC monitor this may be up to 1280x1024, or 1920x1080 onan HD monitor. It also supports a lower resolution where each low resolution pixel isreally 8x8 high resolution pixels.

Your programs should use the GWIDTH and GHEIGHT system variables to scale theiroutput to the current display size. Assuming a fixed resolution may not be advisable forprograms designed to run on systems other than your own.

17.1 System Variables

COLOUR This can be assigned to, or read from, and represents the current plottingcolour.

GHEIGHT This can be read to find the current height of the display in either high resolu-tion or low resolution pixels.

GWIDTH This can be read to find current width of the display in either high resolution orlow resolution pixels.

TANGLE This can be read or assigned to and represents the current angle of the turtlewhen using turtle graphics mode.

41

Page 49: Return to Basics

CHAPTER 17. GRAPHICAL PROCEDURES AND FUNCTIONS 42

See also the colour names and note the current angle modes when using turtlegraphics or any transcendental functions to plot graphics.

17.2 General Graphics

GR This clears the screen and initialises low-resolution graphics mode.

HGR This clears the screen and initialises high-resolution graphics mode.

SaveScreen (filename$) This takes a snapshot of the current screen and saves it tothe filename given.

rgbColour (r, g, b) This sets the current graphical plot colour to an RGB (Red,Green, Blue) value. The values should be from 0 to 255.

UPDATE When plotting graphics, they are actually plotted to a separate off-screen work-ing area, so if you write a program that does lots and lots of graphical actions,then you may not see the display being updated. The UPDATE procedure forcesthe working area to be copied to the display. An update is also performed auto-matically if your program stops for input, or when you PRINT a new line.

17.3 Cartesian Graphics

The graphical origin at (0,0) is at the bottom-left of the display. Coordinates may benegative and may also be off-screen when lines are clipped to the edge of the display.The ORIGIN command may be used to change the graphical origin.

PLOT (x, y) This plots a single pixel in the selected graphics mode in the selectedcolour. Note that (0,0) is bottom left.

HLINE (x1, x2, y) Draws a horizontal line on row y, from column x1 to column x2.

VLINE (y1, y2, x) Draws a vertical line on column x, from row y1 to row y2.

LINE (x1, y1, x2, y2) Draws a line from point x1, y1 to x2, y2.

LINETO (x1, y1) Draws a line from the last point plotted (by the PLOT or LINE proce-dures to point x1, y1.

CIRCLE (cx, cy, r, f) Draws a circle at (cx,cy) with radius r. The final parameter,f is either TRUE or FALSE, and specifies filled (TRUE) or outline (FALSE).

Page 50: Return to Basics

CHAPTER 17. GRAPHICAL PROCEDURES AND FUNCTIONS 43

ELLIPSE (cx, cy, xr, yr, r, f) Draws an ellipse at (cx,cy) with x radius xr and y

radius yr. The final parameter, f is either TRUE or FALSE, and specifies filled(TRUE) or outline (FALSE).

TRIANGLE (x1, y1, x2, y2, x3, y3, f) Draws a triangle with its corners at the threegiven points. The final parameter, f is either TRUE or FALSE, and specifies filled(TRUE) or outline (FALSE).

PolyStart This marks the start of drawing a filled polygon.

PolyPlot (x, y) This remembers the given X,Y coordinates as part of a filled poly-gon. Nothing is actually drawn on the screen until the PolyEnd instruction isexecuted. Polygons can have a maximum of 64 points.

PolyEnd This marks the end of drawing a polygon. When this is called, the storedpoints will be plotted on the screen and the polygon will be filled.

RECT (x, y, w, h, f) Draws a rectangle at (x,y) with width w and height h. Thefinal parameter, f is either TRUE or FALSE, and specifies filled (TRUE) or outline(FALSE).

ORIGIN (x, y) This changes the graphics origin for the Cartesian plotting procedures.The x, y coordinates are always absolute coordinates with (0,0) being bottomleft.

17.4 Turtle Graphics

When the graphics are initialised with either GR or HGR, the turtle position is set tothe middle of the screen, pointing at an angle of 0 degrees (up) with the pen lifted.However note that the turtles position is affected by the ORIGIN command above, soyou may have to take this into consideration if using ORIGIN.

With the noted effects of the ORIGIN procedure, you may mix turtle and Cartesiangraphics without any issues.

PENDOWN This lowers the “pen” that the turtle is using to draw with. Nothing will bedrawn until you execute this procedure.

PENUP This lifts the “pen” that the turtle uses to draw. You can move the turtle withoutdrawing while the pan is up.

MOVE (distance) This causes the turtle to move forwards distance screen pixels.

Page 51: Return to Basics

CHAPTER 17. GRAPHICAL PROCEDURES AND FUNCTIONS 44

MOVETO (x, y) This moves the turtle to the absolute location (x, y). A line will bedrawn if the pen is down.

LEFT (angle) Turns the turtle to the left (counter clockwise) by the given angle.

RIGHT (angle) Turns the turtle to the right (clockwise) by the given angle.

17.5 Sprite Procedures and Functions

Sprites are (usually) small rectangular or square bitmap images which you can moveround the screen under program control. You can create them using one of the manygraphical image creation packages available. The file-format is 24 bits per pixel BMP,and a colour of 254,254,254 is transparent.

LoadSprite (filename$) This loads a sprite from the supplied fine into memory andreturns a handle to the internal sprite data. You need to use the number returnedin all future sprite handling functions/procedures.

PlotSprite (sprite, x, y) This plots the given sprite sprite at the suppled x, y

coordinates. The coordinates specify the bottom-left corner of the bounding rect-angle of the sprite.

DelSprite This removes a sprite from the screen.

You do not have to erase a sprite from the screen when you move it, just callPlotSprite with the new coordinates.

Page 52: Return to Basics

Chapter 18

Writing your own Procedures andFunctions

This chapter will show you how you can eliminate the GOSUB instruction once and for alland not be reliant on line numbers for your programs.

18.1 User defined Procedures

Procedures (and functions) have a name, so just like variable names, make them some-thing that’s easy to understand. We’ll start by example and write a procedure to printout a multiplication table:

500 REM Procedure to print a multiplication table

510 DEF PROC multiplication

520 FOR num = 1 to 10 CYCLE

530 PRINT num; " * "; table; " = "; num * table

540 REPEAT

550 ENDPROC

We’ve given it a high line number to start with as procedures (and functions) should beat the end of your program1.

Understanding the above: DEF PROC tells the system to define a new procedure andcall it multiplication. The procedure uses a global variable called table to representthe table to be printed.

1This isn’t strictly necessary, but if the RTB system sees the DEF keyword in normal program flowthen it will consider it an error and stop. You could put them all at the top, then use a GOTO instruction tojump over them, but some may consider that inelegant!

45

Page 53: Return to Basics

CHAPTER 18. WRITING YOUR OWN PROCEDURES AND FUNCTIONS 46

Demonstration of the above procedure, used to print the 2,4,6 and 8 times tables:

100 REM Print some tables

110 FOR table = 2 TO 8 STEP 2 CYCLE

230 PROC multiplication

240 REPEAT

250 END

PROC without the DEF calls the procedure.

Like built-in procedures and functions, we can give our procedures arguments. Theycan also have variables local to themselves.

Re-writing with an argument and local variable:

500 REM Procedure to print a multiplication table

510 DEF PROC multiplication (table)

515 LOCAL num

520 FOR num = 1 to 10 CYCLE

530 PRINT num; " * "; table; " = "; num * table

540 REPEAT

550 ENDPROC

and our calling program is now:

100 REM Print some tables

110 FOR table = 2 TO 8 STEP 2 CYCLE

230 PROC multiplication (table)

240 REPEAT

250 END

18.2 User defined Functions

Functions work in almost exactly the same was as procedures. We can give themparameters and declare LOCAL variables, however the one difference is that a functioncan return a result, and you do this by ending the function with the value to return afteran equals symbol = as this simple example demonstrates:

100 REM Function test - print squares

110 FOR i = 1 TO 10 CYCLE

130 x = FN square (i)

Page 54: Return to Basics

CHAPTER 18. WRITING YOUR OWN PROCEDURES AND FUNCTIONS 47

140 PRINT i; " squared is"; x

150 REPEAT

160 END

500 REM Function to return the square of a number

510 DEF FN square (num)

520 LOCAL result

530 result = num * num

540 = result

You can use user-defined functions anywhere you might use a built-in function, andyou can define functions that return strings too – just make sure its name ends with adollar sign.

This example reverses a string:

500 REM Function to reverse a string

510 DEF FN reverse$ (s$)

520 LOCAL i

530 LOCAL new$

540 new$ = "" // Start with nothing

550 FOR i = 1 to LEN (s$) CYCLE

560 new$ = new$ + MID$ (s$, LEN (s$) - i, 1)

570 REPEAT

580 = new$

18.3 Recursion

Recursion is when a procedure or function calls itself.

Here is an example – The factorial function is defined as the number multiplied byone less than itself multiplied by one less than that and so on until you reach one.

e.g. 5 factorial is 5 * 4 * 3 * 2 * 1 (or 120)

A way of representing this is to say that 5 factorial is 5 times 4 factorial.

We know that 4 factorial is 4 times 3 factorial, . . .

We also know that 1 factorial is 1 (and that we normally use the exclamation markto represent the factorial function)

So in general terms as can say that N ! = N ∗ (N − 1)!

If you’re having problems thinking that one through, think of Russian matryoshkadolls – A matryoshka doll is a matryoshka doll with another matryoshka doll inside. . .

Page 55: Return to Basics

CHAPTER 18. WRITING YOUR OWN PROCEDURES AND FUNCTIONS 48

Have a look at this function:

500 DEF FN factorial (n)

510 IF n = 1 THEN = n

520 = n * FN factorial (n - 1)

A an excercise, write a program to test this. How big a number can you give it?2

Now think about the string reversal function earlier. Can it be written using recur-sion? If you think it can, then give it a go3.

But before you get carried away with recursion, is it always the best way? Who!knows – each case will be different. Think back to the factorial example – wouldn’t itjust have been easier to write it using a FOR loop? Give it a go and see what you think.

And if you want a recursive challenge, try this: You have a board with three shortwooden rods. On one rod is 5 different sized disks with a hole in the middle to slidedown the rod. Each disk is a different size and the largest is at the bottom and thesmallest at the top. See the picture below.

Your task is to move them from one pole to another, but you must never place alarger disk on top of a smaller one and you can only move one disk at a time. Thispuzzle is called the Towers of Hanoi and is one of the old “classic” problems where arecursive solution works well.

The Towers of Hanoi

18.4 Variable Scope

We talk about the “scope” of a variable to mean what parts of your program can seea variable (and change it!) We’ve seen that subroutines can see (and change!) all

2And did you just pick numbers ar random or use a FOR loop to count up?3If stuck, then think of it this way: a reversed string is the last character in a string followed by the rest

of the string, reversed

Page 56: Return to Basics

CHAPTER 18. WRITING YOUR OWN PROCEDURES AND FUNCTIONS 49

variables we use in the main program, but what about user defined procedures andfunctions?

The rules in RTB are somewhat simple, and whilst not identical, are similar enoughto most other progrmaming languages to give you a good insight.

When a procedure or function is called, the arguments and LOCAL variables store acopy of any variables with the same name, making them avalable for use inside thatprocedure or function. When the procedure ends, then the stored values are copiedback into the original variables.

So what happens if a procedure calls another procedure which doesn’t have localvariables? Well, the values it sees are those of the last procedure and not those in themain program. Essentially, RTB propogates local variables into each new procedure.The easiest way to demonstrate is by example:

100 REM Variable Scope Test

110 v1 = 123

120 v2 = 456

130 PROC test1 (v1)

140 PRINT "v1 is: "; v1; " v2 is "; v2

150 END

300 REM test1

310 DEF PROC test1 (num1)

320 LOCAL v1

330 v1 = 888

340 PRINT "v1 is: "; v1; " v2 is "; v2; " num1 is: "; num1

350 PROC test2

360 ENDPROC

500 //

510 REM test2

520 //

530 DEF PROC test2

340 PRINT "v1 is: "; v1; " v2 is "; v2; " num1 is: "; num1

Page 57: Return to Basics

Appendix A

Colours

To keep thing simple, RTB has 16 colours pre-definied for your use. You can usenumbers to represent them, or use the built-in names. The colours are:

0 1 2 3 4 5 6 7

Black Navy Green Teal Maroon Purple Olive Silver8 9 10 11 12 13 14 15

Grey Blue Lime Aqua Red Pink Yellow White

Note that the colours here are just representative and might not be what you see onthe video screen you are using to run RTB on.

These colours are avalable in both text and graphics modes.

If these 16 colours are not sufficient, then it’s possible to use the full colour pallet ofthe computer system you are using - this is usually a 24-bit system with 8 bits for eachof Red, Green and Blue, giving a maximum of 16777216 different colous avalable.

To use the full-colour mode, you need to use the built-in procedure

• rgbColour (red, green blue)

where the values of red, green and blue are numbers from 0 to 255 inclusive.

The extended RGB colours are avalable in graphics modes only. Text is limited tothe standard 16 colours.

50

Page 58: Return to Basics

APPENDIX A. COLOURS 51

A.1 A Text Colour Demo

There should be a program supplied with your RTB installation called ”colours” whichwill demo the basic colours in text mode. LOAD colours should obtain it for you, but ifit’s not avalable then:

100 REM Colour test program

110 //

120 DIM c$(15)

130 FOR i = 0 TO 15 CYCLE

140 READ c$(i)

150 REPEAT

160 //

170 CLS

180 VTAB = 4

190 PRINT "Colour test program"

200 PRINT "==================="

210 VTAB = 10

220 FOR bg = 0 TO 15 CYCLE

230 FOR i = 0 TO 1 CYCLE

240 BCOLOUR = 0

250 TCOLOUR = bg

260 PRINT c$(bg);

270 FOR fg = 0 TO 7 CYCLE

280 PROC testcolour(fg + i * 8, bg)

290 REPEAT

300 PRINT

310 REPEAT

320 REPEAT

330 TCOLOUR = 15

340 BCOLOUR = 0

345 END

350 //

360 // Procedure to print some text in the colours supplied

370 //

380 DEF PROC testcolour(f, b)

390 TCOLOUR = f

400 BCOLOUR = b

410 PRINT c$(f);

420 ENDPROC

900 //

901 DATA " Black ", " Navy ", " Green ", " Teal "

902 DATA " Maroon ", " Purple ", " Olive ", " Silver "

903 DATA " Grey ", " Blue ", " Lime ", " Aqua "

904 DATA " Red ", " Pink ", " Yellow ", " White "

Page 59: Return to Basics

Appendix B

Numbering Names and Formats

When numbers get very big or very small, it starts to become hard to represent them,so we have developed various way to express them. Then computers came along andthings are now somewhat different and potentially confusing.

The most common way to represent big or small numbers is to write them in groupsof three digits and the International System of Units (SI, or Systeme internationald’unites) has a set of standard names to represent these, however there is a compli-cation. The SI units use powers of 10 (so 1,000, 1,000,000 and so on), but computerscount in twos (binary), so we’ve adopted the same names as the decimal units for com-puter sizes. A further complication is that some components manufacturers use the SIunits to represent capacities because people are used to thinking in computers terms,but then find that the decimal terms are actually smaller. Disk drive manufacturers arefrequently guilty of this.

The following table gives the most popular definitions you’ll encounter:

Name Kilo Mega Giga TeraDecimal 1,000 1,000,000 1,000,000,000 1,000,000,000,000

103 106 109 1012

Computer 1024 1,048,576 1,073,741,824 1,099,511,627,776210 220 230 240

You may see reference to the term MiB in some texts - this stands for Mebi - as inMebibyte and it used to represent the binary forms. So 2MB is 2,000,000 bytes, but2MiB is 2,097,152 bytes.

For the most part you can treat the decimal and binary forms as more or less thesame. The MiB format isn’t that common yet, but seems to be gaining popularity insome circles.

52

Page 60: Return to Basics

Appendix C

File Handling functions

RTB supports reading and writing of files stored on local non-volatile media (typicallydisk drives). There are two main methods of file accessing; sequential and random-access. When you open a file, it is assigned a numerical “handle” and you need touse this handle when performing other operations on the file. RTB allows you to openmultiple files at the same time, up to a limit set by the implementation. (Usually 8). Eachopen file will have a unique handle, so you must keep track of them when manipulatingfiles.

All data written to, or read from a file is textual in nature. You can not write binarydata in RTB. Numbers are stored as literal strings as if they have been printed on thescreen. (And in particular, the format set by the NUMFORMAT instruction is used whenprinting numbers into files).

C.1 Sequential Access

Sequential file access is straightforward. You open a file, then read from it, or write toit. You can reset the file pointer to the start of the file using the REWIND instruction, soyou can write data to a file, then REWIND, then read the data back again. There is nostructure to the file other than what you write into it yourself. ie. if you want to read the15th line in a file, then you need to read the preceding 14 lines unless you know theexact line length of each line, then you can use the SEEK instruction to jump to that line,but if the lines are all different length, then you need to read the while file from start toend,

53

Page 61: Return to Basics

APPENDIX C. FILE HANDLING FUNCTIONS 54

C.2 Random Access

Random file access is similar to sequential, however it additionally allows you to splityour file into fixed-size records, then you can SEEK to any of these records at random.

There are no special instructions that make a file any different when in sequentialmode to random mode other than those you code yourself. You must plan the recordsize (in bytes or characters) before hand and you must always seek to the correctrecord in your file before reading or writing. Each record is then treated like a smallsequential file and you can read and write into it - however you must make sure thatyou don’t write more data into the record than you have planned for, otherwise thatdata will overflow into the next record with unpredictable results.

Random access files can be wasteful of disk space, but they can also be a fastan versatile method of storing and retrieving data. Most modern databases use thistechnique to store data for fast retrieval.

C.3 RTB Instructions and functions

OPEN (filename$)

The OPEN function opens a file and makes it available for reading or writing and returnsthe numeric handle assiciated with the file. The file is created if it doesn’t exist, or if itdoes exist the file pointer is positioned at the start of the file. To append data to theend of the file, you need to use the FFWD instruction after opening the file.

CLOSE (handle)

The CLOSE instruction closes a file after use and makes sure all data is securely writtento the storage medium.

EOF (handle)

The EOF function returns a TRUE or FALSE indication of the state of the file pointer whenreading the file. It is an error to try to read past the end of the file, so if you are readinga file with unknown data in it, then you must check at well defined intervals (e.g. beforeeach INPUT#.

Page 62: Return to Basics

APPENDIX C. FILE HANDLING FUNCTIONS 55

REWIND (handle)FFWD (handle)

Theese instructions move the file pointer back to the start of the file or to the end of thefile respectively. If you want to append data to the end of an existing file, then you needto call FFWD before writing the data.

SEEK (handle, offset)

The SEEK instruction moves the file pointer to any place in the file. It can even move thefile pointer beyond the end of the file in which case the file is extended. The argumentsuppled to SEEK is an absolute number of bytes from the start of the file. If you areusing random access files and want to access the 7th record in the file, then you needto multiple your record size by 7 to get the final location to seek to.

PRINT# handle, . . .

The PRINT# instruction acts just like the regular PRINT instruction except that it sendsdata to the file identified by the supplied file-handle rather than to the screen. Numbersare formatted according to the settings of NUMFORMAT. It is strongly recommended toonly print one item per line if you are going to read those items back into an RTBprogram again.

100 myFile = OPEN ("testfile.dat")

110 FFWD (myFile)

120 PRINT# myFile, "Appended line"

130 CLOSE (myFile)

INPUT# handle, variable

This works similarly to the regular INPUT instruction, but reads from the file identifiedby the supplied file-handle rather than from the keyboard. Note that unlike the regularkeyboard INPUT instruction, INPUT# can only read one variable at a time.

100 myFile = OPEN ("testfile.dat")

110 WHILE NOT EOF (myFile)

120 INPUT# myFile, line$

130 PRINT line$

140 CLOSE (myFile)

Page 63: Return to Basics

Appendix D

Serial Port Programming

RTB supports a few instructions to allow you to send and receive data over serial ports.These go by various names, a common one being RS232.

Moderns PCs often lack a directly connected serial port, so you may have to usea USB connected device. Using them is the same whether directly connected or viaUSB. The important thing to know is the name of the serial device. For directly con-nected devices, this might be something like ”/dev/ttyS1”, or for USB devices it might besomething like ”/dev/ttyUSB0”. Occasionally you have find a ”/dev/ttyACM0” for someolder USB devices.

D.1 SOPEN

This opens a serial device and makes it available for our use. It takes the name of theserial port and the speed as an argument and returns a number (the handle) of thedevice. We can use this handle to reference the device and allow us to open severaldevices at once. Example:

100 arduino = SOPEN ("/dev/ttyUSB0", 115200)

The following baud rates are recognised: 50, 75, 110, 134, 150,200, 300, 600,

1200, 1800, 2400, 19200, 38400 57600, 115200 and 230400, but do check your lo-cal PC and devices capabilities. The device is always opened with the data format setto 8 data bits, 1 stop bit and no parity. All handshaking is turned off.

56

Page 64: Return to Basics

APPENDIX D. SERIAL PORT PROGRAMMING 57

D.2 SCLOSE

This closes a serial port and frees up and resources used by it. It’s not strictly neces-sary to do this when you end your program, but it is considered good practice.

120 SCLOSE (arduino)

D.3 SGET, SGET$

Fetch a single byte of data from an open serial port and return the data as either anumber or a single-character string. This function will pause program execution for upto 5 seconds if no data is available. If there is still not data after 5 seconds, the functionwill return -1 or an empty string.

130 d = SGET (arduino)

140 key$ = SGET$ (arduino)

D.4 SPUT, SPUT$

Send a single byte of data, or a string of characters to an open serial port.

130 SPUT (arduino, byte)

140 SPUT$ (arduino, "Hello")

D.5 SREADY

Returns the number of characters avalable to be read from an open serial port. Thiscan be used to poll the device to avoid stalling your program when there is no dataavlable to be read.

210 IF SREADY (arduino) THEN PROC getSerialData

Page 65: Return to Basics

Appendix E

Raspberry Pi - GPIO Programming

RTB supports the on-board GPIO hardware in the Raspberry Pi computer.

E.1 PinMode

This configures the mode of a pin on the Pi’s GPIO. It takes an argument which speci-fies the mode of the pin - input, output or PWM output.

220 PinMode (4, 0)

In this example, we’re setting pin 4 to be used for input. The modes are;

0 Input

1 Output

2 PWM Output

E.2 DigitalRead

This function allows you to read the state of a digital pin on the Raspberry Pi. You mayneed to set the pin mode beforehand to make sure it’s configured as an input device.It will return TRUE or FALSE to indicate an input being high or low respectively.

200 PinMode (12, 0) // Set pin 12 to input

210 if DigitalRead (12) THEN PROC ButtonPushed

58

Page 66: Return to Basics

APPENDIX E. RASPBERRY PI - GPIO PROGRAMMING 59

E.3 DigitalWrite

This procedure sets a digital pin to the supplied value - 0 for off or 1 for on. As withDigitalRead, you may need to set the pin mode (to output) beforehand.

310 PinMode (2, 1) // Set pin 2 to output mode

320 DigitalWrite (2, 1) // Set output High (on)

330 Wait (1)

330 DigitalWrite (2, 0) // Set output Low (off)

E.4 PwmWrite

This procedure outputs a PWM waveform on the selected pin. The pin must be config-ured for PWM mode beforehand. The value set should be between 0 and 1024.

310 PinMode (1, 2) // Set pin 1 to PWM output mode

320 PwmWrite (11, 200)

Page 67: Return to Basics

Appendix F

DRC - Drogon Remote ControlProgramming

RTB supports the Drogon Remote Control protocol which is used to connect to deviceswhich interpret the DRC protocol. DRC is typically used with small micro-controllerssuch as the Arduino and it allows the control of the digital and analogue input/outputpins to be brought to RTB.

F.1 DrcOpen

This opens a connection to a DRC compatible device and makes it available for our use.It takes the name of the device as an argument and returns a number (the handle) ofthe device. We can use this handle to reference the device and allow us to openseveral devices at once. Some implementations may have IO devices with fixed named.Example:

100 arduino = DrcOpen ("/dev/ttyUSB0")

110 gpio = DrcOpen ("RPI")

On the Raspberry Pi, you may use the string: "RPI", RPI-GPIO or RPI-SYS as thedevice to open and this will open the on-board GPIO pins in native wiringPi, GPIO orSys modes.

60

Page 68: Return to Basics

APPENDIX F. DRC - DROGON REMOTE CONTROL PROGRAMMING 61

F.2 DrcClose

This closes a connection to a DRC device and frees up and resources used by it. It’snot strictly necessary to do this when you end your program, but it is considered goodpractice.

120 DrcClose (arduino)

F.3 DrcPinMode

This configures the mode of a pin on the remote DRC device. It takes an argumentwhich specifies the mode of the pin - input, output or PWM output. Other modes maybe available, depending on the device and its capabilities. Note that not all devicessupport all functions.

220 DrcPinMode (arduino, 4, 0)

In this example, we’re setting pin 4 to be used for input. The modes are;

0 Input

1 Output

2 PWM Output

F.4 DrcDigitalRead

This function allows you to read the state of a digital pin on the remote device. You mayneed to set the pin mode beforehand to make sure it’s configured as an input device.It will return TRUE or FALSE to indicate an input being high or low respectively.

200 DrcPinMode (arduino, 12, 0) // Set pin 12 to input

210 if DrcDigitalRead (arduino, 12) THEN PROC ButtonPushed

F.5 DrcDigitalWrite

This procedure sets a digital pin to the supplied value - 0 for off or 1 for on. As withDigitalRead, you may need to set the pin mode beforehand.

Page 69: Return to Basics

APPENDIX F. DRC - DROGON REMOTE CONTROL PROGRAMMING 62

310 DrcPinMode (arduino, 2, 1) // Set pin 2 to output mode

320 DrcDigitalWrite (arduino, 2, 1) // Set output High (on)

330 Wait (1)

330 DrcDigitalWrite (arduino, 2, 0) // Set output Low (off)

F.6 DrcAnalogRead

This function reads an analog channel and returns the result. The value returned willdepend on the hardware you’re connected to - for example the Arduino will return anumber from 0 to 1023 representing an input voltage between 0 and 5 volts. Otherdevices may have different ranges.

250 voltage = DrcAnalogRead (arduino, 4) / 1023 * 5 // Get voltage on pin 4

F.7 DrcPwmWrite

This procedure outputs a PWM waveform on the selected pin. The pin must be config-ured for PWM mode beforehand, and depending on the device you are using, then notall pins on a device may support PWM mode. The value set should be between 0 and255.

310 DrcPinMode (arduino, 11, 2) // Set pin 11 to PWM output mode

320 DrcPwmWrite (arduino, 11, 200)

Page 70: Return to Basics

Index

ABS, 38Acknowledgements, iiACOS, 38Apple, i, 3Aqua, 50Arguments, 37ASC, 39ASIN, 38ATAN, 38Audience, 1

BASIC, 5BBC, iBCOLOR, 24BCOLOUR, 24Black, 50Blue, 50

Calculator, 4CHR$, 40CIRCLE, 42CLEAR, 10CLOCK, 37CLS, 37COLOR, 25COLOUR, 24, 41Colours, 50Command line, 7

Edit program lines, 8Editing, 7History, 8

Comments, 11Commodore PET, iConditionals, 29

If. . . Then, 26Constants

Built-in, 23Colours, 24Keyboard Keys, 24

CONT, 10Conventions, 1Copying, iiCOS, 38

DATA, 20DATE$, 23DEG, 37DelSprite, 44DIM, 16DIR, 9DRC, 60

DrcAnalogRead, 62DrcClose, 61DrcDigitalRead, 61DrcDigitalWrite, 61DrcOpen, 60DrcPinMode, 61DrcPwmWrite, 62

ED, 10ELLIPSE, 43END, 37EXIT, 10EXP, 38

FALSE, 24File Handling, 53

CLOSE, 54EOF, 54FFWD, 55INPUT#, 55OPEN, 54PRINT#, 55

63

Page 71: Return to Basics

INDEX 64

Random, 54REWIND, 55SEEK, 55Sequential, 53

File names, 10Flow Control, 12Functions, 37

GET, 23GET$, 23GHEIGHT, 41GOSUB, 12

Controversy, 13GOTO, 12

Controversy, 13Respect, 13

GR, 42Graphics, 41Green, 50Grey, 50GWIDTH, 24, 41

HASH, 39HEIGHT, 24HGR, 42HLINE, 42HTAB, 24HVTAB, 37

IFELSE, 27ENDIF, 26Multiple Lines, 26THEN, 26

Immediate mode, 4Immediate mode commands, 9INKEY, 23Input, 17INT, 38

LEFT, 44LEFT$, 40LEN, 39LET, 15

Lime, 50LINE, 42LINETO, 42Linux, 3LIST, 9LOAD, 9LoadSprite, 44LOG, 38Loops, 32

BREAK, 36CONTINUE, 36CYCLE, 32, 34DO, 32, 34FOR, 34NEXT, 35REPEAT, 32STEP, 35UNTIL, 33WHILE, 33

Mac, 3Maroon, 50Matryoshka dolls, 47MAX, 39MID$, 40MIN, 39Modes, 4MOVE, 43MOVETO, 44

Navy, 50NEW, 9Numbers, 14Numeric Operators, 15Numerical Functions, 38NUMFORMAT, 37

Olive, 50ORIGIN, 43Output, 17

PC, 3PENDOWN, 43PENUP, 43

Page 72: Return to Basics

INDEX 65

PI, 23PI2, 23Pink, 50PLOT, 42PlotSprite, 44PolyPlot, 43PolyStart, 43Precedence, 15Preface, iProcedures, 37Prompt, 3Purple, 50

RAD, 37Raspberry Pi, 3, 58

DigitalRead, 58DigitalWrite, 59PinMode, 58PwmWrite, 59

READ, 20RECT, 43Recursion, 47

Factorial, 47Towers of Hanoi, 48

Red, 50RENUMBER, 6, 10RESTORE, 20rgbColour, 42RIGHT, 44RIGHT$, 40RND, 38RUN, 9Run mode, 4

SAVE, 9SAVENN, 9SaveScreen, 42Scientific Notation, 14SEED, 24Serial Ports, 56

SCLOSE, 57SGET, 57SGET$, 57SOPEN, 56

SPUT, 57SPUT$, 57SREADY, 57

SGN, 38Silver, 50SIN, 38Sinclair, iSPACE$, 40SQRT, 38STOP, 10, 37Stored program mode, 5STR$, 40String Functions, 40Strings, 14SWAP, 37

TAN, 38TANGLE, 24, 41TCOLOR, 24TCOLOUR, 24Teal, 50Text Screen

colour Demo, 51TIME, 23TIME$, 23Trademarks, iiTRIANGLE, 43TROFF, 10TRON, 10TRUE, 23TTHEIGHT, 24TWIDTH, 24

UPDATE, 42User defined Procedures and Functions,

45=, 46DEF, 45DEF PROC, 45Functions, 46PROC, 46Procedures, 45

VAL, 39

Page 73: Return to Basics

INDEX 66

Variables, 14Arrays, 16Assignment, 15Associative Arrays, 16Built-in, 23Map, 16Names, 14Read Only, 23Read/Write, 24Scope, 48Types, 14

VERSION, 10VLINE, 42VTAB, 24

WAIT, 37Warning, 2Warranty, iiWhite, 50

Yellow, 50