Sie sind auf Seite 1von 234
THE ART OF LEGO MINDSTORMS EV3 PROGRAMMING “THE ART OF LEGO" MINDSTORMS" EV3 PROGRAMMING. Copyright © 2014 ty Terry Gettin ‘Alvrights reserved. No part of hi work maybe reproduced or transmitted in ny form or by any mens, electronic or mechanical, including photocopying, recording, or by any infarmation storage or retrieval system without the prior writen permission ofthe eapyright owmer and the publisher Print in USA Fst grnting 38171615146 123456789 I5QN-10:4-59927-568-4 I5@N-13:978-1-59327-S68-6 Publcher Wiliam Pallock Production Editor Laurel Chun Interior Design: Octopod Studios Developmental Eetors Sanh Kramer and lennfer Grifth-Delgad | “Technical Reviewers: Daniele Benedetelliand Rob Torok Copyeitor: Gillan MeGarvey Compasitors: Lynn UHeureux and Riley Hoffman Proofreader: Emelie Burnette For information on distribution, translations, or bul sles plesse contact No Starch Pres, Ine, drectiy No Starch Press, in. 245 Buh Street, San Francesa, CA 9403 prone: 415.8689900; mfo@nostarch cm The Library of Congress has cataloged the rst edlivon as flows Grin, Terry 1962 ‘The art of LEGO Mindstorms NXT-G programming /Tery Griffin pem Inches dex I5aN-13 9781-59927. I5QN-10,2-59327-218-9 11. Robots--Design and construction, 2, Robote--Programming. 3, Lago Mindstorms toys. Tile 211.675 2020 62989252--de22 18.0 2010017757 [No Storch Press and the He Stary Press logo ott registered trademarks of No Starch Press. ns. Other protuct and com paty names mentaned herein may Bethe trademarks of their espactive owners, Rather than use a trademark symbol with every occurrence ofa trademarked name, ste are sing the names only man editorial fashion and tothe benoit of the trademark owner, with na intention of infringement of te fader LEGO", MINDSTORMS", the brick contguraton, and the minioute are trademarks of the LEGO Group, which doesnot sponsor, authorize, or endorse thes book ‘The information in this book's dstnibuted an an "ss" Bess, without warranty While every precaution has been token in th preparation of ths work. nether the author nor No Starch Press, Ic. shall havo any Vablity to any Berson or ent with respect to any loss ar damage caused o alleged tobe caused ereetly or indirectly by the information eantined init yrightod material introduction. chapter 1 chapter 2 chapter 3 chapter 4 chapter 5 chapter & chapter 7 chapter 8 chapter 9 chapter 10 chapter 11 chapter 12 chapter 13 chapter 16 chapter 15 chapter 16 chapter 17 chapter 18 chapter 19 appendix A appendix B inden brief contents LEGO and robots: @ great combination ‘the EV3 programming environment. ‘TriBot: the test robot. motion. program flow. the WallFollower program: navigating = maze data wires... _ data wires and the switch block data wires and the loop block. variables ry blocks. math and lagic the EV3 lights, buttons, and display su. arrays i files, data logging rmutitasking cnn ‘a PID-controlled LineFellawer program NXT and £V3 compatibility EV3 websites, Copyrighted material contents in detail introduction . xi ‘who this boak is for xi prerequisites. i what to expect from this book xi how best to use this book a LEGO and robots: a great combination. LEGO MINDSTORMS EV3 the LEGO MINDSTORMS EV3 set the LEGO MINDSTORMS EV3 software van. software, firmware, and hardware art and engineering Gualities of a good program ‘what youll learn from this book. the LEGO MINDSTORMS online community what's next? 2 the EV3 programming environment .. 2 tour through the MINDSTORMS software ‘the programming canvas B: the content editor. G the programming palettes Os the hardware page... E: the download and run buttons. writing an EV3 program. general layout of a block. your first program saving your work running your program. project properties... making back-up copies your second program comments adding a comment tips for working with comments context help. conclusion. 3 TriBot: the test robot. Trifict components. building the metor and wheel assembly building the caster wheel assembly. ‘assembling the caster whee! with the home edition assembling the caster whee! with the education edition, adding the EV3 brick. ‘mounting the infrared or ultrasonic sensor. connecting the color sensor. attaching the gyra sensor (education editian) building a touch sensor bumper. ‘attaching the cables. attaching the touch sensor. attaching the infrared or ultrasonic sensor. attaching the color sensor. attaching the gyro sensor {education edition) attaching the motors. alternate placement for the color sensor. alternate placement for the ultrasonic or infrared sensor. building a lift arm, conclusion & ‘motion. the V3 motors. the move steering block. mode steering power duration brake at end port port view. the EV3 intelligent brick view menu the ThereAndBack program... ‘moving forward. turning around testing a single block moving back to the start the AroundTheBlock program the first side and earner the other three sides and corners testing the program nus the mave tank block the large motor and medium motar blocks, the lift arm... the invert mater block ‘a problem with coasting further exploration, contluston 5 sensors. Using the sensors the touch sensor. the BumperBot prooram. moving forward. detecting an obstacle. backing up and turning around challenge 5-1... challenge 5-2. testing. the color senser. color made. reflected light intensity mode ambient light mtensity Ode nes port view. si challenge 5-3 the [sltBlue program the switch black Improving the program the LineFinder program, Using port view to find @ threshold value challenge 5-6... challenge 5-5. the infrared sensor and remote. proximity mode beacon heading and beacon proximity mode, remote made the BumperBotWithButtons program... challenge 5-6. the ultrasonic sensor distance inches and distance centimeters modes presencellisten mode. the DaorChime program detecting a person playing a chime stopping the chime, the gyra sensor rate mode. angle mode, resetting the angle. the GyroTurn program challenge 5-7. the mater ratation sensor. the BumperBat2 program further exploration, conelusien. 6 program flow the switch block setting the condition resizing a black. the LineFollewer pregram the basic program selecting the color sensor threshold configuring the move blocks testing the program mare than two choices. challenge 6-1 testing the program using tabbed view the RedOrBiue program identifying red objects adding a new case challenge 6-2. the default case the loop block the loop interrupt biock the BumperBot3 program... further exploration, conclusion 7 the WallFollower program: navigating a maze .. pseudocode solving & Mazes program requirements ‘assumptions, initial design following a straight wall. writing the code using the education edition testing turning a corner. writing the code testing going through an opening n. ‘writing the code testing - using sound blocks for debugging... final test farther exploration, conclusion. 8 data wire: what isa data wire? the GentleSiop program. writing the program tins for using data wires. challenge 8-1 the SoundMachine program. controling the volume using the math block adding tone control understanding data types. displaying the frequency and valume values using the text block adding labels to the displayed values. displaying the volume, challenge 8-2. challenge 8-3... challenge 8-4... further exploration conclusion 9 data wires and the switch block.. the switch block's value modes, revmiting the GentleStop program passing data inta a switch block advantages of using a sensor block passing data out of a switch block... simplifying the LineFollower program challenge 9-1 challenge 9-2. further exploration conclusion 10 data wires and the loop black .........0 legie mode challenge 20-1. the loop index. the LoopindexTest aragram restarting a (oop. the final loop index value, the SpiralLineFinder program. fallowing a spiral, detecting a line while moving in a spiral challenge 10-2... Using the gyro sensor for hetter turns. 15 116 116 116 116 v7 118 18 19 120 120 further exploration conclusion aa variables. the variate block the RedOrBlueCount program creating and initializing the variables choosing variable names. displaying the intial values. ‘counting the red objects. ‘counting the blue objects. managing variables using the project properties page. the compare block. the LightPointer program defining the variables finding the light source Intualizing the variables, ‘creating the LightPointer program, the constant block further exploration, challenge 11-1... challenge 11-2. conclusion 12 ‘Hy DAOCKE sn ssserssesetesnesnesnonsinintes creating 3 my block the my blacks palette editing a my block challenge 12-1 the LogicToText my block ‘adding, removing, and moving parameters the parameter setup tab the DisplayNumber my block ry Blocks and debugging... changing my block parameters. variables and my blocks, challenge 12-2. further exploration... contlusion 43 math and logic... the math block's advanced made supported operators and functions. the modulo operator. math black errors 4 proportional LineFollower EV3 timers. challenge 13-4. the DisplayTimer program splitting the timer reading into minutes and seconds. building the text ta dsplay. the round biock the random block ‘adding a random turn to BumperBot. chollenge 13-2. challenge 13-3. the logic block : adding some logic ta BumperBiot the range block the Tagélong program the GyraPointer program... chollenge 13-4. further exploration, conelusion 14 the EV3 lights, buttons, and Aisplay. the EV3 buttons, the PowerSetting program. the initial value and the loop... displaying the current value adjusting the power value challenge 14-1, testing the program.inun changing the value faster the brick status light. the ColorCopy program, challenge 14-2. the display block. Aisplaying an image... the Eyes program : drawing on the EV3 screen, the EV3Sketch program further exploration, conclusio a5 arrays overview and terminalogy. creating an array, the array operations black. length mode. read at index mode write at index mode. append mode the ArrayTest pragram. the ButtonCemmand program ‘creating the array of commands. challenge 15-1 displaying the commands. ‘executing the commands. challenge 15-2. the CalorCount program the ColorToText my block. the AddColorCount my block, using a data wire to select a sound file initialization counting colors, ‘the MemoryGame program the start of the loop creating the sequence of lights. the WaitForButtons my block. checking the user’s response further exploration, conclusion. 16 files. the file access block setting the filename. writing toa file avoiding filename bugs. reading from a file saving the MemoryGame high score the FileReader program, chatlenge 16-1 ener finding the end of a file. ‘aiiding @ menu to the ColorCount pragram the CreateMenu_CC my block., the SelectOption my block the new ColerCaunt program structure ‘counting objects, saving and loading the count data. testing managing memory challenge 16-2... further exploration EV3 toxt files end windows conclusion. 7 data logging.. data collection and the EV3. investigating the current power reading the CurrentPowerTest program the LogData my black the CurrentPowerTest2 program. challenge 37-1 testing the current power with the move steering block the SteeringTest program the VerifyLightPointer program controlling the amount of data challenge 27-2. further exploration conclusion. 18 multitasking.. ‘more than one start block.. the stop program block avoiding a busy loop ‘adding lights to the DoorChime program challenge 18-1 Understanding program flow rules starting blocks and data wires. Using values from a loop or switch block using my blocks. synchronizing two sequences keeping out of trouble.. further exploration conclusion 19 a PID-controlted LineFollower program the PID controller proportional control. the raw data. the good ane bad zones, selecting the targe vo collecting the min and max sensor readings rrormalizing the sensor reading and target values. enhancing the proportional control LineFllower color sensor calibrate modes.. implementing PID contral adding the derivative term. adding the integral term, tuning the controler. further exploration conclusion A NXT and EV3 compatibility.. motors sensors. software This book is about learning how to write programs for LEGO, MINDSTORMS EV3 robots. The EV3 software is ¢ powerful tool, and this book will teach you how to get the mast out of it as you acquire the programming skills necessary to create your own programs who this book is for ‘This book is for anyane wha wants te learn how to create programs to cantral their EV3 robot, whether youre a young robotics enthusiast; an adult teaching children about robotics: a parent: a FIRST LEGO League coach: or @ teacher using the EV3 in a classroom, One af my goals in writing this book was to rmake the material accessible to young learners while going into enough depth to help students and teachers understand the hows and whys of EV3 programming, prerequisites This book can be used with either the Home or Education Edition, and you'll use a single general purpose robot for test ing your pragrams. Thare are anly a few relevant differences between the programs for each edition, and | point them out as appropriate. Almost all the material presented here applies to either edition No previaus programming experience is required. The EV software is powerful but easy to use, and makes a great, introductory too! for first-time programmers introduction what to expect from this book This book focuses on pragramming EV3 rabots rather than on the mechanical aspects of building ther. All of the programs in ‘this book are designed to work with one general-purpose robot or with just the EV3 Intelligent Brick, You'll learn how to work with the core parts of the EV3 software, such as blocks. data wires, files, and variables, and how these pieces work together, You'll also learn some good programming practices, bad habits to avoid, and debugging strategies that will help you have fun while programming and keep your frustration level low In this book you'll find step-by-step instructions and ‘explanations for many EV3 programs, including small examples designed to help you understand exactly how EV3 programs work, as well as complete, sophisticated srograms designed, to perform complex behavior. Along the way, you'll also see programming challenges, which will prompt you to explore EV3 programming on your own to practice the concepts you've learned, ‘The book begins with an introduction to the EV3 set and the: software you'll be using ta create your programs, This is followed by the building instructions for the test robot. The next few chapters cover the basics of the EV3 software, culminating in a maze-solving program in Chapter 7. Thats followed by Proce Sounds = ————— ©: x icon Click the LEGO Sound Files folder to open it and then select the Communications folder. Scroll dawn and select the Hello file. The white box at the top right of the Sound block should now contain the word Helle, saving your work Before continuing, name your program and save the project. In this book, | generally use ane projact for each chapter, na Chapter2. Chapter3. and so on, Each project contains all the ams developed in that chapter. Let's change the name of this program to Hello and save the project as Chapter? a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book CHALLENGE 13. ‘The GyroTurn program introduced in Chapter 5 has the ‘TriBot make a quarter-turn by moving until the Gyro Sensor reads 90. This works well if the robot moves slowly and less well when it moves quickly. Imprave fon this by using the Gyro Sensor reading to control the speed, making the robot move more quickly when {far from the end of the turn and slowing it down as it gets close to the target. This lets the TriBot make an accurate quarter-turn quickly, completing it cond or two rather than the several seconds it takes at a constant, slow speed. Keep the TriGot from spinning too slowly by making sure that the Math block always gives some minimum value 10 + (Target - Sensor reading) « Gain will always be at least 10 if the target is greater than or equal to the reading (and the gain is positive). imers can be selected from the S ait. Switch, and Loop blacks. You can also cantrol a tim Timer block, which appears on the Sensor palette. The Timer block (Figure 13-14) has three modes: Measure mode reads the using the current value of pares the valu and Reset mode resets The block also has a parameter to select which of the eight timers to use. nds). Compare mode com- with a threshold and gives @ Logic value result, Figure 45-18, Te Timer tock the DisplayTimer program The DisplayTimer program cam Timer blacks to show a running timer 1s the pawer of the Math and nthe EV3 screen, The gram takes a reading the Timer black and displays it in for example, 0:03 fer thre ands. The ypical minutes:seconds format fonds, and 2:15 for two minutes and fiftean s rogram uses Loop black to keep running until you stop it, and each time through the loop, a Timer black reads the timer and the new value is displayed Note that the Timer blo a8 7.46 or 11.038, and the pr woll (215947). But t simple, lluse whale numbers in any examples, eads out alues, such gram displays keep the discussion of this program splitting the timer reading into minutes and seconds inthis program, we take the reading from the Timer block and divide it into minutes and seconds. For example, if the Timer block reading is 127 seconds, we want to display this as 2 min utes and 7 seconds. We can do this with two simple formulas Seconds = Timer read) Minut 260 seconds] / 60 To calculate how mary seconds te display (between 0 and 59), we take the remainder of the Timer block reading divided Wy 60. So when the Timer block reading is 127, we get 7 because 127 % 60 is 7 Subtracting the number of seconds from the Timer block reading gives us a value that is a multiple of 60, 4127-7 is 120. Dividing this value by 60 gives umber of minutes, because there are 6( ame examp| us the seconds in @ minute, Figure 13-12 shows the first part of the program. The Timer black reads the elapsed time and passes the value to the two Math blocks, The Math blocks each use Advanced mode t calculate the number of seconds and minutes to display. The lock to calculate the numb first because its result is used by the other Math block building the text to display ‘of seconds needs to The next step isto take the minutes and seconds values. whic are numbers, and combine them with a colon (3) to create a Text value in the form minutes:seconds, We could send the minutes and seconds values straight to a Text block to combine them, but there is one problem: If the number of seconds is less than 10, then the result wor't be correct. For example, if the wumbe' is 7. then block would generate 2:7 instead of 2:07. Before we send values to the Text block, we have to add a leading 0 to the Seconds value when i's less than 10. Figure 13-13 shows the code that constructs the correctly formatted Text value when the number of seconds is less than 20, along with the two Math blacks that supply the number of minutes and s ‘ompare blo check ifthe seconds value is less than 10, The Switch block's number of minutes is 2 and the of secon the Text ra aa] Fahl el cd bes sd pad igure 13-23. Aainge 0 the numberof secanas i ess than 30 true case uses a Text block to add a 0 to the number of sec- nds and passes the result aut of the Switch black to another Text block, which combines it with the number of minutes and a colon to generate the formatted value. The final Text block now gives the correct value, 2:07, when the number of minutes is 2 and the number of seconds is 7 What happens when the number of seconds isr't less than 10? The false case of the Switch block is used, and we want to pass the value to the final Text black unchanged. so we can just ‘av a data wire from the block input plug directly to the block output plug, as shown in Figure 13-14, Figure 13-16: Possng the vlue out of he Switch block sa Test volve aaa ade Fire 23-42. Turning the elepsed tine i [oC % pel Soo Goat Soe = (atellna| niall | |] leila tl tg Notice that the data wire going into the Switch block contains a Numeric value from the Math block, which explains wiy its yellow. The data wire coming out holds a Text value (this plug wes created by the Text block inside the true case), which explains why it's orange. When you build this program, be sure to fill in the true case first so that you get an orange Text output plug going out of the Switch block. Ifyou fll n the false case first (passing the Numeric data wire through to the this would create a yellow Numeric output plug. and then you wouldr't be able to connect the data wire from the Text block in the true case to that plug, Remember, you can connect Numeric values to a Text plug (and the data type will convert automatically). but you can't connect a Text value to a Numeric plus ‘The final part of the program is a Display block to show the result af the final Text black on the EV3 screen (Figure 13-15). Ive set the Row parameter to 4 because the text is alittle easier to see when it's closer to the middle of the screen Run the program and you should see the Time value counting up. Let the timer run for at least @ minute sa_you can make sure it does the right thing when the number of seconds is less than 10, when the number af seconds is greater than 10, and when the minute should change from 0 ta 4. The display will include three digits after the decimal point, and these dig- its vill change very quickly. You'll see how to hide or truncate thase fractional values in the next section using the Round block. a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book as) Out of range, Sn for nat Ts the Tndot atthe desved nda? eee % JT (al) A Thrange, Stop We motors Figure 13-29: The GyroPointer program + The parameters of the Range block check that the sensor reading is between -10 and 10. The reading at the start of the program will be O, $0 this range keeps the robot pointing clase to where it starts The Math block’s result is connected to the Move Steering block’s Steering parameter rather than the Power parameter, To make the TriGot spin, we want this setting to be either ~100 or 100: otherwise. the robot will move forward as well as turn, The Math block multiplies the sensor reading by ~10. If the sensor reading is lass than -10, the result will be more than 100, and the Move Steering block will treat the value as 100 IF the sensor reading is more than 10, the result wil be less than ~100, and the Move Steering black will treat the value as -100, ‘When you run this program, the TriBot sits motionless. until you move the turntable at least 10 degrees (in either Girection). Then the Tribot spins to keep facing the direction in which it started. Move the turntable in one direction and then the other, and the TriBiot should adjust to keep pointing in the same general direction CHALLENGE 13-4 I you have the EV3 Home Edition, use the Infrared ‘Sensor and Infrared Remote to create a RemotePointer program that works the same way as the GyroPointer program. The pragram should spin the TriBat co that it keeps pointing in the general direction of the Infrared Remote by using the beacon heading to control the Move Steering block. further exploration Try these activities for more practice with math-related blocks: 4. Write a CountDown program that shows the time counting down from two minutes an the EV3 screen. The Loop block should exit when the time gets to zera, Combine the TagAlong and RemotePointer programs to ‘make a program that will maintain bath its distance and direction. The resulting program should make the robot follow the Infrared Remate around a room, Use the Raniiom block to ereate a MagickightBall program. When you ask a question and trigger the rabot [such as by pressing the Touch Sensor), the robot should select an answer randamly fram several possible answers. Use the Display block to show the answer an the screen. You can also use the Sound Editor tol to record your own answers and the Sound block to play them, This activity uses some knowledge of trigonometry: feel free to skip over this one if you haver't reached that level of mathematics yet, The sine function starts at O and oscilates between 1 and -1. so if you graph this function. it creates a srake-like curve, You can make the robot fol- low a winding, serpentine path by using the sine function to control how the robot steers. Create a Siither programm Using a timer and a Math block that uses the sine function to control the Steering parameter on @ Move Steering block: Hint: Using sin{Elapsed time] directly wor't be very inter~ esting because it takes 6 minutes (360 seconds) for the value to go from one extreme to the other, and the value only goes between -1 and 1. However, you multiply the lapsed time by 10, the entire range will be covered in only 36 seconds, and if you multiply the result by 50. the Steering value will oscillate between 50 and -50. conclusion In this chapter, you learned how use blocks that work with numbers and lagic. The Math block's Advanced made gives you all the power you need to calculate complex equations, which allowed you to improve the LineFollower program by using € proportional contraller. You also learned about the modulo, ‘operator and saw it in action in the DisplayNumberNextLine My Block and the DisplayTimer program. ‘The Logic block lets you write programs that make com- plex decisions, such as combining the input fram multiple sen sors. The Range block gives you a convenient way to perform ‘the common operation of testing a value to see whether it's in a certain range. The ather black intraduced in this chapter was the Random block, which you can use to add a litle unpredict= ability to your programs and personality to your robots. a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book a You have elther reached a page that is unavailable for viewing or reached your viewing lil far this book index Page numbers shown in italic indicate figures A absolute value, 97 /AddColorCount My Block, 184-285 acting labels toa displayed value, 161-162 algorithm, 82 control algeithm, 152 ambient ight, measuring, 55 ‘nd logic operation, 159 Around TheBlock program. 44-46 ertey, 175-177 creating, 175-178 elements in, 275 empty. 175 indices and, 175 length of 175, ‘Array Operations block, 176-177, 178 Length mode, 176 Read At Index mode, 176-177 Write At Index mode, 177 Append mode, 177 ArrayTest program, 177-479 prt. and engineering. 3 ‘assumptions, program design 4 AutorD, 42, 52 utomatiereuting of date wires, 96, 8 Back button, 265 back-up copies of programs, 3 beeping, while backing up, 66-87 binning. 111-112 blocks. See oso specifi block names fonmecting with data wires, 95 copying. 194 modes, 2 running. 2 selecting. 197, 138 BlockStartTest program. 226-225 Bluetooth $ Brick, See £V3 iteligent Bric Brick Buttan block, 165-166, 167 Erick buttons, 164, 165-165, 179, 188, 198-200 Brick Status Light, 168-169, 190, 226 Brick Status Light block, 168-169 bugs. n programs, 10, Bumped (Touch Sensor state, 52 BumperBot program. 52-54, 62, 159-160 BumperBot2 program, 65-87 BumperBot3 program, 78-80 busy loop, 220 Button 0, 265 ButtonCommand pragram, 179-181 button, Soe Brick buttons c (Chime My Block, 137-239 clesting the screen, 10, 172 CoastTest program. 48-69 Color Sencor 54-56 In Ambient Light Intensity mode, $5 in Color made. 54 col values, 54, 183 connecting to the TriBet, 24-25, 30 in Reflected Light Intensity mode, 55 Color Sensor block. 112 Calibrate modes, 236 olor Copy prosram, 169, (ColorCount program. 182-187 adding 2 menu to. 198-205 ColorToText My Block, 183-284 comma-separated values (C5¥), 209, 210 comments, 11-42 adding, 12-12 comment too, 11 find the Switeh block, 74 tips for working with. 12 Compare block, 128, 990, Constant block, 134 Content Editor. 6 Context Help, 12,98 control algorithm, 252 control variable, 152 controller, three-state, 152 Count_C€ My Block 202-203, 204 CreateMeru_CC My Black, 198-199 CSV (comma-separated values), 209, 210 CurrentPowerFest program, 208-222 CurrentPowerTest2 program, 212-216 Do {ata collection. See datalogging fata logging, 209-248, 230

Das könnte Ihnen auch gefallen