Book Image

Smart Robotics with LEGO MINDSTORMS Robot Inventor

By : Aaron Maurer
Book Image

Smart Robotics with LEGO MINDSTORMS Robot Inventor

By: Aaron Maurer

Overview of this book

LEGO MINDSTORMS Robot Inventor is the latest addition to the LEGO MINDSTORMS theme. It features unique designs that you can use to build robots, and also enable you to perform activities using the robot inventor application. You'll begin by exploring the history of LEGO MINDSTORMS, and then delve into various elements of the Inventor kit. Moving on, you'll start working on different projects which will prepare you to build a variety of smart robots. The first robotic project involves designing a claw to grab objects, and helps you to explore how a smart robot is used in everyday life and in industry. The second project revolves around building a working guitar that can be played and modified to meet the needs of the user. As you advance, you'll explore the concept of biomimicry as you discover how to build a scorpion robot. In addition to this, you'll also work on a classic robotic challenge by building a sumobot. Throughout the book, you'll come across a variety of projects that will provide you with hands-on experience in building creative robots, such as building a Dragster, Egg Decorator, and Plankton from Spongebob Squarepants. By the end of this LEGO book, you'll have got to grips with the concepts behind building a robot, and also found creative ways to integrate them using the application based on your creative insights and ideas.
Table of Contents (12 chapters)

Coding a rock, paper, scissors wrist game

You are now going to code this wrist game to be able to randomly choose rock, paper, or scissors. You can play it against yourself or use it against another human. If you have a friend that has a kit, then you can both build one and compete this way:

  1. Using the default yellow when program starts block, add a purple Light block named write Hello. You will need to change the word from Hello to Left:
    Figure 2.37 – "Left" being programmed to screen

    Figure 2.37 – "Left" being programmed to screen

  2. Add two yellow Events blocks named when Left Button pressed. Make one that activates when the setting is set for the left button and the second block set for the right button.
  3. Go to the pink My Block sections of the coding blocks and make two new blocks. Name one start game and the other playagain. Add these new blocks to the yellow blocks. Add the start game block for the left button and playagain for the right button:
    Figure 2.38 – Decision making with MyBlocks

    Figure 2.38 – Decision making with MyBlocks

  4. In the previous step when you created these blocks, you should have seen two new pink blocks show up in your coding canvas named define playagain and define Start Game. Locate define playagain:
    Figure 2.39 – playagain My Block

    Figure 2.39 – playagain My Block

  5. Under the define playagain block, add a sound effect by adding a purple play sound block named play sound Cat Meow until done. Change the audio file to one of your choice. Under this block, add a Light block named turn on Smiley Face for 2 seconds. Click the face and change the graphic to your choice. In this example, an X was made. Add another light block underneath this one, turning off all the lights. Finally, add a final purple Light block named write Hello and change Hello to Left.
  6. You will follow a similar process for the other define Start Game block. Locate this pink block. Add a purple play sound block named play sound Cat Meow until done. Change the audio file to your choice. Add a purple Light block named turn on Smiley Face for 2 seconds. Change this block to the number 3 in the graphical interface and change it from 2 seconds to 1 second. Right-click the purple play sound block you just added and choose duplicate. Duplicate these two blocks two times so you don't have to keep dragging blocks. Change the 3 to a 2 and the other to a 1 to create a countdown timer. Last, create another pink My Block and name it Rps. Add this block to the end of this code:
Figure 2.40 – start game My Block

Figure 2.40 – start game My Block

The final step is to program the Rps block:

  1. Add a purple Light block named turn on and change the smiley face to the four squares in the corners.
  2. Add an orange Control block named wait until. Add a blue Sensor block of Hub is shaken to the diamond space.
  3. Go to the orange Variable block and make a variable named Rps_Random. Add the set Rps_Random block to the code. Use a green Operator block named Pick Random and select numbers 1-3.
  4. Add an orange Control block named If. Drag in a green Operator block that compares with the = sign. On one side of the equals sign, add your Rps_Random block, and on the other side, insert the number 1.
  5. Within that If block, add a purple Light block named turn on and turn on all the lights to symbolize paper.
  6. Add one more purple Light block named set Center Button light to and choose a color.
  7. Right-click this If block you just created and duplicate it two more times. For these two copies, change the numbers to 2 and 1. Change the design from paper (all lights) to a smaller square for rock for one, and for the other, design a pair of scissors.
  8. Add an orange Control block named Wait and choose 5 seconds so that you have time to see your game choice.
  9. Add a purple Light block named write Hello and change it to Right:
Figure 2.41 – Rps My Block

Figure 2.41 – Rps My Block

In the end, your code should look like this:

Figure 2.42 – Complete view of the code

Figure 2.42 – Complete view of the code

How it works is the game will tell you Left on the screen to indicate to press the left button. This will trigger the start game My Block, which will do a countdown timer to show your choice. Once it counts down, it will then move to Rps, where the code will wait for you to shake the Intelligent Hub before displaying a choice of rock, paper, or scissors. After it displays the choice for 5 seconds, it will then tell you Right, so when you press the right button, it will reset the screen and wait for you to press left again to play the game.