Chapter 0: Programming BASICs

42
1 Chapter 0: Programming Chapter 0: Programming BASICs BASICs Presentation based on: "Robotics with the Boe-Bot" By Andy Lindsay Parallax, Inc 7/6/04

description

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 [email protected] For more info. Watch their amazing night dive in Monterey Bay! http://www.youtube.com/watch?v=lFYAUklUxiQ. - PowerPoint PPT Presentation

Transcript of Chapter 0: Programming BASICs

Page 1: Chapter 0: Programming BASICs

1

                      

Chapter 0: Programming Chapter 0: Programming BASICsBASICs

Presentation based on:

"Robotics with the Boe-Bot"By Andy LindsayParallax, Inc

7/6/04

Page 2: Chapter 0: Programming BASICs

2

                      

MPC ROV Team FormingMPC ROV Team Forming

[email protected]

duFor more info

Watch their amazing night dive in Monterey

Bay!

http://www.youtube.com/

watch?v=lFYAUklUxiQ

Page 3: Chapter 0: Programming BASICs

3

                      

Today’s AgendaToday’s Agenda

Overview of STAMP HardwareBasic ElectronicsBuild an LED circuitProgram the STAMP to flash LEDBuild a Speaker CircuitProgram the STAMP to play musicSkills:

• Breadboarding• Use of variables• Use of Control Structures (DO, IF, FOR)

Page 4: Chapter 0: Programming BASICs

4

                      

BASIC Stamp ModuleBASIC 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.

Page 5: Chapter 0: Programming BASICs

5

                      

Basic Stamp IIBasic Stamp II

Self contained computer• “Micro-controller”

Specialized for “embedded”computing (sensing and controlling things)

• Built in programming languagePicBasic (interpreted)Small programming environment runs on a

PC• Connected with a serial cable

Page 6: Chapter 0: Programming BASICs

6

                      

Parallax Basic Stamp IIParallax Basic Stamp II

PIC processor• (Very) roughly the

computing power on the lunar module (much faster, but much less memory)

• 1 million instructions / secNon-volatile memoryPower regulationSerial interface

{{ }}

Page 7: Chapter 0: Programming BASICs

7

                      

Parallax Basic Stamp IIParallax Basic Stamp II

Input/output pins• 16 total (P0…P15)• This is where the action

is• Each pin can be accept

input or produce outputLogic levels (0 or +5v)Also “tricks” for sensing

and producing values in between

Page 8: Chapter 0: Programming BASICs

8

                      

BASIC Stamp and the Board of EducationBASIC Stamp and the Board of Education

The Board of Education provides a platform for programming and connecting devices to the controller.

BreadboarBreadboardd

Page 9: Chapter 0: Programming BASICs

9

                      

Basic ElectronicsBasic 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

Page 10: Chapter 0: Programming BASICs

10

                      

Page 11: Chapter 0: Programming BASICs

11

                      

ResistorsResistors

Devices used to insert resistance, or oppose current flow. Used to limit current flow.

Color codes are used to indicate the value.

Page 12: Chapter 0: Programming BASICs

12

                      

From Appendix C

Page 13: Chapter 0: Programming BASICs

13

                      

The LEDThe LED

LED: Light Emitting DiodeDiode: Device which allows current

to flow in only one direction.To operate, is must be installed

correctly with respect to polarity.

Page 14: Chapter 0: Programming BASICs

14

                      

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

Page 15: Chapter 0: Programming BASICs

15

                      

LED Circuit SchematicLED Circuit Schematic

Page 16: Chapter 0: Programming BASICs

16

                      

The BreadboardThe Breadboard

Breadboards are used to make connections between devices.

TheseTheseConnectConnect

To To BASICBASICStampStamp

I/OI/OPinsPins

Battery + Battery + VoltageVoltage

+5V+5VGroundGround

(Return to (Return to battery --)battery --)

Page 17: Chapter 0: Programming BASICs

17

                      

Check out this video on Check out this video on Breadboards !Breadboards !

http://www.youtube.com/watch?v=q_Q5s9AhCR0

Page 18: Chapter 0: Programming BASICs

