David Luders on Flickr

Learn some nifty coding tricks while creating a Formula EV3 Race Car.

#### Bringing Legos to Life with Code

Have you ever wished that you could bring your favorite Lego creations to life? Maybe not to life in the way that a plant or animal is alive, but at least to see them move across the room on their own, react to their environment, or even just to make a little noise? The Lego EV3 set allows us to do just that.

#### What Distinguishes EV3 from other Lego Sets?

For starters, the home EV3 set contains over 600 pieces, mostly similar to those in the Lego Technic line. However, it also includes a set of sensors (infrared sensor, color sensor, and touch sensor) and a “brain”. The brain is actually a small computer that connects to a laptop or PC with a USB cord and allows programs written on your computer to be downloaded to any EV3 project!

#### How Does it Work?

Through code, we can program motors to rotate at varying speeds, obstacles can be detected, and beginners as well as experienced coders can transfer their programs from a computer to a Lego robot. Although Lego created its own drag and drop language for the EV3 set, it shares many of the features of a standard, text-based, language. As such, when we program an EV3 robot we develop algorithms that include looping, conditional logic, variables, and math, and we can even write our own functions.

We use a loop in our code when we want to repeat a process over and over.

Conditional logic is used to specify a condition and have the robot react one way if the condition has been met, but another way if not.

We use variables to store numbers, words (AKA “strings”), and arrays.

We can add, subtract, multiply, and divide the numbers stored in variables or read in through sensors

#### A Program With It All

In the past two issues of Beanz we explained a bit of the basics of the EV3 programming language. This month we decided to make use of a few more of the tools available to us. To do so, we built a race car robot that incorporates two sensors and three motors. All of the pieces for the body of the car come in the EV3 set. The infrared sensor sits on top which allows us to control the car with the infrared remote. The color sensor in the back gives the car the ability to detect and react to colors behind it. But first it must all be programmed!

Just about anytime steering is involved in a project, calibration is required. If we want our race car to travel straight, we must first program it to recognize the position of the wheels when the car travels forward. Similarly, we need to define “left” and “right” in order to turn the car. Each of these three directions requires a small program of its own, and they will all be used later to program the remote.

Putting these three programs together as is, in a loop that allows the remote to communicate with the infrared sensor, would make our program messy and challenging to read. Fortunately we can “cover up” the details of much of this code by building our own blocks and storing it inside. This is similar to writing a function in text-based languages such as Python or Java. If we name our blocks wisely, a person reading our program will look at the name of the block and have a sense of what it does without seeing the details of the code inside of it.

Below is the final program to calibrate the wheels and enable the infrared remote. It might look too easy to be true, however there is quite a bit of code stored in the calibrate block as well as in the remote block. In fact we created these blocks on our own so that our final program is clean and easy to read.

For those who are curious about how the wheels are calibrated and the remote is programmed, take a look at the code below:

Calibrate – The Reset, Left, Right, and Center blocks are our own creations as well. Each one of these has even more code hidden within it:

#### Code Inside the Remote Block:

And with that, our race car is ready to roll!

#### The Lego Mindstorms Discovery Book

https://www.amazon.com/LEGO-MINDSTORMS-EV3-Discovery-Book/dp/1593275323/ref=sr_1_2?ie=UTF8&qid=1506329047&sr=8-2&keywords=ev3+programming+books

#### The Art of Lego Mindstorms EV3 Programming

https://www.amazon.com/LEGO-MINDSTORMS-Programming-Full-Color/dp/1593275684/ref=sr_1_1?ie=UTF8&qid=1506329047&sr=8-1&keywords=ev3+programming+books

#### The Lego Mindstorms EV3 Idea Book

https://www.amazon.com/s/ref=nb_sb_ss_i_1_11?url=search-alias%3Daps&field-keywords=ev3+programming+books&sprefix=ev3+program%2Caps%2C194&crid=25KLEYR0YKV17

#### Collection of online video tutorials

http://www.drgraeme.org/EV3/EV3.html

### Also In The December 2017 Issue

Learn some nifty coding tricks while creating a Formula EV3 Race Car.

With binary search, you don’t have to be a mind reader to beat this simple coding activity.

Controllers have come a long way in 50 years. Let’s appreciate the bulky, awkward ancestors that led us here!

Take your Scratch games to the next level by adding multiple modes with multiple sprites.

Let the CSS Zen Gardens wow you with variety and elegance. Making a pretty website isn’t so complicated after all!

An introduction to a hip new alternative to Java. It’s all the rage in android development!

Create illuminating art with micro:bit

Ducks, dolls, and other stuff make great programmers. Here's how it works.

Resources to transform you into a web virtuoso, from messaging apps to testers to code converters.

New treasures and new enemies await in two unique, dangerous realms.

Learn how Twine can be used to make interactive games and stories.

A simple zigzag pattern can scramble your messages beyond recognition.

Create your own 3D digital museum and learn how you can help preserve cultural artifacts.

Interesting stories about computer science, software programming, and technology for December 2017.

Add data types to your new programming language in this third installment of the series.

Links from the bottom of all the December 2017 articles, collected in one place for you to print, share, or bookmark.