Parrot Chasing Ball: Learn How to Code with Blocks

In the last decade or so, programming has become a vital skill. Whatever the industry, gaining a working knowledge of programming makes you a more attractive employee, especially when you consider that only around 30% of developers and programmers work for software or internet companies. It’s clearly a skill set that has become important for many companies and businesses around the world.

According to Stack Overflow survey the term ‘Software Developer’ has already become the most common job title in several US states. What’s even more interesting however is that 70% of them are self-taught. For many of us who want to learn more about programming, the big question is, how do I get started?

VIA Pixetto is the perfect place to start. As well as being a powerful vision sensor suited for a variety of robotic and AI projects, it also supports Scratch, an easy, block-based visual programming language that makes programming more accessible and the ideal place to start your programming journey.

Parrot Chasing Ball Step-by-Step Guide

In this step-by-step guide, I am going to introduce you to the logic of block coding. Block coding is a way of learning programming that involves dragging visual ‘blocks’ of instructions, with an easier vocabulary than traditional, text based programming.

This guide allows the user to program graphical parrot to chase a colored ball. The ball being chased is determined by color recognition. If VIA Pixetto detects the color red, the parrot chases the red ball. If the color yellow is detected, the parrot will chase the yellow ball. The idea is to introduce color recognition applications in a fun and graphical way.

Step 1

Connect your VIA Pixetto to your computer using a Micro USB cable.

Step 2

Open the VIA Pixetto Utility application and train the camera to recognize the colors red and yellow. This can be done by using the configuration panel on the right side of the utility. Simply select ‘Color Detection‘ as the function, and then select the relevant two colors.

Step 3

Go to https://mls.pixetto.ai/scratch/

Then, link your VIA Pixetto with the Scratch application.
To do this click on the ‘Not Connected’ button at the top of the page.

When the following pop-up window appears, click ‘CONNECT‘.

Your VIA Pixetto is now configured to recognize red and yellow (Step 2), and connected to the Scratch application (Step 3).

We are now ready to start coding with blocks!

Step 4

First we need to set up the sprites and the backdrops (optional).
For this ‘recipe’ we need a parrot and two balls (one yellow, one red).

Click on the Cat shaped icon at the bottom right of the page to choose the parrot and two balls. The following sprite selection panel should appear.

As you can see above, we can modify the size of the sprites and their names. I chose to change the Parrot size (from 100 to 50). I also changed the ball size (from 100 to 70) and modified their names to Yellow ball and Red ball.

You can customize any sprite using the Costumes Tab on the left corner.

Step 5

We want the two balls to continually move in random movements on the display panel. For that we will need three blocks.

The first is an Event Hat block. It tells the sprite to start running the blocks when the green flag is clicked.

The second is the Control C block ‘Forever’. It tells the blocks inside to run ‘forever’, i.e. constantly.

The last one is the Motion block ‘Glide.. secs to’. It tells the ball to glide on the screen. You can modify the glide speed by editing the seconds. We set this block to ‘random position‘ so that the ball glides around randomly.

Having setup the blocks for the Yellow ball, we can repeat the same code for the Red ball.

Step 6

We now need to instruct the parrot to follow the ball according to the object color that the VIA Pixetto detects.

Optional: We can fix the parrot’s starting position on the display panel.
To do this, we use the following three blocks:

We use a Control Hat block to start. This Motion block moves any sprite to any position on the display panel. The X and Y coordinates can be edited according to where you want your the parrot’s starting point to be on the screen.

This Motion block controls the angle where the sprite should point. You can also edit the angle by changing the degree number.

When the green flag is clicked the parrot comes back to its starting position.

Step 7

Now we are going to set up the blocks allow us to add the VIA Pixetto’s color recognition. The color detected by the VIA Pixetto will determine which colored ball is chased by the parrot.

As you can see in the previous picture we are missing the VIA Pixetto block category.  If this happens, click the ‘Add Category‘ button (bottom left corner). Then, click on VIA AI Learning Kit.

To instruct the parrot to chase the Red ball when a red object is detected by the VIA Pixetto, we will need the following seven blocks.

Step 8

Now we are going to set up the blocks that instruct the parrot to chase the ball as determined by the color of the object detected by the VIA Pixetto vision sensor.

Repeat the same blocks as the Red color detection. Don’t forget to change to yellow on the scrolling menu of the Pixetto block ‘Color‘, and to Yellow ball on the scrolling menu of Motion blocks ‘Glide‘ and ‘Point Towards‘.

Enjoy! Don’t forget to share your own Blocks creations with us on our social media channels!

Share this blog post!

Share on linkedin
Share on twitter
Share on facebook

Leave a Reply