Programming the Tinker Pi Robot for Arduino Using App Inventor Part 1 #tt27

Tinkerer: Lory Livezey

We've now gone through several videos in this series to lay the ground work for our project. In this tutorial, we're going to start to put it all together and you're going to learn how to control this Arduino robot with an Android Phone.


You will learn:

  • How to apply what you learned in the previous App Inventor tutorials and create a remote control for the Tinker Pi Robot
  • How the formula that determines the speed of the wheels works
  • How to set the accelerometer values on the screen labels

Previous Step

We learned how to get started with the Arduino in #19. Then we learned how to connect the HC-06 Bluetooth Module in #20. In #21, we learned how to create the simplest of App Inventor Android Apps. In #22 and #23, we connected that app to our bluetooth module. #24 was for those of you who are using a Kindle, we had a minor hurdle to get over there. Then in #25, we added the L298 Motor driver to the mix, and learned to turn it using our app. Finally, in #26 we learned how put together our Tinker Pi robot. Now keep in mind, these tutorials will apply to any Arduino robot you may have.

  Assembling the Tinker Pi Robot for Arduino #tt26

Starter Breakpoint

The Android Project (continuing from #tt25):

The App Inventor Project (continuing from #tt25):

What you will need

Like all new projects we get here at the Tinker Pi Factory, we're going to start with a list of requirements. This is what we want our robot to do:

  1. It should be modular, meaning, we want to be able to add more components to it, like sensors
  2. It should be easily controllable
  3. It should be fairly inexpensive
  4. It should be easy as possible to program and modify

So based on those criteria, I decided to use the Arduino UNO, because though it's not the smallest, it's usually the easiest board in the Arduino family. Because I wanted it easy to control, I chose the Android App developed using App Inventor because tilting the phone is more natural than pushing buttons, and App Inventor is super simple. All of these parts are fairly inexpensive, and the rechargeable batteries will save money in the long run.

Getting Started and Breaking the Project Down

Let's open up App Inventor to where we left off in #25. We're going to remove the test buttons, we don't need those any more.

Delete Test Buttons

We're going to break this project down into smaller tasks, which is what is called "Decomposition" in computational thinking. If you'd like to learn more about that, click right up here.

We're going to focus on one task at a time, and test as we go:

  • Forward (Left and Right)
  • Reverse (Left and Right)
  • Stop
  • Pivot Left
  • Pivot Right

By breaking things down, we make the project less daunting. Let's get started.

Understanding the Formula

As we tilt the Android forward, the Y axis increases and X remains 0.

Y Increases the Speed

So Y represents the speed of the robot, and X represents how far left or right we want it go.

Y Increases the Speed

The formula we're going to use to calculate the speed is this:

speed = 255 * (y / 255)

Where 255 is the max speed of the motors.

This means that if we tilt the android forward all the way, our speed will be 255 * (255/255), which is 255, so that's full speed.

Full Speed Formula

Our accelerometer will go higher than 255, but we are going to cap it off.

We're going to start by focusing just on the Forward direction. X is negative in the upper left quandrant. Do you remember that Cartesian Plane? I know, it sounds like an airplane going to a really warm place, but it's this..

Cartesian Plane

The more negative X is, the tighter the turn. So if you picture what the wheels are doing in this quadrant, the right wheel is going to the speed of Y, and the left wheel is going...slower.

Wheel Speed

How much slower is what we'll determine using this formula.

differential = speed - (speed * (x / 255))

I call it differential, because we want to figure out the difference in speed between the wheels. The bigger the difference, the tighter the turn.

Looking at an Extreme Example

Let's look at that extreme example, where X is 0 because we want to go straight ahead. We determined that the speed is 255, so the differential would be:

255 - (255 * (1 / 255))

We have to make all 0's 1 because we can't divide by 0. So we just make 0's 1's, this comes out to essentially 255. In other words, both motors full speed ahead.

Extreme Example Full Speed Ahead

Example in the Left Quadrant

Let's try this again somewhere in this left quadrant. When Y = 150 and X = -100, then the speed is:

speed = 255 * (150 / 255) = 150
differential = 150 - (150 * (100 / 255)) = 91.5

Example in the left quadrant

The reason I call them speed and differential is because the wheel you set to either value will depend on the direction. The speed is always the faster wheel.

Programming Round #1

Let's go ahead and program this. We're going to first remove the test buttons we used in the last step, we won't need those. Then we'll pull over an accelerometer from the sensors group.

Add accelerometer to canvas

Go into blocks view, and let's move the blocks we won't be touching out of the way.

Arrange Canvas

Create Clock

We'll be using the clock, and we're going to drag over the accelerometer's OnChange event.

Add accelerometer on change event

Create 2 Global Variables

Next, create two global variables to hold the X and Y values from the accelerometer.

Create global Variables

Drag the value 0 from the Math section:

Create global Variables

Set x and y to 0:

Create global Variables

They should look like this:

Global variables

Set Global Variables from Accelerometer

Next, we'll set those global variables from the Accelerometer readings. Click xAccel and a box will pop up. Drag the set block over:

Set Accelerometer OnChange

Select global x in the drop-down:

Set Accelerometer OnChange

Click xAccel and 2 options will pop out. We want to get the accelerometer reading, so drag that onto the canvas:

Set Accelerometer OnChange

We want to do the same thing with the y, so duplicate the x:

Set Accelerometer OnChange

And change the values to y:

Set Accelerometer OnChange

One little caveat is that the values coming from the accelerometer don't match up with the cartesian plane as we learned during the Microbit series..the + and - are swapped. Since 3D printers and Mills also have left as (-), right as (+), I am going to multiply by -1 to switch them around. This way, when you tilt the phone left, the robot will go left. It would hard to steer otherwise, wouldn't it?

Set Accelerometer OnChange

Math Skill:  Multiply a number by -1 to get the opposite sign

Your final blocks should look like this:

Set Accelerometer OnChange

Write Accelerometer Values to Labels on the Screen

Next, we'll write these values to the labels we created in the last tutorial.

Click on the XLabel and drag the set Xlabel.Text to block to the canvas:

Set Labels

The "Join" block (under Text), is used to combine one string with another. In this case, it will create the string "X=" and then the value of of the Global Variables (x or y), and will round to "0" decimal places.

Drag the join block over to the canvas:

Set Labels

Drag a text block to the top slot of the join and type X= in it:

Set Labels

Under Math, drag the format as decimal number block to the canvas:

Set Labels

Get the global x variable and place it in the top slot:

Set Labels


We need to set the other label to display the Y axis.  

Rather than doing everything we just did for the X axis again for Y...

What can we do instead?

After you duplicate the block for x, your blocks should look like this:

Set Labels

Testing Round #1

Next, test the app to see how you're doing so far. Click Connect then AI Companion to bring up the QR code:

Set Labels

You may notice that you are not seeing the values change as you might expect. We need the range to be - to + 255 in a comfortable range. In other words, we don't want to have to twist our wrists around to go full speed.

Adjusting the Multiplier

Let's go back into the app, and i'm going to set this -1 to -60. I had to play around with this number to get the range right, so we wouldn't have to spend time here. This process often involves a lot of tweaking and testing.

Set Labels

Test Again

We'll switch back to the android app, which should automatically update, and now you can see the numbers are more in our range. So far, so good.

Part 2

In Part 2 of this series, we're going to continue on with programming the robot:

Be sure to subscribe to our channel!