Programming the CheapBot-14. Start the Editor Set the Mode.

32
Programming the CheapBot-14

Transcript of Programming the CheapBot-14. Start the Editor Set the Mode.

Programming theCheapBot-14

Start the Editor

Set the Mode

Set the Serial Port

Syntax Check

Make your Robot Controller Count

Counting:

DEBUG

B0 = B0 + 1

GOTO Counting

• Look at B0 in the pop-up Debug window

Why does this work?

Programs

• List of statements

• The statements follow rules

• Statements are executed one after the other

Tokens and Syntax

• Tokens are elements (words) of the programming language

• Syntax is the rules for combining tokens

A Robot Program Has…

• Input

• Output

• Math

• Conditional Execution

• Repetition

RAM Variables

• Numbers that your program creates and updates must be stored in RAM

• RAM can be updated many times in a program

• Each RAM variable has a name that acts like the address of the variable

Token: RAM Variables

• Bit (BIT)

• Byte (B)

• Word (W)

Syntax: RAM Variables

• BIT0 to BIT31

• B0 to 27

• W0 to 13

BIT0 = 1

B5 = 215

W2 = 2000

RAM Variables

Input/Output Pins

• The CheapBot-14 robot controller has 10 pins that connect it to the world

• Four control the motors (B.2, B.3, B.4, B.5)

• Six are for sensors and actuators (C.0, C.1, C.2, C.3, C.4, B.1)

Token: HIGH and LOW

• I/O pins can only be ON or OFF

• ON means an I/O pin has 5 volts

• OFF means an I/O pin has 0 volts

• HIGH turns an I/O pin on (+5V)

• LOW turns an I/O pin off (ground)

Syntax: HIGH and LOW

HIGH B.4

LOW B.5

HIGH and LOW Notes

• A HIGH I/O pin is a source

• A LOW I/O pin is a sink

• There must be a resistance on an I/O pin before sourcing or sinking current

• Maximum current is 30 mA

Token: Math

• Numbers exist in RAM variables

• Math is carried out in RAM variables

Syntax: Math

• Numbers (whole, positive amounts) can be added or subtracted from a value stored in RAM (like B0)

• Incrementing (adding 1)

• Decrementing (subtracting 1)

B0 = B0 + 1

B2 = B2 - 2

Token: IF-THEN

• Conditional execution (two different ways)

• If condition is true, then jump execution to a label

• If condition is true, then execute a block of code

Syntax: IF-THEN

IF PINB.1 = 1 THEN Turn_Right

IF PINC.3 = 0 THEN

HIGH B.2

LOW B.3

PAUSE 100

ENDIF

Light an LED

• What command turns on the LED?

• What command turns it back off?

Token: PAUSE

• The PAUSE command stops the PICAXE from executing anymore commands for a specific length of time.

Syntax: PAUSE

PAUSE 1000

• Units of pause in milliseconds with the maximum being 65,535 ms

Blink the LED

• Use the following commands to blink the LED

– Label– PAUSE– HIGH– LOW– GOTO

Making an H-Bridge Drive a Motor

• An H-Bridge makes a motor rotate clockwise, counter-clockwise, or stop based on its two inputs.

ClockWise:

HIGH B.2

LOW B.3

CounterClockWise:

LOW B.2

HIGH B.3

Coast:

LOW B.2

LOW B.3

Brake:

HIGH B.2

HIGH B.3

Token: GOSUB

• Jump execution to a subroutine

• Saves program memory

• Makes it easier to understand a program

What is a Subroutine?

• Subroutines are a simple way to call a series of commands that are used frequently

• By replacing all the code with calls to subroutines, you make your program smaller

Syntax: Subroutines

• Begins with the name of subroutine

(a label)

• Ends with a command to go back to where it was called

(RETURN)

• Between the label and the RETURN is the code you want to execute in the subroutine

• Called with the GOSUB command

Example of a Subroutine

- some code -

GOSUB Rotate

- rest of code goes here -

Rotate:

HIGH B.2

LOW B.3

PAUSE 1000

RETURN

Token: GOTO

• Example of code repetition

• Unconditional

Syntax: GOTO

CheapBot-14:

some code

more code

GOTO CheapBot-14