420 likes | 550 Views
Chapter 0: Programming BASICs. Presentation based on: "Robotics with the Boe-Bot" By Andy Lindsay Parallax, Inc. 7/6/04. MPC ROV Team Forming. Contact Jeremy jhertzberg@mpc.edu For more info. Watch their amazing night dive in Monterey Bay! http://www.youtube.com/watch?v=lFYAUklUxiQ.
E N D
Chapter 0: Programming BASICs Presentation based on:"Robotics with the Boe-Bot" By Andy LindsayParallax, Inc 7/6/04
MPC ROV Team Forming • Contact Jeremy jhertzberg@mpc.edu For more info Watch their amazing night dive in Monterey Bay! http://www.youtube.com/watch?v=lFYAUklUxiQ
Today’s Agenda • Overview of STAMP Hardware • Basic Electronics • Build an LED circuit • Program the STAMP to flash LED • Build a Speaker Circuit • Program the STAMP to play music • Skills: • Breadboarding • Use of variables • Use of Control Structures (DO, IF, FOR)
BASIC Stamp Module A microcontroller, which is like a very small computer, can be programmed to perform essential tasks: • Monitor sensors to detect the world around it. • Make decisions based on what it senses. • Control motion. • Exchange information with its Roboticist.
Basic Stamp II • Self contained computer • “Micro-controller” • Specialized for “embedded”computing (sensing and controlling things) • Built in programming language • PicBasic (interpreted) • Small programming environment runs on a PC • Connected with a serial cable
Parallax Basic Stamp II } { • PIC processor • (Very) roughly the computing power on the lunar module (much faster, but much less memory) • 1 million instructions / sec • Non-volatile memory • Power regulation • Serial interface
Parallax Basic Stamp II • Input/output pins • 16 total (P0…P15) • This is where the action is • Each pin can be accept input or produce output • Logic levels (0 or +5v) • Also “tricks” for sensing and producing values in between
BASIC Stamp and the Board of Education The Board of Education provides a platform for programming and connecting devices to the controller. Breadboard
Basic Electronics • Voltage (V or E): Potential difference between 2 points. Volts. • (+) - A lack of electrons • (-) – A surplus of electrons • When a circuit is formed, electrons will flow to equalize out the 2 sides. • Current (I): The measure of the flow of electrons. Amps. • Resistance (R): Opposition to current flow. Ohms. • Ohm's Law: I = V/R
Resistors • Devices used to insert resistance, or oppose current flow. Used to limit current flow. • Color codes are used to indicate the value.
The LED • LED: Light Emitting Diode • Diode: Device which allows current to flow in only one direction. • To operate, is must be installed correctly with respect to polarity.
A regular LED can be damaged if current exceed 20mA (milliamps). • Apply ohm's law to find the resistor size that will limit current to 20mAwith a 5V supply.I = V/R
The Breadboard • Breadboards are used to make connections between devices. Battery + Voltage +5V Ground (Return to battery --) These Connect To BASIC Stamp I/O Pins
Check out this video on Breadboards ! • http://www.youtube.com/watch?v=q_Q5s9AhCR0
Building the Circuit Use 220 Ohms for a brighter LED (Less resistance = more current)
Pictorial or Wiring Diagram Remember, Long LED wire goes on bottom for this circuit! 470 Ohms 220 Ohms are colored Red-Red-Brown
Connect Components Note the direction of the BASIC Stamp Module
Hardware and Software BASIC Stamp, programming board, robot parts, and various electronic parts are the hardware. Software is used to instruct the BASIC Stamp what to do. Programs are written on the PC and transferred to and run on the BASIC Stamp.
Place the switch (if you have one!) in programming mode 1 (motors disabled). • Power light on the board will come on.
Your First Program • Enter the program • Shortcuts for device and language:
Save your program as"HelloBoeBot.bs2" • "Run" your program.
Code Discussion • Comments – used to explain or comment code – start with apostrophe (') • Directives – such as '{$STAMP BS2} – used to inform the program of something special, such as the device being used. • Commands – Word used to instruct the BASIC Stamp what to do:DEBUGEND
Code Discussion • HIGH defines the pin to be an output and sets it to a HIGH state, digital 1 or 5V. • HIGH pin 0-15 • HIGH 13 • LOW defines the pin to be an output and sets it to a LOW state, digital 0 or 0V. • LOW pin 0-15 • LOW 13 • PAUSE instructs the BS2 to wait for the defined number of milliseconds (1/1000 seconds). • PAUSE time in milliseconds 0-65535 • PAUSE 500 • DO and LOOP instructs the BS2 to repeat the enclosed statements. More about this will be covered in Programming Structures.
Challenge : Blink the 2nd LED • Modify the program to blink only the second LED using HIGH and LOW instructions. • Then Modify to blink both LEDs on and off at the same time
Challenge : LED Cycling • Code a program to perform the following sequence (use HIGH and LOW): • LED1 on P12 ON, LED2 on P13 OFF • Wait 2 seconds • LED1 on P12 ON, LED2 on P13 ON • Wait 1 second • Both LEDs OFF • Wait one-half second • Repeat
Next Up – The Speaker/Buzzer • A small piezoelectric speaker is included with your kits. • A frequency can be sounded on it using the FREQOUT command.FREQOUT pin, duration, frequency • pin: Pin it is connected to. • duration: Length of time in milliseconds. • frequency: Frequency to play, measured in Hertz (Hz).
Sound the Speaker for 1 second at 1000Hz freqout 2, 1000, 1000
Musical And BASIC Programming • We can use freqout to demonstrate different programming concepts. • You’ll be able to “Hear” what your code is doing. • Helps to understand logic of your code.
A. Simple Program ' {$STAMP BS2} ' Tell what version Stamp we are using ' {$PBASIC 2.5} ' Using latest version 2.5 software FREQOUT 2, 500, 3000 'Send a 3000 Hz signal to pin 2 for 500 msec END 'End of program. Turns robot off Activity 1) make the robot play a 1000 Hertz tone for five seconds. 2) Make the robot play three notes: 1000, 1500, and 3000 Hertz for two seconds each. Which one is loudest? Your speaker has a filter effect. 3) Change the notes to any other three frequencies you like
B. DO-LOOP a structure that repeats code indefinitely. Change your program for A to look like this instead: DO 'Start of loop FREQOUT 2, 500, 1500 '1500 Hz tone for 0.5 sec FREQOUT 2, 1000, 3000 ‘ 3000 Hz tone for 1 sec LOOP END 'End of program. Turns robot off – unreachable now TIP: Always indent code that appears in a loop or other control structure
B. Activity 4) Make the robot repeat your tone pattern from 3) 5) Make a folder called Lab0 and Save this as program sound1.bs2 inside it 6) Now save the program again as sound2.bs2 so we can make changes to it.
Storing and Retrieving Values • Variable are used to store values • Variables must be declared prior to being used. Declaring a variable is giving it a name, and a size to hold.VariableName var Size
C. Variables name of a place in memory where a number is stored. Delete all but top 2 lines and add the following: frq VAR Word ' frq can hold a value 0 to 65535 n VAR Byte ’n can hold a value 0 to 255 frq = 1500 'assign a value of 1500 to frq FREQOUT 2, 500, frq 'now send a tone pitch at value of frq (1500) frq = 2000 'change frq to 2000 FREQOUT 2, 500, frq 'now send a tone pitch at value of frq (2000)
Counting and Controlling Repetitions • FOR…NEXT loops can be used to control the number of repetitions a segment of code takes.FOR StartValue TO EndValue {STEP StepValue}…NEXT