Programming the Cheap Bot14 Start the Editor Set
Programming the Cheap. Bot-14
Start the Editor
Set the Mode
Set the Serial Port
Syntax Check
Make your Robot Controller Counting: DEBUG B 0 = B 0 + 1 GOTO Counting • Look at B 0 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 • BIT 0 to BIT 31 • B 0 to 27 • W 0 to 13 BIT 0 = 1 B 5 = 215 W 2 = 2000
RAM Variables
Input/Output Pins • The Cheap. Bot-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 (+5 V) • 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 m. A
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 B 0) • Incrementing (adding 1) • Decrementing (subtracting 1) B 0 = B 0 + 1 B 2 = B 2 - 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 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. Clock. Wise: Counter. Clock. Wise: Coast: Brake: HIGH B. 2 LOW B. 2 HIGH B. 2 LOW B. 3 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 Cheap. Bot-14: some code more code GOTO Cheap. Bot-14
- Slides: 32