Books in black and white
 Books Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

# Electronics for dummies - McComb G.

McComb G., Boes E. Electronics for dummies - Wiley publishing, 2005. - 433 p.
ISBN: 0-7645-7660-7
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 149 >> Next

TERM LinG - LIVE, Informative, Non-cost and ÁÅÏÛ1ÏÅ !
Chapter 15: Cool Robot Projects to Amaze Your Friends and Family 355
Making the Smart Rover Smart
Now the BASIC Stamp has to endow the Smart Rover with some brains. Check out the program listing below. It shows you how the program operates the two servos and reacts to the leaf switch:
The program starts both servos, propelling the robot forward.
If the robot hits something, the impact triggers the switch, and the program reverses one of the motors.
The program reverses that motor for about three quarters of a second, which makes the robot spin around.
The robot moves forward again until it, inevitably, hits something else.
Putting the program in place
In this section, we give you the program that you need to get your Rover
rolling. Enter it into the BASIC Stamp editor, as we describe in Chapter 13, and run the program when you’re done; this uploads it to your ‘bot. Don’t forget that you need to connect your PC to the Board of Education through a serial or USB cable, depending on the BOE version you have. Read Chapter 13 for the complete picture.
' {\$STAMP BS2}
OUTPUT 0
btn VAR Byte ’ set up BUTTON variable
cnt VAR Byte ’ set of FOR/NEXT variable
loop:
PULSOUT 12,1000 ’ motor A
PULSOUT 14,500 ’ motor B
PAUSE 15 ’ wait 15 milliseconds
BUTTON 1,0,255,250, , btn,0,noSwitch
OUT0 = btn ’ turn LED on
FOR cnt = 1 TO 50 ’ count to 50 iterations
PULSOUT 12,1000 ’ motor A
PULSOUT 14,1000 ’ motor B
PAUSE 15 ’ wait 15 milliseconds
NEXT
OUT0 = 0 ’ turn LED off
noSwitch: GOTO loop ’ repeat loop
Hmmm. You say your robot goes backward, rather than forward? You can fix that problem easily. Simply reverse the timing instructions in the program for motor A and motor B immediately following the loop: label:
TERM LinG - LIVE, infDrmatlVE, NDn-CD5t and ÁÅÏÛ1ÏÅ !
356 Part V: A Plethora of Projects
PULSOUT 12,500 ’ motor A
PULSOUT 14,1000 ’ motor B
Looking at the program up-close
Taking a closer look at how the Smart Rover program works helps you become more programming savvy. Here’s a blow-by-blow description of what each line does:
' {\$STAMP BS2}
This line tells the BASIC Stamp editor that you’re using BASIC Stamp 2.
OUTPUT 0
This line tells the BASIC Stamp to treat I/O port 0 as an output. The indicator LED connects to I/O port 0, and the program control turns the LED on and off.
btn VAR Byte
cnt VAR Byte
These lines set up the BASIC Stamp with two variables. These variables ensure that temporary data is stored until it’s used again later in the program.
loop:
This line is the main loop of the program. It tells the BASIC Stamp to repeat the instructions from this point to the GOTO loop instruction at the bottom of the program. These instructions repeat over and over again. Get used to this command if you plan to do much ‘bot building — almost all robot control programs have one.
PULSOUT 12,1000 ’ motor A
PULSOUT 14,500 ’ motor B
Pulses operate R/C servos. The length of the pulse determines the direction of travel. The PULSOUT programming statement sends a pulse of a specified duration to the indicated I/O port. For example, PULSOUT 12,1000 sends a 2,000-microsecond pulse to I/O port 12. (You specify the pulse duration in 2-microsecond increments: 1000 in the code equals 2000 microseconds.) But, hey, motor A pulses at 2000 microseconds, while motor B pulses for only 1000 microseconds. Why? Because you mount the motors in mirror image.
To move the robot forward, one motor has to turn clockwise, and the other turns counter-clockwise.
PAUSE 15
TERM LinG - Live, informative, Non-cost and Genuine !
Chapter 15: Cool Robot Projects to Amaze Your Friends and Family 357
Setting the exact center of your servos
When an unmodified R/C servo motor receives a series of pulses 1000 microseconds long, it moves as far as it can go in one direction. When it receives a series of pulses 2000 microseconds long, it moves as far as it can go in the other direction. As you may have guessed, when R/C servos receive a series of pulses 1500 microseconds long, the motors move to the center position. Makes sense, doesn't it?
When you modify your two servos, as we describe in the "Modifying the R/C servo motors" section of this chapter, you set the potentiometer to its center position. Well, there's physical center, and then there's electrical center. You only move the potentiometer to its center physical position, but not its electrical center position. You can make your Rover easier to control by setting the potentiometer to its electrical center. You do this Rover taming by running a short program and turning the potentiometer until all motor activity stops. Of course, you have to disassemble the servos again to reach the potentiometer.
Here's the program to use for this positioning (the program assumes that you have the servos connected to the Board of Education, as we discuss in the "Connecting Up to the Board of Education" section in this chapter):
Previous << 1 .. 122 123 124 125 126 127 < 128 > 129 130 131 132 133 134 .. 149 >> Next