Robotics boe bot - Georgia Southern University...

23
ENGR 2110 ENGR 2110 PBASIC programming

Transcript of Robotics boe bot - Georgia Southern University...

ENGR 2110ENGR 2110

PBASIC programming p g g

BOE BOTBOE BOTBOE BOTBOE BOT

Your Your boeboe--botbot has 15 has 15 pins which whose pins which whose voltages can voltages can be be vvaried aried depending on the depending on the gg p gp gcommand used.command used.

The PBASIC Editor WindowThe PBASIC Editor Window

Variable DeclarationVariable DeclarationVariable DeclarationVariable Declaration

A variable in PBASIC is declared with the syntax of: A variable in PBASIC is declared with the syntax of: variablename VAR size, for examplevariablename VAR size, for example

Variable TypesVariable TypesIn PBASIC, all variables and their sizes must be declared at the beginning of the program. The possible sizes of variables are

If a variable is assigned a number outside the allowable range for its size the editor will not return an error but the programfor its size, the editor will not return an error, but the program will behave erratically.

Variable Example Variable Example pp

' {$STAMP BS2}' {$STAMP BS2}

' {$PBASIC 2.5}

value VAR Word ' Declare variables

anotherValue VAR Word

value = 500 ‘ Initialize variables

anotherValue = 2000

DEBUG value ' Display values

DEBUG anotherValueDEBUG anotherValue

value = 10 * anotherValue ' Perform operations

DEBUG value ' Display values again

DEBUG anotherValue

END

Variables can be read and modified. Variables can be read and modified.

'Prog 5B: Test of variable sizes

' *********************** Declare Variables Declare VariablesByteCount VAR BYTEWordCount VAR WORDBitCount VAR BITNibCount VAR NIB

Main:WordCount = WordCount + 1000 'Add to each variableByteCount = ByteCount + 20NibCount = NibCount + 1NibCount NibCount + 1BitCount = BitCount + 1DEBUG CLS 'Clear the screenDEBUG ? WordCount : DEBUG ? ByteCount: DEBUG ? NibCount: DEBUG ? BitCountPAUSE 500

GOTO M iGOTO Main

Colons may be used to separate instructions on a single line

PAUSEPAUSEPAUSEPAUSE

The PAUSE command suspends theThe PAUSE command suspends the execution of a program a specified amount of timeof time.

DEBUGDEBUGDEBUGDEBUG

The DEBUG command is used solelyThe DEBUG command is used solely for the purpose of observing the execution of a programexecution of a program

not able to change values of variables or affect the flow of a program.

DEBUGDEBUGDEBUGDEBUGDEBUG “text to be displayed” Prints the string between the quotes to the debug windowbetween the quotes to the debug window.

DEBUG dec variable Prints the value ofDEBUG dec variable Prints the value of variable to the debug window.

DEBUG “ ” i th i tDEBUG cr “cr” is the carriage return parameter. It moves the cursor the next line in the debug window.g

DEBUG cls Clears the debug window screen.

DEBUG exampleDEBUG exampleDEBUG exampleDEBUG examplevariable VAR byte

i bl 27variable = 27

DEBUG cls, “The value of the variable is: ”, dec variable, crDEBUG “This is the second line.”DEBUG “V i bl ” d i blDEBUG “Variable =”, dec variable

ASCIIASCIIASCIIASCIIASCII is short for American Standard Code ASCII is short for American Standard Code for Information Interchange. for Information Interchange.

Most microcontrollers Most microcontrollers and PC computers and PC computers use this code to assign a number to eachuse this code to assign a number to eachuse this code to assign a number to each use this code to assign a number to each keyboard function.keyboard function.

Stamp Stamp will read will read any any number number entered as entered as an an ASCII ASCII code code

ENDENDThis command puts the BASIC Stamp into low power This command puts the BASIC Stamp into low power mode when it’s done running the program. mode when it’s done running the program.

