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

Image for Programming the Tinker Pi Robot for Arduino Using App Inventor Part 3 #tt27
Tinkerer: Lory Livezey

In Part 3 of this series, we're going to finish programming the robot by building out the left/right pivot, stop/go, and the reverse movements.

Objective

You will practice and learn how to:

  • Create containers with images in App Inventor
  • Program the accelerometer
  • Send accelerometer readings to the Arduino

Previous Step

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


Starter Breakpoint

Arduino Sketch (continuing from #tt25):

Starter Sketch


What you will need


Direction Buttons

Now that the wheels are working, we'll go ahead and finish out the interface. It would be great if you could copy and paste the existing objects like you can in Make Code, but this isn't a feature that App Inventor has. You will have to build these out like you did the first three.

  • Create another row (horizontal alignment container) for pivot left, stop & go and pivot right (3 horizontal alignment containers inside the first one).
  • Set the 3 inside boxes to 50px height, 50px width, and center them inside the parent by setting the center on the PARENT.
  • Then create another row for reverse left, reverse, and reverse right.
  • Set the images that you uploaded in the previous tutorial
  • Rename the objects you intend to program, so you can identify them in blocks view.

Direction Buttons

Rename Buttons

Below the images and above the Debug label, put two more labels to display the formulas. Pull over two labels and set them to "Speed Formula" and "Differential Formula".

  • Width: Fill Parent
  • Font: 18
  • Text Align: Center
  • Text: Speed Formula & Differenatial Formula
  • Names: speedFormula & differentialFormula

Create Formula Labels

At the bottom of the clock event, set the text of the labels to show the formula with the actual current values plugged in.

Populate Labels

Hint: If you are using a smaller device, such as a phone, you may want to shorten the text so it doesn't wrap. Rather than "differential=", you could use "d=".


Setting Images to Blank

Only one "direction" image will show on the screen at a time. At the beginning of each loop, we need to set all of the images to blank initially, then once the new direction is determined, that image will be displayed. Let's set all of the images to blank at the beginning of the loop:

Set Images to Blank

If you test your robot now, you will see the forward images changing as you tilt your phone


Reverse Direction

The reverse direction is most like the forward direction, so let's duplicate that block. The difference between the forward and reverse directions is that the y will be less than 0 for reverse:

Reverse Direction Insert Else If

Hint: You will save yourself a lot of trouble by inserting the Else If block in the right place. If you don't, you will have to move the others around, or worse! If you remove one, you will have orphaned blocks everywhere! So, rather than removing one in the middle, move the others up first, then remove the one at the bottom.

Reverse Direction Duplicate Forward

Change the y to < 0:

Reverse Direction Change Y

Testing Reverse: You can keep your MIT App Inventor App connected while you make changes, and they should refresh automatically. If you test your robot now, it should move in reverse.


Reverse Left/Right

Next is the Reverse Left/Right. Add 2 more else if blocks to the bottom. Then you can copy the last block in the loop -- the forward right:

reverseLeft:  x < 0 and y < 0
reverseRight: x < 0 and y > 0

Reverse Left


Pivot Left/Right

Next is Pivot Left and Pivot Right. We'll place these at the very top of the If/Then block we've been working in. When it comes to if/then/else, order matters. If two or more of the conditions you are setting would apply, then the first one that is evaluated wins. This is why we'll put the pivots at the top.

Pivot Left and Right

If you test your robot now, and tilt your Android to the left or right while keeping the Y axis fairly level, you should see the pivot images. This will also send pivotLeft and pivotRight in the direction parameter to the Arduino. We haven't programmed that yet, but we will finish one last task in App Inventor before we do that.


Stop & Go

We are going to have our robot stop when the device is fairly level. Since you can't expect a person to hold the phone exactly level, we're going to build in a tolerance. A tolerance is an amount on either side of the optimum number that we're going to say is close enough.

Since we want the stop function to override anything else, it will go at the very top of the if/then block:

Stop Block

Finally, we'll need to put a block after the whole if/then block to check to see if we're stopped. If we are not stopped, then we'll display the go image:

Go Block


The Arduino (Again)

We need to program the Arduino for the Pivot Left and Pivot Right scenario. Let's pop back over there, and add two else blocks for pivotLeft and pivotRight. We'll switch the HIGH/LOW on one of these motors.

Arduino Pivot

If you test your robot after you upload this change to your Arduino, the Pivots should now work!


Enlarge Images

Now that everything is on the screen, we can make the images as big as possible while keeping everyting on the screen. Set them to 75px x 75px:

Images Larger


Extra Credit: Adjusting the Formula

Let's also adjust the formula so that the robot doesn't steer so crazy-like. If you think about what's happening when it turns too sharp, the difference between the speed of the wheels is too much. This means we need to adjust the differential to lessen that range. I'm going to use a spreadsheet to work this out (you can download it here).

Column A has the differential, and Column B has the X value. We'll set the speed at 255, and X to 255 to start out. Plug that formula in. Then we'll change the X value down Column C and copy the formula. When one wheel is 255 and the other is 0, that's too sharp of a turn. We'll set a multiplier, say .4, and multiply x. Plug that in, the differential is closer. You can play around with this value and see if you get better results.

We'll set a global variable, plug it into our formula, then put it in the debugging at the bottom.

Add global variable for multiplier

Plug in multiplier


Finish Breakpoint

Arduino Sketch:

Finish Sketch

App Inventor:

Finish AIA


Summary

This has been a big project! And if you're still with me, you will have learned a great deal about the Arduino, MIT App Inventor, Bluetooth, and more! I hope you'll come back and join me for other adventures in IoT! We'll be adding a Raspberry Pi to our robot, so check back and subscribe!

Be sure to subscribe to our channel!