Upload
nora-marsh
View
233
Download
1
Tags:
Embed Size (px)
Citation preview
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
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
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)
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
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
Token: PAUSE
• The PAUSE command stops the PICAXE from executing anymore commands for a specific length of time.
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