18

                      

Building the CircuitBuilding the Circuit

Use 220 Ohms Use 220 Ohms for a brighter for a brighter

LEDLED(Less resistance (Less resistance = more current)= more current)

Page 19: Chapter 0: Programming BASICs

19

                      

Pictorial or Wiring DiagramPictorial or Wiring Diagram

Remember,Remember,Long LED Long LED wire goes wire goes on bottom on bottom

for this for this circuit!circuit!

470 Ohms 470 Ohms

220 Ohms are 220 Ohms are colored colored

Red-Red-BrownRed-Red-Brown

Page 20: Chapter 0: Programming BASICs

20

                      

Connect Components

Note the direction of the Note the direction of the BASIC Stamp ModuleBASIC Stamp Module

Page 21: Chapter 0: Programming BASICs

21

                      

Hardware and SoftwareHardware 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.

Page 22: Chapter 0: Programming BASICs

22

                      

Place the switch (if you have one!) in programming mode 1 (motors disabled).

Power light on the board will come on.

Page 23: Chapter 0: Programming BASICs

23

                      

Open the software and test communications.

Page 24: Chapter 0: Programming BASICs

24

                      

Your First ProgramYour First Program

Enter the programShortcuts for device and language:

Page 25: Chapter 0: Programming BASICs

25

                      

Save your program as"HelloBoeBot.bs2"

"Run" your program.

Page 26: Chapter 0: Programming BASICs

26

                      

Debug Window appears with message.

Page 27: Chapter 0: Programming BASICs

27

                      

Code DiscussionCode 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

Page 28: Chapter 0: Programming BASICs

28

                      

Code to Blink LEDsCode to Blink LEDs

Page 29: Chapter 0: Programming BASICs

29

                      

Code DiscussionCode 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.

Page 30: Chapter 0: Programming BASICs

30

                      

Challenge : Blink the 2Challenge : Blink the 2ndnd LED 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

Page 31: Chapter 0: Programming BASICs

31

                      

Challenge : LED CyclingChallenge : 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

Page 32: Chapter 0: Programming BASICs

32

                      

Next Up – The Speaker/BuzzerNext 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).

Page 33: Chapter 0: Programming BASICs

33

                      

Add the Piezo speaker circuit to your breadboard

Page 34: Chapter 0: Programming BASICs

34

                      

Sound the Speaker for 1 second at 1000Hz

freqout 2, 1000, 1000

Page 35: Chapter 0: Programming BASICs

35

                      

Musical And BASIC ProgrammingMusical 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.

Page 36: Chapter 0: Programming BASICs

36

                      

A. Simple Program 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 Activity1)      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

Page 37: Chapter 0: Programming BASICs

37

                      

B. DO-LOOPB. 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 secLOOP END       'End of program.  Turns robot off – unreachable now

TIP: Always indent code that appears in a loop or other control structure

 

 

Page 38: Chapter 0: Programming BASICs

38

                      

B. ActivityB. 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.

Page 39: Chapter 0: Programming BASICs

39

                      

Storing and Retrieving ValuesStoring and Retrieving Values

Variable are used to store valuesVariables must be declared prior to

being used. Declaring a variable is giving it a name, and a size to hold.VariableName var Size

Page 40: Chapter 0: Programming BASICs

40

                      

C. VariablesC. 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 65535n VAR Byte             ’n can hold a value 0 to 255

frq = 1500 'assign a value of 1500 to frqFREQOUT 2, 500, frq 'now send a tone pitch at value of frq

(1500)

frq = 2000 'change frq to 2000FREQOUT 2, 500, frq 'now send a tone pitch at value of frq

(2000)

Page 41: Chapter 0: Programming BASICs

41

                      

Counting and Controlling Counting and Controlling RepetitionsRepetitionsFOR…NEXT loops can be used to

control the number of repetitions a segment of code takes.

FOR StartValue TO EndValue {STEP StepValue}…NEXT

Page 42: Chapter 0: Programming BASICs

42

                      

Continue Lab 0 with Step D on Continue Lab 0 with Step D on handouthandout