Intro to Block Programming

Check out a very good resource on Block programming by Bruce Shaffer


What is code?

Have you ever wondered how humans tell machines what to do? Machines might seem to work via magic, but that “magic” is actually thousands of lines of computer code.

Computer Code is how humans talk to computing machines, which nowadays include desktop computers, laptop, phones, tablets, video game systems, robots, satellites… and the list goes on!

At the heart of all these machines are microprocessors, which is the “brain” of a machine. Unlike our brains which evolved over thousands of years, machine brains were created by human scientists over about a century.

Microprocessors control the flow of electricity in a computing machine, and by “talking” to them we can tell them what to do. Microprocessors might tell a light to turn on, a phone to vibrate, the color of pixels, or a robot to start moving.

Computer programming languages like JAVA, Python, and C++ allow humans to write machine code in a language they can understand.

Enter blocks

Teachers of Computer Programming noticed that it was difficult for students to “climb the first hill” of learning a programming language because it was too much new information at once, so they thought of a new way to teach the concepts of computer programming: programming blocks.

The future of blocks

While blocks were developed initially as a way for students to have a comfortable introduction to writing programming languages, they have become a method of programming that is independent from coursework. Many Integrated Development Environments such as Unity (the IDE we used to build the Virtual Robot Simulator) and the Unreal Engine (the IDE used to build popular games like Fortnight) have visual programming languages that are similar to blocks programming. These languages tend to use a “node-based” language instead of a “blocks-based” language, but the idea is the same.

Whether you are hoping to learn a programming language, learn programming concepts, or build your skills at visual programming, block coding with the Virtual Robot Simulator is a great place to start!

Do I need ALL these blocks?

You might notice that there are quite a few blocks available to use. Do you really need all of them?

Actuators, Logic, Loops, Math, Text, Lists… there are quite a few different block categories, and within those, there are lots of blocks. Where do you even start?

The key to learning most things, including blocks programming, is to start with simple tasks and then figure out how to use the tools provided to accomplish those tasks. If you start with learning every tool, you’ll have forgotten them by the time you need to use them, so instead focus on a few at a time.

Our first task will be to move the robot forward and backwards.

Writing your first blocks program

Let’s take a look at the default code template we see when we load up the program for the first time and select “Autonomous Mode”

Blocks programming | Getting Started with Blocks, Download Virtual Robot Simulator Lessons with video links

Setting motor power

The following code blocks drive the robot forward for two seconds at a power of 0.2. After that we want it to strafe to the right. If we set the frontLeft power to 0.2, what would we set the other values to? Try this out in the app and then come back here to see the answer.

Aha! We hope you see that exploring the blocks in your toolbox often reveals a better way to accomplish a task.

What power should I use?

Often in programming you need to set a value. Sometimes you have the opportunity to define what that value means before setting it, but often you have to work within the constraints of the existing software. In this case, the “power” value we “set” for each wheel is determined by the VRS App. The only way you can figure out what the power should be is to experiment. Remember: in most cases you don’t have to use whole numbers only; numbers can be negative, decimals, or both!

What now?

Ok so we’ve successfully set the power of motors, now what? Well, if we simply wanted a robot that rams things at high speed, we are done. Most of the time, though, we want our robots to behave a bit more deliberately, so we need to introduce some more commands so our robot will move forward AND backward AND forward again (as many times for as far a distance as we want).

Learn how to do that in our motor power guide!

Leave a Reply

Your email address will not be published. Required fields are marked *