In low power mode, the BASIC Stamp waits for either the In low power mode, the BASIC Stamp waits for either the Reset button to be pressed (and released), or for a new Reset button to be pressed (and released), or for a new program to be loaded into it by the BASIC Stamp Editor. program to be loaded into it by the BASIC Stamp Editor. p g y pp g y p

If the Reset button on your board is pressed, the BASIC If the Reset button on your board is pressed, the BASIC Stamp will run the current program. If a new program is Stamp will run the current program. If a new program is loaded into it, the old one is erased, and the new program loaded into it, the old one is erased, and the new program begins to run.begins to run.beg s o u .beg s o u .

FOR..NEXT LOOPFOR..NEXT LOOP

The most convenient way to control the number of times a piece of code is executed is with a FOR…NEXT loop. Here is the syntax:

FOR Counter = StartValue TO EndValue {STEP stepValue}…NEXT

' {$STAMP BS2} {$STAMP BS2}

' {$PBASIC 2.5}

myCounter VAR Word

FOR myCounter = 1 TO 10y

DEBUG DEC myCounter

PAUSE 500

NEXT

DEBUG CR, "All done!"

END

Do..LOOPDo..LOOP

You can place your commands between the words DO and LOOP if you want themexecuted over and over again.

O OODO…LOOP

' Display a message once every second.

' {$STAMP BS2}

' {$PBASIC 2.5}

DO

DEBUG "Hello!", CR

PAUSE 1000

LOOP

Do LoopDo LoopDo…LoopDo…Loop

An EXIT command can be issued insideAn EXIT command can be issued inside the DO . . .LOOP command

variable VAR byteDOIF variable = 10 THEN EXITIF variable 10 THEN EXITLOOP

IF..THENIF..THEN

The IF…THEN statement is used to make decisions in PBASIC . The syntax for IF…THEN statements is:

IF (condition) THEN…{ELSEIF (condition)}…{ELSE}…ENDIFIF (condition) THEN…{ELSEIF (condition)}…{ELSE}…ENDIF

IF (IN7 = 0) THEN

HIGH 1

ELSE

LOW 1

ENDIF

IF (IN5 = 0) THEN

HIGH 10

ELSE

LOW 10

ENDIF

What would this do?What would this do?What would this do?What would this do?variable VAR word

IF (variable > 4000) THENDEBUG “Variable was greater than 4000”, crELSEIF (variable = 4000) THENDEBUG “V i bl l t 4000”DEBUG “Variable was equal to 4000”, crELSEDEBUG “Variable as less than 4000” crDEBUG “Variable was less than 4000”, crENDIF

Subroutine Subroutine

There are two parts of a PBASICThere are two parts of a PBASIC subroutine.

Call

Actual Subroutine

Subroutine Subroutine

It starts with a label that serves as its name and ends with a RETURN command.

The commands between the label and the RETURN command is the code block that does the job you want j ythe subroutine to do.

Subroutines Subroutines k f d i bl b i' Make forward movement in reusable subroutines.

' {$STAMP BS2}

' {$PBASIC 2.5}

counter VAR Word

GOSUB ForwardGOSUB Forward

END

F dForward:

FOR counter = 1 TO 64

PULSOUT 13, 850

PULSOUT 12, 650

PAUSE 20

NEXTNEXT

PAUSE 200

RETURN

Subroutines Subroutines

DO

DEBUG "B f b ti " CRDEBUG "Before subroutine",CR

PAUSE 1000

GOSUB My_Subroutine

DEBUG "After subroutine", CR

PAUSE 1000

LOOP

My_Subroutine:

DEBUG "Command in subroutine" CRDEBUG "Command in subroutine", CR

PAUSE 1000

RETURN

Blinking an LED with HIGH, Blinking an LED with HIGH, LOWLOW

Main:HIGH 8 'Turn off LEDPAUSE 1000 'Wait 1 secondPAUSE 1000 Wait 1 secondLOW 8 'Turn on LEDPAUSE 5000 'Wait 5 seconds

GOTO Main 'Jump back to beginning