0% found this document useful (0 votes)
926 views

Prizm Programming Guide

lool

Uploaded by

Bucur Matei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
926 views

Prizm Programming Guide

lool

Uploaded by

Bucur Matei
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 168

TETRIX PRIZM Robotics Controller

Programming Guide

43212
Content advising by Paul Uttley, Pamela Scifers, Tim Lankford, Nevin Jones, and Bill Holden.

Build creation and SolidWorks Composer and KeyShot renderings by Tim Lankford, Brian Eckelberry, and Jason Redd.
Desktop publishing by Todd McGeorge.

2016 Pitsco, Inc., 915 E. Jefferson, Pittsburg, KS 66762

All rights reserved. This product and related documentation are protected by copyright and are distributed under licenses
restricting their use, copying, and distribution. No part of this product or related documentation may be reproduced in any
form by any means without prior written authorization of Pitsco, Inc.

All other product names mentioned herein might be the trademarks of their respective owners.
V1.0
Check TETRIXrobotics.com for PDF updates of this guide. 10/16
Table of Contents
TETRIX PRIZM Robotics Controller Introduction..................................................................................2
PRIZM Controller Technology Overview...................................................................................................................3
PRIZM Setup......................................................................................................................................................................................................5
Software Overview..................................................................................................................................................................................8
Software Setup..............................................................................................................................................................................................9
Getting Started Activities
Activity 1: Hello World!............................................................................................................................................................................15
Activity 2: Moving Your DC Motors...................................................................................................................................................19
Activity 3: Moving Your Servo Motors..............................................................................................................................................22
Activity 4: Introduction to the Line Finder Sensor......................................................................................................................25
Activity 5: Introduction to the Ultrasonic Sensor........................................................................................................................29

Building and Coding the PRIZM TaskBot


Hardware Overview.................................................................................................................................................................................36
Activity 6: Build the TaskBot.................................................................................................................................................................51
Activity 7: Drive Forward........................................................................................................................................................................84
Activity 8: Drive in a Circle.....................................................................................................................................................................87
Activity 9: Drive in a Square..................................................................................................................................................................90
Activity 10: Simplify the Square..........................................................................................................................................................93
Building Interlude: Make the TaskBot Smart!................................................................................................................................96
Activity 11: Drive to a Line and Stop.............................................................................................................................................. 102
Activity 12: Follow a Line.................................................................................................................................................................... 105
Activity 13: Drive Toward a Wall and Stop................................................................................................................................... 108
Activity 14: Avoiding Obstacles....................................................................................................................................................... 111
Building Interlude: Give the TaskBot Attitude!.......................................................................................................................... 114
Activity 15: Combining the Sensors............................................................................................................................................... 120
Build, Code, Test, Learn . . . Go!......................................................................................................................................................... 123

Appendix
TETRIX PRIZM Robotics Controller Technical Specifications................................................................................................ 124
PRIZM Controller Component Overview and Pinout Diagrams........................................................................................ 126
TETRIX PRIZM Arduino Library Functions.................................................................................................................................... 133
TETRIX PRIZM Arduino Library Functions Chart....................................................................................................................... 146
TETRIX PRIZM Arduino Library Functions Cheat Sheet.......................................................................................................... 153
TETRIX PRIZM Sample Code Library.............................................................................................................................................. 154
Welcome, Coders, Roboticists,
Engineers, Students, Teachers,
Mentors, and Coaches,
Pitsco Education is pleased to bring you the TETRIX PRIZM Robotics Controller Programming Guide
an exciting and progressive series of activities that teaches the essentials of learning to program your
TETRIX MAX creations using the PRIZM controller and the Arduino Software (IDE).

This programming guide offers a valuable tool for teaching students and teachers how to use the
PRIZM controller (the brain) and the TETRIX MAX system to build and code smart, precise robots that
are as real-world as it gets. The guide comes with five getting started activities, step-by-step building
instructions for creating a TaskBot, 10 complete TaskBot-oriented lessons, and Hacking the Code
extension activities, making it a great tool for exploring the functionality of the PRIZM controller,
TETRIX hardware components, and Arduino Software (IDE) and offering students a great foundation to
build on.

By combining the plug-and-play PRIZM controller with the intuitive MAX building system and an easy-
to-use, text-based software environment, this solution offers a great entry into teaching and learning
through robotics. The progressive nature of the activities enables robotic creations to come to life
quickly and easily, meaning students can experience instant success and focus more classroom time
on problem solving and applying their STEM knowledge.

Plus, PRIZM is not just a great tool for teaching programming. It can bring to life lessons on sensors,
power, gear ratios, and more. Even the controllers clear polycarbonate shield was designed to
maximize educational value letting users see the inner architecture of the controller.

We have also included some STEM Extensions (concepts beyond the scope of this guide) that can
be covered in each lesson if you choose to do so. These extensions can be incorporated if you have
content knowledge of these concepts or if you can work with other teachers to integrate these
concepts.

We hope this guide offers a great jumping-off point to learning with PRIZM. We cannot wait to see the
innovative projects and robotic creations that result.

Happy coding,
building, problem
solving, engineering,
and collaborating!

2 Getting Started
PRIZM Controller Technology Overview
PRIZM Robotics Controller:
A programmable device that is the brain of a TETRIX MAX robot.

4 digital sensor ports


6 standard control servo ports
1 I2C port
3 analog 1 motor controller
sensor ports expansion port

USB
programming
port

Stop/Reset
button
Start
button

2 continuous
rotation
servo ports

2 DC motor
control
ports

2 quadrature
Battery connection port encoder input ports

For complete, detailed specifications, please refer to the TETRIX PRIZM Robotics
Controller Technical Specifications located in the appendix on page 124.

Getting Started 3
Sensor:
A device that detects and responds to surrounding environmental factors

Ultrasonic Sensor: Line Finder Sensor:


Enables a robot to measure Enables a robot to follow
distance to an object and a black line on a white
respond to movement background or vice versa

Motor:
A machine that produces motion or power for completing work

DC Motor: Powerpole Motor Cable:


12-volt DC motor that Connects the DC Motor to
features 152 rpm and the power source
320 oz-in. of torque

Standard Servo Motor: Continuous Rotation


Allows for exact Servo Motor:
positioning within a Allows for continuous
180-degree range of direction of movement,
motion both forward and backward

Encoder:
A sensing device that converts motion to an electrical signal that is read by the PRIZM controller to determine position,
count, speed, or direction of the TETRIX DC Motors

Motor Shaft Encoder:


Allows you to program the DC motors using both rotation
and degrees. Please note: The TETRIX Motor Shaft Encoder
Pack does not come as part of the robotics set and requires an
additional purchase.

4 Getting Started
PRIZM Setup

Attaching the Sensors:


To connect a sensor to the PRIZM, plug one
end of the sensor adapter wire into the
sensor. Plug the other end into ports labeled
D2-D5 or A1-A3.

Attaching the DC Motors:


To connect a DC motor to the PRIZM, attach
the white end of the Powerpole connector
to the motor and plug the mating colored
connectors into one of the two motor
ports. Connectors should be plugged in
RED to RED and BLACK to BLACK for proper
operation.

Attaching the Servo Motors:


To connect a standard servo motor to the
PRIZM, plug one end of the servo wire into
servo ports 1-6. To connect a continuous
rotation servo motor to the PRIZM, plug one
end of the servo wire into servo ports labeled
CR1 or CR2.

Getting Started 5
Downloading and Uploading:
The PRIZM USB port is used for
communication between PRIZM and a
Windows, Mac, or Linux device.
The port enables users to download and
upload data from the computer to the PRIZM
controller.
To upload a program to the PRIZM, plug
the B Connector of the USB cable into the
controllers USB labeled port and plug the A
Connector into your device.

Powering the PRIZM:


The PRIZM controller is powered by a
TETRIX 12-Volt Rechargeable NiMH Battery
Pack.
Included with the PRIZM controller is an
on/off power switch assembly designed to
connect the battery pack connector to the
PRIZM-style Powerpole battery connection
port.
Instructions for assembling the battery and
on/off kit are included in the Building the
TaskBot activity in this guide.

Warning: Do not attempt to use third-party battery packs with the PRIZM
controller. The TETRIX battery packs are equipped with a safety fuse and are
the only packs approved for use with the system. Damage to the product as a
result of doing so will void your warranty.

6 Getting Started
Attaching the Battery to the PRIZM:
To connect the battery pack to the PRIZM,
plug the colored battery connectors into
one of the two battery ports located on the
controller. Connectors should be plugged
RED to RED and BLACK to BLACK for proper
operation. Do not connect in reverse polarity.
The battery pack can be plugged into either
the top or bottom row of the port.

Warning: Do not attempt to connect two battery packs to one PRIZM


controller. Damage and failure will occur if this is attempted. Use only one
battery pack to power the PRIZM system and any extra daisy-chained
expansion controllers.

Getting Started 7
Software Overview
Note: This is not meant
The Arduino Integrated Development Environment (IDE) is open-source software
to be a complete tutorial
that compiles code and sends it to the hardware. As a text-based and developer-
on programming with the
friendly software with a huge existing user base, the Arduino Software (IDE) is
Arduino C-based language.
supported for a variety of Windows, Macintosh, and Linux devices. This makes the
There are many excellent
Arduino Software (IDE) flexible, classroom friendly, and competition ready and offers
resources available on the
all users, even the hobbyist, a low entry and high ceiling. All these factors make it
web to learn more about
perfect for beginner and veteran coders alike.
advanced programming
The Arduino Software (IDE) uses a C-based programming language to communicate skills. If you are interested
with the PRIZM. Within the Arduino Software (IDE) an individual program is referred in such resources, a good
to as a sketch. Each of the activities in this guide will involve creating a sketch that place to start would be the
gives instructions to the robot. Arduino website at
www.arduino.cc.
For the purposes of this guide, we will focus on the basics of using the Arduino
Software (IDE) as it applies to the PRIZM controller. Working through examples and
hands-on application of code using a small TaskBot constructed with the TETRIX
MAX robotics building system will show you how easy it is to use Arduino with
PRIZM.

8 Getting Started
Software Setup
Note: All instructions
The first thing we need to do is install the Arduino Software (IDE). The software can and screen shots used
be found at the Arduino website (www.arduino.cc) for Windows, Macintosh, and throughout this guide are
Linux operating systems. From the Arduino homepage, click the Download tab. On based on the 1.6.11 version
the Download page, select the download for your operating system and follow any of the Arduino Software (IDE).
additional instructions. Instructions and views might
Installing the PRIZM Controller Arduino Library slightly vary based on the
platform and version you are
Adding custom libraries can expand the usability of the Arduino Software (IDE). using.
Libraries are collections of code that make it easier to create a program, or, as
Arduino calls it, a sketch. After you have successfully installed the Arduino Software
(IDE), you can add the Arduino PRIZM controller library. The PRIZM controller library
contains special functions written for the TETRIX PRIZM controller. Many, but not all, Teacher note:
of the functions will be explored in our activities. Depending on your
classroom and IT situation,
For a deeper look at all available functions in the PRIZM library, please refer to the
you might want to
appendix section titled TETRIX PRIZM Arduino Library Functions.
download and install the
The PRIZM library is distributed as a .zip file: TETRIX_PRIZM.zip. The first step is to Arduino Software (IDE) and
download the PRIZM library from the TETRIX website. We can find the library at the TETRIX PRIZM Arduino
www.TETRIXrobotics.com/PRIZMdownloads. Library on the computers
you and your students will
After the library has been downloaded, there are two ways to install the PRIZM be using.
library into the Arduino Software (IDE).
It is recommended that
Importing a .zip Library you organize the class into
One way is to import it using the Arduino Software (IDE) Add .ZIP Library menu teams of two to four It is
option. recommended that you
go through each process
In the Arduino Software (IDE), navigate to Sketch > Include Library. From the drop- before students do. This
down menu, select Add .ZIP Library (Figure 1). will enable you to have a
good understanding of
what student questions
might arise and how to
answer those questions.

Tip: Figures within this


section show typical
installation within Windows.
The look and file locations
within the Mac and Linux
operating systems might
vary.

Figure 1

Getting Started 9
You will be prompted to select the library you would like to add. Navigate to the
location where you saved the TETRIX_PRIZM.zip library file, select it, and open it
(Figure 2).

Figure 2

Return to the Sketch > Include Library menu. You should now see the library at
the bottom of the drop-down menu. It is ready to be used in our sketches; however,
example sketches for the library will not appear in the File > Examples menu until
after the Arduino Software (IDE) has been restarted.
Manual Installation
To install the PRIZM library manually, first close the Arduino Software (IDE)
application. Then, extract the .zip file TETRIX_PRIZM.zip containing the library. After
the folder is extracted, drag or copy the TETRIX_PRIZM folder into the Arduino
libraries folder.
For Windows users, it will likely be called Documents\Arduino\libraries.
For Mac users, it will likely be called Documents/Arduino/libraries.
For Linux users, it will be the libraries folder in the Arduino sketchbook.
Restart the Arduino Software (IDE). Make sure the TETRIX_PRIZM library appears in
the Sketch > Include Library menu of the software.
In addition, several PRIZM sketch examples will now appear in the File > Examples
> TETRIX_PRIZM drop-down menu.
Thats it! We have successfully installed the PRIZM Arduino library.

10 Getting Started
Configuring USB Communication
PRIZM and the Arduino Software (IDE) will communicate with each other through
the computers USB port.
Therefore, before we can begin programming, we first need to be sure that the
PRIZM controller is properly set up in the Arduino Software (IDE) for communication
over the USB port.
The easiest way to do this is to first start the Arduino Software (IDE) and navigate to
Tools > Board and select Arduino/Genuino Uno (Figure 3). The PRIZM controller
uses the same processor chip as a genuine Arduino UNO, so this is the board you
will select.

Figure 3

Getting Started 11
Next, without the PRIZM connected, navigate to Tools > Port (Figure 4) and check
the current connections. If there are no current connections detected, the word Port
will be grayed out. If there are connections detected, take note of the COM ports
that are listed.

Figure 4: Port drop-down menu with PRIZM not connected.


Please note that lists might vary.

Next, plug the PRIZM controller into a USB port and power it up by connecting the
TETRIX battery pack and turning the power switch on.

12 Getting Started
With power applied, the blue power indicator LED will be lit. Be sure to give the
PRIZM controller time to complete the first-time connect installation. This could
take 5-10 seconds. After the PRIZM has been connected and installed, it will be
assigned a COM port by the computer system.

Navigate to Tools > Port and select the newly installed COM port. The new COM
port will be the PRIZM. By selecting the new COM port, you are telling the Arduino
Software (IDE) to use this port for communications. The COM port you use will likely
be different from the one in Figure 5.

Tip: Each PRIZM unit will


use a different COM port
on the same computer. For
each new PRIZM that is
connected, follow the steps
for numbering controllers
Figure 5: Port drop-down menu after PRIZM is and matching to the
connected to USB port and powered on. computer as detailed above.
One potential way around
The new port that appears in this example is COM32. this issue is to number
each PRIZM controller and
Select the new port item to tell the Arduino Software (IDE) to use this port for assign it to a corresponding
communications. Our port will likely be different and that is OK. When the computer. This will facilitate
communications port has been set up, communications with the PRIZM controller the computer selecting the
have been enabled and coding can begin. correct port for PRIZM each
When this step is complete, our computer system will automatically default to time it is connected and
this selected port each time we plug in our PRIZM controller and start the Arduino powered up.
Software (IDE).

Getting Started 13
Getting Started Activities
Note: If you are already
Now, it is time to get started with the activities. Each of the five getting started
comfortable with coding in
activities is designed to introduce you to the Arduino Software (IDE) and how it
Arduino sketches and want
works with the PRIZM and select basic hardware. Success with these first five
to jump ahead, an overview
activities will demonstrate how easy it is to use Arduino Software (IDE) with the
of each library function can
PRIZM and prepare you for coding the PRIZM TaskBot.
still be a helpful starting
The Arduino Sketch point. We have created
and included definitions of
As we begin creating our first sketch, it is necessary to understand some basic rules the functions along with
about sketches. It is best to think of a sketch as a list of instructions to be carried out descriptions that show how
in the order that they are written down. Each sketch will have several instructions, each would appear in an
and typically each instruction will be one line of text within the sketch. Lines of text Arduino sketch. You can find
within a sketch are also known as code, which is why programming is sometimes these in the appendix of this
called coding. guide on pages 133-145.
Most of the rules that affect text-based programming are known as syntax, which is More library functions might
similar to grammar and punctuation in English. be added in the future as the
library is updated to newer
For instance, each line of code must end with a semicolon just like a period marks versions.
the end of a sentence. As we come across more coding rules in the activities, we will
make sure to explain them.
Many times the best way to learn how to code is by following an example. In the Note: In addition to the
following sections we will work through several coding examples to better explain PRIZM library, there is an
how to create sketches and upload them to the PRIZM controller. entire collection of Arduino
An example sketch of each function can be found in the File > Examples > language commands that
TETRIX_PRIZM drop-down menu. are necessary to understand
before we can create
functional programs. The
Arduino language reference
as well as numerous
language learning tutorials
can be found by visiting the
Arduino homepage at
www.arduino.cc.

14 Getting Started Activities


Activity 1: Hello World!
Lets begin with a very simple sketch that will blink the onboard PRIZM red LED. This
activity will be the PRIZM equivalent of a Hello World! program, which is usually
the intro activity for any beginning programmer. The sketch we will create is one of
the simplest and most basic PRIZM functions and requires only the PRIZM, a power
source, and a USB connection to the computer.

Parts Needed
PRIZM controller
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
PRIZM Controller On/Off Battery
Switch Adapter
Computer

Opening the Sketch


Lets start by looking at our first example sketch. Open the sketch by selecting File >
Examples > TETRIX_PRIZM > GettingStarted_Act1_Blink_RedLED. A new sketch
window will open titled GettingStarted_Act1_Blink_RedLED (Figure 6).

Figure 6

Getting Started Activities 15


Building the Knowledge Base
Before we can upload the sketch to the PRIZM, we need to make sure the PRIZM
has power, is connected to the computer, and is detected by the computer (Figure
7). When the PRIZM is connected as shown, power on the PRIZM with the on/off
switch. You will know the PRIZM has power by the glowing blue light. To see if the
PRIZM is detected by the computer, check the port as we did in the Configuring
USB Communication section.

Figure 7

Executing the Code


To upload the sketch to the PRIZM, click Upload (Figure 8).

Figure 8

As the data uploads, the yellow LEDs to the side of the USB connection will flash.
When the upload is finished, there will be a solid green LED beside the red Reset
button. The green LED means the code is ready to execute. Press Start to execute
the code. The red LED next to the Reset button will blink off and on in one-second
intervals. To stop the program, press the Reset button.
Congratulations! You have successfully uploaded your first sketch to the PRIZM and
demonstrated the results to the world.

16 Getting Started Activities


Moving Forward
Tip: Remember that you
For more detailed information about the sketch process and the PRIZM library can print and use the TETRIX
functions involved in blinking the LED, refer to the following sections within the PRIZM Arduino Library
appendix: TETRIX PRIZM Arduino Library Functions: Functions Cheat Sheet within
Page 133 about include statements the appendix on page 153 as
a quick reference for all the
Page 134 about initialization functions functions available to you.
Page 136 about the Start button
Page 137 about onboard LEDs
Tip: Want to see this in
For now, lets change some parameters in our code and see how it affects the action? You can by watching
behavior of the red LED. According to the comments in the example, the delay our RoboBench video series
function defines the duration the LED is on or off. This is a parameter we can for the PRIZM Programming
change in our code. Experiment with changing those values to create new blinking Guide. You can find the
behaviors for the LED. Try making the LED blink faster or slower. entire series at video.
tetrixrobotics.com or on
the TETRIXrobotics YouTube
channel.

Real-World Connection
Many things within the electronic world around us blink, such as caution lights for
road construction warnings, waffle irons (the blinking light turns solid when the
waffles are ready), or notifications on our phones indicating that we have incoming
calls. The rate at which these items blink or when they blink is controlled by
electronics. This control can be from simple timing circuits or it can be from
computers or other devices with microprocessor chips.
STEM Extensions
Science
Electricity terms (voltage, current, and resistance)
How an LED works
What determines the color of an LED
Technology
How computers work
How computers are programmed
Engineering
Problem-solving process
Math
Frequency
Duration
Sequence

Getting Started Activities 17


Hacking the Code Activity
With the example as a reference, try creating the blinking LED in a new sketch.
Instead of just blinking the red LED, try to blink the green LED too. Flashing or
blinking lights have a rich tradition as a method of signaling or long-distance
communication. You could challenge yourself to communicate Hello World! in
blinking Morse code.
To start a new sketch, select File > New. Be sure to use the appendix: TETRIX PRIZM
Arduino Library Functions in the back of the guide for help with sketch structure
and syntax. An example library of code can also be found in the appendix for those
that need extra help. Do not forget to use basic software skills such as copy and
paste.
When creating your own sketch, there is a built-in software tool to help ensure your
code is free of syntax errors. You can check your syntax by clicking Verify (Figure 9).
This will cause the code to compile but not upload. You will be prompted to save
your sketch before verification.

Figure 9

If there are errors in the code, they will be displayed in the compiler error window at
the bottom of the sketch window (Figure 10).
Errors will need to be corrected before code can be uploaded to the PRIZM Tip: In the sketch window
controller. If there are no errors, the compiler will complete and indicate that it is the PRIZM library functions
done compiling, and you can upload your code. change color when they
are typed or spelled
correctly. Consequently,
if spelled incorrectly they
will not change. In Arduino,
the PRIZM functions are
recognized by the software
as keywords and will turn
orange when the syntax is
correct.

Tip: An example library of


code to help you get started
with this challenge can be
found in the appendix. If you
have a digital copy of this
guide, you can simply copy
and paste the sample code
for each activity into your
sketch window. A digital
download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.
Figure 10

18 Getting Started Activities


Activity 2: Moving Your DC Motors
For our second activity we want to keep things simple but add an element of
motion. We will create a sketch that will rotate a TETRIX DC Motor.

Parts Needed
TETRIX DC Motor
TETRIX Powerpole Motor Cable
PRIZM controller
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
PRIZM Controller On/Off Battery
Switch Adapter
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later. Open the sketch by selecting File > Examples > TETRIX_PRIZM
> GettingStarted_Act2_Move_DCMotor. A new sketch window will open titled
GettingStarted_Act2_Move_DCMotor (Figure 11).

Figure 11

Getting Started Activities 19


Building the Knowledge Base
In this second sketch we want to take a closer look at the sketch syntax, specifically
comments. Comments are lines in the program that are used to inform yourself or
others about the way the program works. They are for informational purposes only
and do not affect the code.
There are two types of comments: single line and multiline. Single-line comments
are preceded by // and anything after // is a comment to the end of the line.
Multiline comments are preceded by /* and can be several lines long but must be
closed with */.
When you look at this second sketch, the comments explain how the sketch author
intended this program to work. The intent of this sketch is to spin the DC motor
channel 1 for five seconds and then coast to a stop. After two seconds of coasting,
the motor will spin in the opposite direction. This behavior will continue until the
red Reset button is pressed.

Executing the Code


Before we can upload the sketch to the PRIZM, remember to check our connections.
Keep in mind that we added a new connection with the motor.
Upload the sketch. The green LED will light up, indicating that the code is ready
to execute. When this has happened, press the green Start button on the PRIZM
controller.
Observe the direction and duration of the motor rotation. Based on the sketch
comments, did the behavior match expectations?
Press the red Reset button when you are ready to stop the motor.

20 Getting Started Activities


Moving Forward
Tip: Remember that you
The function used in this sketch is prizm.setMotorPower. It has two parameters: can print and use the TETRIX
motor channel and motor power. In the example, prizm.setMotorPower(1,25) PRIZM Arduino Library
means motor 1 will spin at 25% power clockwise. The first value in parentheses Functions Cheat Sheet within
defines the motor channel, and the second value in parentheses defines power the appendix on page 153 as
percentage and direction. a quick reference for all the
We can alter the direction and stopping behavior by changing the second value in functions available to you.
parentheses. If the second value is negative, the motor rotates counterclockwise,
as shown in the sketch. If we change the value to 125 instead of 0, the stopping
behavior will change from coast to brake.
Tip: Want to see this in
For more detailed information about the sketch process and the PRIZM library action? You can by watching
functions involved in moving the motor, refer to the following section within the our RoboBench video series
appendix: TETRIX PRIZM Arduino Library Functions: for the PRIZM Programming
Guide. You can find the
Pages 137-142 about DC motor functions
entire series at video.
Practice changing the parameters in the sketch. We can change the motor power, tetrixrobotics.com or on
motor direction, stopping behavior, and delay between functions. Observe the the TETRIXrobotics YouTube
effect these changes have on the motor. channel.

Real-World Connection
Controlling motors is not a new thing. Controlling them by an onboard computer
in an electric car (such as a Tesla) at 70 mph down the road or during a sharp turn
on a curvy highway that is new! For these cars that are driven by electric motors, Tip: In the sketch window
the speed and power levels of all the drive motors must be coordinated to make the PRIZM library functions
the turn in the highway as the driver is turning the steering wheel. All this has been change color when they
programmed into the brains of these cars to make it simple and easy for the driver. are typed or spelled
correctly. Consequently,
STEM Extensions
if spelled incorrectly they
Science will not change. In Arduino,
How DC motors work the PRIZM functions are
recognized by the software
Angular velocity as keywords and will turn
Technology orange when the syntax is
Relationship between power, voltage, and current correct.
Torque
Engineering
Tip: An example library of
Determining load and torque code to help you get started
Math with this challenge can be
Pulse width modulation (PWM) found in the appendix. If you
have a digital copy of this
Revolutions per minute (rpm) guide, you can simply copy
Hacking the Code Activity and paste the sample code
for each activity into your
With the example as a reference, try creating a new sketch to move your DC motor. sketch window. A digital
Remember what we learned from our first activity and think of creative ways to download can be found at
include blinking LEDs with your rotating motor. www.TETRIXrobotics.com/
Be sure to use the appendix: TETRIX PRIZM Arduino Library Functions on page 133 PRIZMdownloads.
for help with sketch structure and syntax.

Getting Started Activities 21


Activity 3: Moving Your Servo Motors
Our third activity will explore another element of motion with servo motors. We will
create a sketch to rotate a servo motor.
Servo motors offer the benefit of being able to move to a set position regardless
of the start position within a limited range of motion. Servo motors have an
approximate range of motion from 0 to 180 degrees. For example, we can tell
a servo to go to position 45 degrees regardless of where it starts. If it starts at 0
degrees, it will move clockwise to 45 degrees. If it starts at 120 degrees, it will move
counterclockwise to 45 degrees.

Parts Needed
180-Degree Standard-Scale HS-485HB
Servo Motor
PRIZM controller
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
PRIZM Controller On/Off Battery Switch
Adapter
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File>Examples>TETRIX_PRIZM>GettingStarted_
Act3_Move_Servo. A new sketch window will open titled GettingStarted_Act3_
Move_Servo (Figure 12).

Figure 12

22 Getting Started Activities


Building the Knowledge Base
In this third sketch we want to continue looking at sketch syntax, specifically the
include statement and the object declaration. The include statement is used to
add the functionality of the PRIZM software library into the sketch. The object
declaration is a technique used when writing programs to make them easier to
manage as they grow in size and complexity.
The PRIZM software library is a collection of special mini programs each with its
own distinct function name. These mini programs are designed to make writing
sketches for PRIZM easy and intuitive. By using the include statement, we add
the functionality of the library to our sketch. The include statement for PRIZM is
#include <PRIZM.h>.
The object declaration is an important statement when using the PRIZM controller
along with the PRIZM software library. In order to use the functions contained in
the PRIZM software library, we must first declare a library object name that is then
inserted as a prefix before each library function. The object declaration we use is
PRIZM prizm;. We use this statement just after the include statement.
In all of our sketch examples, we use an include statement and object declaration to
add the functionality of the PRIZM software library. Include statements and object
declarations are common for most forms of C-based language.

Executing the Code


Before we can upload the sketch to the PRIZM, remember to check our connections.
Keep in mind that we added a new connection with the servo motor.
Upload the sketch. The green LED will light up, indicating the code is ready to
execute. When this has happened, press the green Start button on the PRIZM
controller.
Observe the direction and duration of the servo motor rotation. Based on the
sketch comments, did the behavior match expectations?
Press the red Reset button when you are ready to stop the motor.

Getting Started Activities 23


Moving Forward
Tip: Remember that you
In this sketch we introduce two new PRIZM functions, prizm.setServoSpeed and can print and use the TETRIX
prizm.setServoPosition. Both functions have two parameters, but they are different. PRIZM Arduino Library
The two parameters of the prizm.setServoSpeed function are servo channel and Functions Cheat Sheet within
servo speed. In the example, prizm.setServoSpeed(1,25) means servo 1 will spin at the appendix on page 153 as
25% power while it rotates to the position commanded by the a quick reference for all the
prizm.setServoPosition function. This function needs to be called only once at the functions available to you.
beginning of the program.
The two parameters of the prizm.setServoPosition function are servo channel and
target position. In the example, prizm.setServoPosition(1,180) means servo 1 will Tip: Want to see this in
rotate to the target position of 180 degrees. action? You can by watching
our RoboBench video series
In the sketch both of these functions work together to tell the servo motor not only
for the PRIZM Programming
the target position but also the speed to use while moving to the target position.
Guide. You can find the
We can alter the position and speed of the servo by changing the values of both
entire series at video.
functions.
tetrixrobotics.com or on
For more detailed information about the sketch process and the PRIZM library the TETRIXrobotics YouTube
functions involved in moving the servo motor, refer to the following section within channel.
the appendix: TETRIX PRIZM Arduino Library Functions:
Pages 144-145 about servo motor functions
Practice changing the parameters in the sketch. Observe the effect these changes
have on the servo motor.
Real-World Connection
Historically, servo motors were used mostly with a remote-controlled (R/C)
transmitter for R/C model cars to control steering or R/C model airplanes to control
flaps and rudders. Robots can use servos controlled by R/C transmitters but they
can also use servos controlled by PRIZM to operate robotic arms, grippers, tilting
Tip: In the sketch window
and rotating mounts for cameras, or many other applications in which precise
the PRIZM library functions
movement is needed.
change color when they
STEM Extensions are typed or spelled
correctly. Consequently,
Science if spelled incorrectly they
Levers will not change. In Arduino,
Centripetal force the PRIZM functions are
recognized by the software
Technology
as keywords and will turn
Mechanical linkages orange when the syntax is
Transmission of force correct.
Engineering
Applying torque
Tip: An example library of
Math
code to help you get started
Radian vs Cartesian measurements with this challenge can be
Arc length found in the appendix. If you
have a digital copy of this
Hacking the Code Activity guide, you can simply copy
and paste the sample code
With the example as a reference, try creating a new sketch to move your servo for each activity into your
motor. Remember what we learned from our previous activities and think of sketch window. A digital
creative ways to combine the functions you have learned. download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

24 Getting Started Activities


Activity 4: Introduction to the Line Finder Sensor
For the fourth activity we will change focus from motor outputs to sensor inputs
by introducing and exploring sensors. In this example we will connect a Line Finder
Sensor to digital sensor port D3, and we will create a sketch to read a digital input
from the Line Finder Sensor.
Sensors enable us to gather information from the world around us. The type of
information depends on the type of sensor. The Line Finder Sensor uses reflected
infrared light to distinguish between light and dark surfaces.

Parts Needed
Contrasting light and dark surface
Grove Line Finder Sensor and cable
PRIZM controller
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
PRIZM Controller On/Off Battery
Switch Adapter
Computer

Figure 13: Contrasting light and dark surface


Opening the Sketch
Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM >
GettingStarted_Act4_Intro_LineFinder. A new sketch window will open titled
GettingStarted_Act4_Intro_LineFinder (Figure 14).

Figure 14

Getting Started Activities 25


Building the Knowledge Base
For the fourth sketch we want to take a closer look at two of the fundamental
structure elements that make up a sketch. In every sketch you write, there will be a
setup() and a loop() function.
The setup() function follows right after the include statement and object
declaration as part of the beginning of our code. The setup() function contains
items that need to be run only once as part of the sketch. Many functions can
go here, but we always use at least the PRIZM initialization statement, prizm.
PrizmBegin(). The main purpose of this function is to configure the Start button.
The loop() function does exactly what its name suggests. Everything contained
within the brackets of the loop will repeat consecutively until the sketch is ended
with a command or the Reset button. The loop() contains the main body of our
code.
The contents of the setup() and the loop() are contained between curly braces.
A left curly brace { begins a group of statements, and a right curly brace } ends
a group of statements. They can be thought of as bookends, and the code in
between is said to be a block of code.

26 Getting Started Activities


Executing the Code
Note: The Line Finder Sensor
Before we can upload the sketch to the PRIZM, remember to check our connections. has an adjustable range that
Keep in mind that we added a new connection in digital sensor port 3 with the Line we can tweak by turning the
Finder Sensor. small adjustment screw on
Upload the sketch. The green LED will light up indicating the code is ready to the back side of the sensor
execute. When this has happened, press the green Start button on the PRIZM board.
controller.
Hold the sensor over the contrasting surface. As the sensor moves from light
to dark, observe the red LED on the PRIZM. When the sensor is over a line, non- Tip: Remember that you
reflective, or dark surface, the red LED will be off. When the sensor is over a white or can print and use the TETRIX
reflective surface, the red LED will be on. PRIZM Arduino Library
Press the red Reset button when you are ready to stop the sensor. Functions Cheat Sheet within
the appendix on page 153 as
Moving Forward a quick reference for all the
functions available to you.
This sketch introduces a program structure, a new function, and a comparison
statement. The program structure is an if statement, the new function is
prizm.readLineSensor, and the comparison statement is == (equal to).
The basic if statement enables us to test for a certain condition. If this condition is Tip: Want to see this in
met, then the program can perform an action. If the statement in the parentheses action? You can by watching
is true, the statements within brackets are run; if the statement is not true, the our RoboBench video series
program will skip over the code. for the PRIZM Programming
Guide. You can find the
The function prizm.readLineSensor reads the state of the Line Finder Sensor
entire series at video.
and returns a value of 1 (HIGH) or 0 (LOW). A value of 1 is returned when the
tetrixrobotics.com or on
Line Finder Sensor detects a dark line or a non-reflective surface; a value of 0 is
the TETRIXrobotics YouTube
returned when the Line Finder Sensor detects a white or reflective surface.
channel.
The comparison statement == (equal to) defines a type of test.
When these three elements are combined in the sketch, we create a test condition
based on the input of the Line Finder Sensor that turns the red LED on or off. In
simple terms, if the Line Finder Sensor detects a line or a non-reflective surface,
then it turns the red LED off. If the Line Finder Sensor detects a white or reflective
surface, it turns the LED on.
For more detailed information about the sketch process and the PRIZM library
functions involved in using the Line Finder Sensor, refer to the following section
within the appendix: TETRIX PRIZM Arduino Library Functions:
Pages 134-135 about sensor port functions
Experiment with the Line Finder Sensor on different surfaces and different heights
to see how the sensor reacts.

Getting Started Activities 27


Real-World Connection
Tip: In the sketch window
Finding our way in this world can be challenging. Telling a robot how to find its the PRIZM library functions
way can be challenging as well. One way that robots within a warehouse can find change color when they
their way to the right location is by having them follow lines. But to follow lines, are typed or spelled
they have to detect the lines. One way to accomplish this is by using a sensor correctly. Consequently,
that detects dark and light surfaces. This, through the computer code, provides if spelled incorrectly they
information to the robot about where the line is. Other code controls the DC will not change. In Arduino,
motors to change course if the robot strays from the line. the PRIZM functions are
STEM Extensions recognized by the software
as keywords and will turn
Science orange when the syntax is
Light reflection and absorption correct.
Electromagnetic spectrum
Technology
Digital vs analog Tip: An example library of
code to help you get started
Calibration with this challenge can be
Engineering found in the appendix. If you
Determining an edge location have a digital copy of this
guide, you can simply copy
Math
and paste the sample code
Data analysis for each activity into your
sketch window. A digital
Hacking the Code Activity download can be found at
www.TETRIXrobotics.com/
With the example as a reference, try creating a new sketch to use your Line Finder PRIZMdownloads.
Sensor. Remember what we learned from our previous activities, and think of
additional creative actions to perform based on the condition of the Line Finder
Sensor.

28 Getting Started Activities


Activity 5: Introduction to the Ultrasonic Sensor
For the final getting started activity, we will finish up our exploration of sensors
by creating a sketch using the Ultrasonic Sensor. In this activity we will connect an
Ultrasonic Sensor to digital sensor port D3 and display the distance to an object we
place in front of it using the serial monitor window.
Like all sensors, the Ultrasonic Sensor enables us to gather information. For the
Ultrasonic Sensor, the information gathered communicates distance. The sensor
works by sending a sonic pulse burst and then waiting on its return to the sensor
as it is reflected off an object in range. The reflected sonic pulse time period is
measured to determine the distance to the object. The sensor has a measuring
range of approximately 3-400 centimeters.

Parts Needed
Grove Ultrasonic Sensor and cable
PRIZM controller
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
PRIZM Controller On/Off Battery
Switch Adapter
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM >
GettingStarted_Act5_Intro_UltraSonic. A new sketch window will open titled
GettingStarted_Act5_Intro_UltraSonic (Figure 15).

Figure 15

Getting Started Activities 29


Building the Knowledge Base
For our fifth sketch of the getting started activities, we want to look at a useful tool
for viewing data as a dynamic text output. The serial monitor displays serial data
being sent from the PRIZM via the USB connection.
The serial monitors value as a tool lies in its ability to display data in real time that
enables you to make better-informed design decisions about robot builds and
programming. It can be used to display data from sensors connected to PRIZM or
to examine any program data collected by the PRIZM for example, encoder count
data, DC motor current data, or servo position data.

Executing the Code


Before we can upload the sketch to the PRIZM, remember to check our connections.
Keep in mind that we added a new connection in digital sensor port 3 with the
Ultrasonic Sensor.
Upload the sketch. Before we execute the sketch, we need to open the serial
monitor from the sketch window. To open the serial monitor, click the magnifying
glass in the top-right corner of the sketch window (Figure 16).

Figure 16
The serial monitor will open in a separate window (Figure 17).

Figure 17
With the sensor lying flat on the desk pointed up, press the green Start button to
execute the code.

30 Getting Started Activities


Hold an object above the sensor at varying distances and observe the serial
monitor to see the real-time data. Tip: Remember that you
can print and use the TETRIX
Press the red Reset button when you are ready to stop the sensor. PRIZM Arduino Library
Moving Forward Functions Cheat Sheet within
the appendix on page 153 as
This sketch introduces several new functions: Serial.begin(), Serial.print(), a quick reference for all the
prizm.readSonicSensorCM(), and Serial.println(). functions available to you.
Serial.begin() enables the use of serial communication within the sketch. As an
initialization function that needs to be run only once, it belongs in the setup of the
sketch. An important part of Serial.begin() is the speed of communication in bits
per second, which is called baud rate. The default setting for baud rate in the serial Tip: Want to see this in
window is 9600, so that is what we use in Serial.begin(). action? You can by watching
our RoboBench video series
for the PRIZM Programming
Guide. You can find the
entire series at video.
tetrixrobotics.com or on
the TETRIXrobotics YouTube
channel.

Figure 18
Serial.print() prints data to the serial port as readable text. This can take the
form of dynamic information from another device or static information from the
programmer.
prizm.readSonicSensorCM() reads the state of the Ultrasonic Sensor and returns a
digital value within the designated measurement range. For our sensor, that range
is between approximately 3-400 centimeters. This value should reflect the distance
the Ultrasonic Sensor is from a detectable object.
Serial.println() prints data to the serial port as readable text followed by a built-in
command for a new line.

Figure 19

These four functions might seem complicated, but they actually work together
simply. In this sketch, Serial.begin(9600) enables and defines the speed of
communication in the setup. Serial.print() tells what type of data to print.
prizm.readSonicSensorCM() provides the type of data to print because it is within
the parentheses of Serial.print(). And Serial.println( Centimeters) clarifies the type
of data being printed in this case, with the modifier Centimeters.

Getting Started Activities 31


For more detailed information about the sketch process and the PRIZM library
functions involved in using the Ultrasonic Sensor and the serial monitor, refer to Tip: In the sketch window
www.arduino.cc and the appendix: TETRIX PRIZM Arduino Library Functions: the PRIZM library functions
change color when they
Pages 134-135 about sensor port functions are typed or spelled
Real-World Connection correctly. Consequently,
if spelled incorrectly they
When you were very, very young, you were probably scanned by an ultrasonic will not change. In Arduino,
sensor. If you do not remember this, it is because it was prior to you being born. the PRIZM functions are
One of the great applications of ultrasonic technology is within the medical field. recognized by the software
Doctors can use the reflection of sound waves back to sensor waves to see inside as keywords and will turn
a living person. Doctors can see a babys size and development and determine orange when the syntax is
when they think he or she might be born! correct.
STEM Extensions
Science
Tip: An example library of
Sound wave terminology (frequency, amplitude, crest, trough) code to help you get started
Reflection of sound waves with this challenge can be
Technology found in the appendix. If you
have a digital copy of this
Measuring frequency
guide, you can simply copy
Sound digitization and paste the sample code
Engineering for each activity into your
sketch window. A digital
Applications of sonic measurements
download can be found at
Math www.TETRIXrobotics.com/
Inverse square law PRIZMdownloads.

Hacking the Code Activity


With the example as a reference, try creating a new sketch to use the Ultrasonic
Sensor with the serial monitor. Remember what we learned from our previous
activities and experiment with different objects in front of the Ultrasonic Sensor to
see if they are detectable and if the distances can be measured accurately.
Try changing the code for prizm.readSonicSensorCM() to prizm.readSonicSensorIN()
to display the distance from an object in inches. Also be sure to change the Serial.
printIn from Centimeters to Inches so that the unit is correctly labeled in the
serial monitor window. Understanding how to use the Ultrasonic Sensor will give
your robot vision to be able to steer around objects and obstacles.

32 Getting Started Activities


Building and Coding the PRIZM TaskBot
This is what you have been waiting for. It is time to move to the next level. You
have worked hard to learn the basics, and now it is time to apply them to an
actual robot. The next 10 activities will walk us through building a robot, basic
movement, navigation, adding sensors, and more, culminating in an activity
that combines everything. It is about to get exciting.

Getting Started Activities 33


Programmable Robotics Set
Note: In order to complete the activities shown in this book, you must have the TETRIX MAX Programmable Robotics Set.

34 Building and Coding the PRIZM TaskBot


TETRIX MAX Programmable Robotics Set Parts Index

Servos & Hardware


Part No. Part Name Quantity
Channels 39060 TETRIX MAX Single Standard-Scale Servo Motor Bracket. . . 1
Part No. Part Name Quantity 39593 TETRIX MAX Standard-Scale Pivot Arm with Bearing. . . . . . . . 1
39065 TETRIX MAX 32 mm Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 39197 180 Standard-Scale HS-485HB Servo Motor. . . . . . . . . . . . . . . . . 2
39066 TETRIX MAX 96 mm Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 39081 Servo Extension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
39067 TETRIX MAX 160 mm Channel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 39082 Servo Y Connector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
39068 TETRIX MAX 288 mm Channel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 41789 TETRIX MAX Standard Servo Mounting Kit. . . . . . . . . . . . . . . . . . 2
39280 TETRIX MAX Adjustable Servo Brackets. . . . . . . . . . . . . . . . . . . . . 2

Bars & Angles


Part No. Part Name Quantity DC Motors & Hardware
39070 TETRIX MAX 288 mm Flat Bars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Part No. Part Name Quantity
39072 TETRIX MAX 144 mm Angle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 39089 TETRIX MAX Motor Mount. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
39071 TETRIX MAX 288 mm Angle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 39530 TETRIX MAX DC Gear Motor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Plates & Brackets


Part No. Part Name Quantity Tires & Wheels
39073 TETRIX MAX Flat Building Plates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Part No. Part Name Quantity
39061 TETRIX MAX Flat Brackets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 39055 TETRIX MAX 4" Wheel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
39062 TETRIX MAX L Brackets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 36466 TETRIX MAX 4" Omni Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
39281 TETRIX MAX Inside Corner Brackets. . . . . . . . . . . . . . . . . . . . . . . . . 6
39270 TETRIX MAX Inside C Connectors. . . . . . . . . . . . . . . . . . . . . . . . . . . 6
41790 TETRIX MAX Adjustable Angle Corner Bracket. . . . . . . . . . . . . . 4
Nuts, Screws, & Fasteners
Part No. Part Name Quantity
39094 Kep Nuts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Flats 39097 Socket Head Cap Screws 6-32 x 1/2". . . . . . . . . . . . . . . . . . . . . . . 100
Part No. Part Name Quantity 39098 Socket Head Cap Screws 6-32 x 5/16". . . . . . . . . . . . . . . . . . . . . . 100
39274 TETRIX MAX Flat 64 mm x 16 mm. . . . . . . . . . . . . . . . . . . . . . . . . . . 2 39111 Button Head Cap Screw 3/8". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
39273 TETRIX MAX Flat 96 mm x 16 mm. . . . . . . . . . . . . . . . . . . . . . . . . . . 2 31902 Zip Ties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
39272 TETRIX MAX Flat 160 mm x 16 mm . . . . . . . . . . . . . . . . . . . . . . . . . 2
39271 TETRIX MAX Flat 288 mm x 16 mm . . . . . . . . . . . . . . . . . . . . . . . . . 2
41791 TETRIX MAX Adjustable Angle Flat Bracket . . . . . . . . . . . . . . . . . 6

Battery & Charger


Part No. Part Name Quantity
Axles, Hubs, & Spacers 38009 TETRIX MAX Battery Clips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Part No. Part Name Quantity 39057 TETRIX MAX 12-volt 3,000 mAh Battery. . . . . . . . . . . . . . . . . . . . . 1
39079 TETRIX MAX Motor Hub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 39830 TETRIX MAX 12-volt Battery Charger. . . . . . . . . . . . . . . . . . . . . . . . 1
39172 TETRIX MAX Axle Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
39092 TETRIX MAX Axle Set Collars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
39088 TETRIX MAX 100 mm Axles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
39091 TETRIX MAX Bronze Bushings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
39090 TETRIX MAX Gear Hub Spacers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Tools
39100 TETRIX MAX Axle Spacers 1/8". . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Part No. Part Name Quantity
39101 TETRIX MAX Axle Spacers 3/8". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 36404 4-in-1 Screwdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
39387 TETRIX MAX Flat Round Spacer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 38001 TETRIX Wrench Set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
39104 TETRIX MAX Hex Key Pack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
40341 Miniature Ball-Point Hex Driver. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
42991 2-in-1 Screwdriver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Gearing
Part No. Part Name Quantity
39028 TETRIX MAX Gear 40-Tooth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
39086 TETRIX MAX Gear 80-Tooth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Posts & Standoffs Electronics & Control


Part No. Part Name Quantity Part No. Part Name Quantity
39102 TETRIX Stand-Off Post 6-32 x 1" . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 43000 PRIZM controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
39103 TETRIX Stand-Off Post 6-32 x 2" . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 41352 TETRIX Motor Cable with Powerpoles. . . . . . . . . . . . . . . . . . . . . . . . 2
39107 TETRIX Stand-Off Posts 6-32 x 32 mm. . . . . . . . . . . . . . . . . . . . . . 12 43169 PRIZM Controller On/Off Battery Switch Adapter. . . . . . . . . . . . 1
41253 TETRIX Stand-Off Posts 6-32 x 16 mm. . . . . . . . . . . . . . . . . . . . . . 12 43056 Line Finder Sensor Pack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
43055 Ultrasonic Sensor Pack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
40967 3-foot Type A-B USB Cable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Building and Coding the PRIZM TaskBot 35


TETRIX MAX Hardware Components Overview
The following mechanical parts overview includes elements from the TETRIX MAX Programmable Robotics Set.
Channels
TETRIX MAX structural elements are identified by length. For example, a 32 mm Channel or a 288 mm Flat Bar. Use the
inches ruler below to measure stand-off posts and wheels. Use the centimeters ruler at the bottom of this spread to
measure part lengths.

Structural Elements

32 mm Channel 39065

96 mm Channel 39066

160 mm Channel 39067

288 mm Channel 39068

36 Building and Coding the PRIZM TaskBot


Structural Elements

Flat Bracket 39061

288 mm Flat Bar 39070

144 mm Angle 39072

288 mm Angle 39071

Flat Building Plate 39073

L Bracket 39062

Inside Corner Bracket 39281

Inside C Connector 39270

Adjustable Angle Corner Bracket 41790

Building and Coding the PRIZM TaskBot 37


Structural Elements

Flat 64 mm x 16 mm 39274

Flat 96 mm x 16 mm 39273

Flat 160 mm x 16 mm 39272

Flat 288 mm x 16 mm 39271

Adjustable Angle Flat Bracket 41791

Flat Round Spacer 39387

Stand-Off Post 6-32 x 1" 39102

Stand-Off Post 6-32 x 2" 39103

Stand-Off Post 6-32 x 32 mm 39107

Stand-Off Post 6-32 x 16 mm 41253

38 Building and Coding the PRIZM TaskBot


Motion Elements

Gear 40-Tooth 39028

Gear 80-Tooth 39086

Gear 120-Tooth 39085

Axle Hub 39172

Motor Hub 39079

Axle Set Collar 39092

100 mm Axles 39088

Bronze Bushing 39091

Gear Hub Spacer 39090

Axle Spacer 1/8" 39100

Axle Spacer 3/8" 39101

Building and Coding the PRIZM TaskBot 39


Motion Elements

Motor Mount 39089

DC Gear Motor 39530

4" Wheel 39055

4" Omni Wheel Pack/Assembly 36466

Kep Nut 39094

Socket Head Cap Screw 6-32 x 1/2" 39097

Socket Head Cap Screw 6-32 x 5/16" 39098

Button Head Cap Screw 3/8" 39111

40 Building and Coding the PRIZM TaskBot


Motion Elements

Single Standard-Scale Servo Motor Bracket 39060

Standard-Scale Pivot Arm with Bearing 39593

180 Standard-Scale HS-485HB Servo Motor 39197

Servo Extension 39081

Servo Y Connector 39082

Standard Servo Mounting Kit 41789

Adjustable Servo Bracket 39280

Building and Coding the PRIZM TaskBot 41


Power, Tools, and Accessories Elements

Battery Clip 38009

12-volt 3,000 mAh Battery 39057

12-volt Battery Charger 39830

2-in-1 Screwdriver 42991

4-in-1 Screwdriver 36404

Wrench Set 38001

Hex Key Pack 39104

Miniature Ball-Point Hex Driver 40341

Zip Tie 31902

42 Building and Coding the PRIZM TaskBot


Control Elements

PRIZM Controller 43000

TETRIX Motor Cable with Powerpoles 41352

PRIZM Controller On/Off Battery Switch Adapter 43169

3 foot Type A-B USB Cable 40967

Line Finder Sensor Pack 43056

Ultrasonic Sensor Pack 43055

Building and Coding the PRIZM TaskBot 43


PRIZM Controller Wiring Illustrated

Detail

44 Building and Coding the PRIZM TaskBot


TETRIX MAX Builders Guide Setup/Construction Tips
Planning for easy access to fasteners will make the building experience better for everyone involved.
As a general rule, when you create any substructure, its a good idea to only snug nuts and screws until you are sure all
elements fit together. Then, go back and tighten everything before moving on to the next step.

1. Channel Placement
A little planning and forethought about how structural elements go together can make your building experience easier,
quicker, and more efficient.

In the figure above, both structures have similar building surfaces to work from but have different accessibility for
fastening the Kep Nuts on the screws. Easy access is preferred. Avoid difficult access if possible.

Building and Coding the PRIZM TaskBot 45


2. Tool Use
Proper use of the basic tools makes the building process smoother and more enjoyable and saves time.

46 Building and Coding the PRIZM TaskBot


3. Take Advantage of Design Features
Some elements have design features that either make the element function better or fill a specific role. Recognizing those
so you can take full advantage will make your build be stronger, last longer, and function better.

This is the proper orientation for Kep Nuts. The self-locking feature of the Kep Nut should always be against the flat surface
of the structural element, such as in the example on the left. The image on the right is wrong!

Below is a view of both together for comparison.

The orientation of the Kep Nut on the left is correct.


The orientation of the Kep Nut on the right is wrong!

Using the right-length screw for the job will make best
use of your available resources. Use the shortest screw
possible to get the job done, and save the longer screws
for the places where they are necessary.

While either screw in the image above would


work, the best use of resources would dictate
using the one shown on the right.

Building and Coding the PRIZM TaskBot 47


Axle Hubs and Motor Hubs are often confused because they look alike and they serve the same function but in slightly
different applications.

The difference between the two elements is the size of the inside diameter of the hole in the center of the element. The
Axle Hub is sized for the axle, while the Motor Hub is sized for the motor shaft.

While Axle Hubs can be used only on axles because of size, the Motor Hub could be incorrectly used on an axle and cause
problems with function and attachment of wheels or gears. The image on the left shows an axle in the center of a Motor
Hub. Notice the difference in size between the outer diameter of the axle and the inside diameter of the Motor Hub. The
image on the right shows an axle in the center of the Axle Hub. Notice the elements are sized correctly to fit together and
function properly.

48 Building and Coding the PRIZM TaskBot


4. Assembly of Specialty Parts

Omni Wheel Setup Parts Needed

2x 1x 2x 4x
4" Omni Wheel Pack/ Omni Wheel Bronze Bushings Button Head Cap
Assembly 36466 Spacer 36466 39091 Screw 3/8" 39111

Step 1.0

Step 1.1 (front view) Step 1.1 (back view)

Building and Coding the PRIZM TaskBot 49


Step 1.2 Step 1.3

Tip: To get proper offset of rollers


between the two wheels, remove
screws from Step 1.3 and rotate the
wheel shown in either direction.
Reattach screws.

50 Building and Coding the PRIZM TaskBot


Activity 6: Build the TaskBot
Teacher note: Many factors
We need a robot. Because the focus of this guide is on working with PRIZM and the
can affect building time,
Arduino Software (IDE), we do not need a complicated robot. With that in mind, we
including such things as set
created the PRIZM TaskBot. The TaskBot is meant to be simple, easy to build, and
organization and whether
exactly what we need for the purposes of this guide without any unnecessary parts.
the builder has a partner.
That being said, everything you learn with this basic bot can be transferred to a
The above time is only an
more complicated bot as you continue on your robotics journey.
estimate and is based on
The TaskBot uses two 12-volt motors mounted back to back for a good example of a an individual builder of
differential-drive robot. Two omni wheel assemblies on the opposite end combined average experience who is
with the maneuverable drive make a perfect test vehicle for our work with PRIZM. comfortable with hands-on
We will start with a basic drive frame and add more to it in later activities. building activities and has
access to complete, well-
This build should be perfect for students who have little to no experience with organized sets. Actual time
metal-based building systems. might vary.
Building Time Expectations
45-55 minutes

Real-World Connection
The construction of a machine will determine its effectiveness as well as its
durability. Engineers design machines (including robots) for specific purposes. For
instance, a bulldozer is built from very strong and durable materials to be able to
withstand the forces involved in pushing over trees and buildings or digging out
large holes in the ground.
STEM Extensions
Science
Structure and function
Technology
Materials
Fasteners
Engineering
Machine design
Math
Measurement
Modulus

Building and Coding the PRIZM TaskBot 51


Step 1
Parts Needed

8x
Socket Head Cap Screw
6-32 x 5/16" 39098

4x
288 mm Channel 39068 8x
Kep Nut 39094

Tip: See page 36 for help with identifying Channel elements. Remember, we identify by length.

Partial assembly should look like this.

52 Building and Coding the PRIZM TaskBot


Step 1.0

Step 1.1

Tip: Its a good idea to only snug the


nuts and screws until all four Channels
are connected.

Building and Coding the PRIZM TaskBot 53


Step 1.2

Step 1.3

54 Building and Coding the PRIZM TaskBot


Rotate build to match this view.

Step 1.4

Building and Coding the PRIZM TaskBot 55


Step 1.5

Step 1.6

Tip: After all four Channels are connected and


the square frame is created, dont forget to go
back and tighten all the screws and nuts.

56 Building and Coding the PRIZM TaskBot


Step 2
Parts Needed

8x
Kep Nut
2x 39094
L Bracket 39062

1x
4x 4x PRIZM Controller On/Off Battery
Socket Head Cap Screw Socket Head Cap Screw Switch Adapter 43169
6-32 x 1/2" 39097 6-32 x 5/16" 39098

Partial assembly should look like this.

Building and Coding the PRIZM TaskBot 57


Rotate build to match this view.

Step 2.0

58 Building and Coding the PRIZM TaskBot


Step 2.1

Rotate build to match this view.

Building and Coding the PRIZM TaskBot 59


Step 2.2 Tip: This step uses Socket Head Cap
Screws (39097).

Tip: It does not matter in which direction the On/Off Power Switch is mounted in the
plate. It is strictly a matter of personal preference.

Step 2.3

60 Building and Coding the PRIZM TaskBot


Step 3
Parts Needed

2x 2x 4x
32 mm Channel 100 mm Axle Axle Set Collar
39065 39088 39092

2x
4" Omni Wheel Pack/Assembly
36466

4x 6x 6x 2x
Bronze Bushing 39091 Socket Head Cap Screw Kep Nut 39094 Axle Spacer 1/8" 39100
6-32 x 5/16" 39098

Partial assembly should look like this.

Building and Coding the PRIZM TaskBot 61


Step 3.0

Step 3.1

62 Building and Coding the PRIZM TaskBot


Step 3.2

Step 3.3

Building and Coding the PRIZM TaskBot 63


Step 3.4

Step 3.5

Tip: Refer to Omni Wheel Setup


on pages 49-50 to see how to
assemble the Omni Wheel.

64 Building and Coding the PRIZM TaskBot


Step 3.6

Step 3.7

Building and Coding the PRIZM TaskBot 65


Step 3.8

66 Building and Coding the PRIZM TaskBot


Step 4
Parts Needed

2x 2x 2x
4" Wheel 39055 Motor Mount 39089 DC Gear Motor 39530

2x 8x 4x
Motor Hub 39079 Socket Head Cap Screw Kep Nut 39094
6-32 x 1/2" 39097

Finished assembly should look like this.

Tip: See the setup tips on page 48 for help distinguishing between the Motor Hub and Axle Hub.

Building and Coding the PRIZM TaskBot 67


Step 4.0

Tip: The screw over the split end of the motor


mount holds the motor in position inside the
mount. To insert the motor and move it into
position, you must keep the screw loose. Dont
forget to go back and tighten this screw after
the motor has been inserted and moved into the
desired position.

Step 4.1

68 Building and Coding the PRIZM TaskBot


Step 4.2

Step 4.3

Tip: This step uses the


Motor Hub (39079).
Dont forget to tighten
the set screw.

Building and Coding the PRIZM TaskBot 69


Step 4.4

Tip: The screw over the split end of the motor


mount holds the motor in position inside the
mount. To insert the motor and move it into
position, you must keep the screw loose. Dont
forget to go back and tighten this screw after
the motor has been inserted and moved into the
desired position.
Step 4.5

70 Building and Coding the PRIZM TaskBot


Step 4.6

Step 4.7

Building and Coding the PRIZM TaskBot 71


Step 4.8

Tip: This step uses the Motor


Hub (39079). Dont forget to
tighten the set screw.

Step 4.9

72 Building and Coding the PRIZM TaskBot


Step 5
Parts Needed

1x 2x
12-volt 3,000 mAh Battery 39057 Flat Building Plate 39073

2x
Battery Clip 38009

1x 8x 16x
Flat 160 mm x 16 mm Stand-Off Post 6-32 x 1" Socket Head Cap Screw
39272 39102 6-32 x 5/16" 39098

Finished assembly should look like this.

Building and Coding the PRIZM TaskBot 73


Step 5.0
Tip: This step uses 6-32 x 1"
Stand-Off Posts (39102).

Step 5.1

74 Building and Coding the PRIZM TaskBot


Step 5.2
Tip: This step uses 6-32 x 1"
Stand-Off Posts (39102).

Step 5.3

Building and Coding the PRIZM TaskBot 75


Step 5.4

Step 5.5

76 Building and Coding the PRIZM TaskBot


Step 6
Parts Needed

8x 8x
Socket Head Cap Screw Kep Nut 39094
6-32 x 5/16" 39098

Finished assembly should look like this.

Building and Coding the PRIZM TaskBot 77


Step 6.0

Step 6.1

78 Building and Coding the PRIZM TaskBot


Step 6.2

Step 6.3

Building and Coding the PRIZM TaskBot 79


Step 7
Parts Needed

1x 4x 4x
PRIZM controller 43000 Socket Head Cap Screw Kep Nut 39094
6-32 x 1/2" 39097

Finished assembly should look like this.

80 Building and Coding the PRIZM TaskBot


Step 7.0

Step 7.1

Building and Coding the PRIZM TaskBot 81


Step 7.2

Step 7.3

82 Building and Coding the PRIZM TaskBot


Finished assembly should look like this.

Building and Coding the PRIZM TaskBot 83


Activity 7: Drive Forward
Activity 7 is the first activity with the TaskBot. We want to start with something
simple. In this activity we will create a sketch to move the TaskBot forward for three
seconds, stop, and end the program.
Building on what we learned in Activity 2, we will add a second motor and have
them work together in unison. Being able to use two motors together in unison is a
fundamental requirement for mobile robots.

Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_
Act7_Drive_Forward. A new sketch window will open titled TaskBot_Act7_Drive_
Forward (Figure 20).

Figure 20

84 Building and Coding the PRIZM TaskBot


Executing the Code
Tip: Remember that you
Before we can upload the sketch to the PRIZM, remember to check our connections. can print and use the TETRIX
Upload the sketch. The green LED will light up, indicating the code is ready to PRIZM Arduino Library
execute. When the LED comes on, disconnect the USB cable and set the TaskBot on Functions Cheat Sheet within
the floor. the appendix on page 153 as
a quick reference for all the
Press the green Start button to execute the sketch. Observe the direction and functions available to you.
duration of the TaskBots motion. Based on the sketch comments, did the behavior
match expectations?
The TaskBot should stop after three seconds.
Tip: Want to see this in
Moving Forward action? You can by watching
This sketch introduces three new functions, prizm.setMotorInvert(), our RoboBench video series
prizm.setMotorPowers(), and prizm.PrizmEnd(). for the PRIZM Programming
Guide. You can find the
The prizm.setMotorInvert() enables you to invert the rotational direction of a motor. entire series at video.
When two motors are mounted on opposite sides, this function enables you to give tetrixrobotics.com or on
a single direction command to both motors and have them work together. This the TETRIXrobotics YouTube
makes your job as a programmer easier. There are two parameters to the function. channel.
The first parameter designates the motor channel, and the second parameter
designates no invert or invert (0 or 1).
The prizm.setMotorPowers() enables you to set the power level of motor 1 and
motor 2 at the same time. The two parameters set the speed for each motor. In this
sketch the motor power for each motor is 50%.
The prizm.PrizmEnd() ends or terminates the sketch.
In this simple sketch all these functions work together so the robot can move
forward for three seconds and then stop. Because we want the motors to always
work together, prizm.setMotorInvert needs to be used only in the setup. The
prizm.setMotorPowers tells both motors to move at 50% power with a single
function. To finish the sketch we use prizm.PrizmEnd() instead of having it loop.
For more detailed information about the sketch process and the PRIZM library
functions involved in using the DC motors, refer to the appendix: TETRIX PRIZM
Arduino Library Functions:
Pages 137-142 about DC motor functions
Real-World Connection
Trains are a very good example of machines that drive forward. Their drive motors
(usually diesel powered) do not have to be able to rotate independently to turn
corners they just have to provide forward thrust to get (and keep) the train going.
Occasionally they have to go backward but they just provide reverse thrust in that
instance.

Building and Coding the PRIZM TaskBot 85


STEM Extensions
Tip: Without the use of
Science encoders, speeds of DC
Rotational kinetic energy motors using the power
Rotational torque commands can vary
depending on the charge
Technology
level of the battery.
Direct drive vs geared drive
DC motor control functions
Engineering Tip: In the sketch window
Differential, skid, and tricycle steering mechanisms the PRIZM library functions
change color when they
Math
are typed or spelled
Clockwise, counterclockwise rotation correctly. Consequently,
Wheel circumference vs distance traveled per rotation if spelled incorrectly they
will not change. In Arduino,
Hacking the Code Activity the PRIZM functions are
recognized by the software
With the example as a reference, try creating a new sketch to move the TaskBot as keywords and will turn
forward and stop. Remember what we learned from our previous activities and orange when the syntax is
experiment with trying to make the TaskBot move forward for an amount of time correct.
and then reverse to the same spot.
We have the fundamentals to explore speed because we can measure distance over
time. In our sketch, when we use a specified time, we can physically measure how Tip: An example library of
far the robot moves. When we change our power parameter, it should affect the code to help you get started
distance traveled in the same time frame. with this challenge can be
found in the appendix. If you
With that in mind, create a challenge by marking a specified distance on the floor. have a digital copy of this
Using the data you have collected, program the TaskBot to get as close to the guide, you can simply copy
specified distance as possible without going over. and paste the sample code
for each activity into your
sketch window. A digital
download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

86 Building and Coding the PRIZM TaskBot


Activity 8: Drive in a Circle
For our eighth activity we will apply our knowledge of motors to create a new
behavior. While being able to move straight is important, we need to be able to
expand on that and make turns. This activity will have your TaskBot driving in circles
by varying motor power as the motors work in unison.

Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act8_
Drive_Circle. A new sketch window will open titled TaskBot_Act8_Drive_Circle
(Figure 21).

Figure 21

Building and Coding the PRIZM TaskBot 87


Before we can upload the sketch to the PRIZM, remember to check our connections.
Tip: You will need about
Upload the sketch. The green LED will light up indicating the code is ready to five to six feet of empty
execute. When the LED comes on, disconnect the USB cable and set the TaskBot on floor space for the robot to
the floor. complete the circle.
Press the green Start button to execute the sketch. Observe the direction and
duration of the TaskBots motion.
Press the red Reset button to end the sketch. Based on the sketch comments, did
the behavior match expectations?
Moving Forward
While this sketch does not add any new functions, it should lead to a deeper
understanding of how motors work in unison to create a specified behavior.
All the functions in this sketch work together to make the TaskBot move in a circle.
Because we want the motors to always work together, prizm.setMotorInvert needs Tip: Remember that you
to be used only in the setup. The prizm.setMotorPowers tells both motors to move can print and use the TETRIX
at different speeds in a single function. One motor is set to 50%, while the other is PRIZM Arduino Library
set to 25%, resulting in a circular motion. Functions Cheat Sheet within
the appendix on page 153 as
For more detailed information about the sketch process and the PRIZM library
a quick reference for all the
functions involved in using the DC motors, refer to the appendix: TETRIX PRIZM
functions available to you.
Arduino Library Functions:
Pages 137-142 about DC motor functions
Real-World Connection
Tip: Want to see this in
If you have ever seen farmland that is watered by sprinkler irrigation systems, you action? You can by watching
have likely seen an example of a machine that goes around in a circle. The typical our RoboBench video series
sprinkler irrigation system has a center pivot point where the water source is for the PRIZM Programming
connected, and the beam of the pipe system transports water to sprinkler outlets Guide. You can find the
down the length of the beam. The beam is supported by wheels every 30 or 40 feet, entire series at video.
and the wheels are powered by electric motors. Because the center pivot of the tetrixrobotics.com or on
system remains in place, as the wheels rotate, they move the entire beam in a circle, the TETRIXrobotics YouTube
irrigating the farmland as the system goes. channel.

88 Building and Coding the PRIZM TaskBot


STEM Extensions
Tip: In the sketch window
Science the PRIZM library functions
Circular motion change color when they
Tangential motion are typed or spelled
correctly. Consequently,
Technology
if spelled incorrectly they
Motor control and precision will not change. In Arduino,
Problem solving via coding the PRIZM functions are
Engineering recognized by the software
as keywords and will turn
Machine design for following geometric paths orange when the syntax is
Math correct.
Technical (mathematical) definition of a circle
Determining radius
Tip: An example library of
Hacking the Code Activity code to help you get started
with this challenge can be
With the example as a reference, try creating a new sketch to move the TaskBot in found in the appendix. If you
a different-size circle. Remember what we learned from our previous activities and have a digital copy of this
experiment with different parameters to make the circle diameter larger or smaller. guide, you can simply copy
and paste the sample code
Challenge yourself to add behaviors. What would it take to make your robot drive in
for each activity into your
an oval or a figure eight?
sketch window. A digital
download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

Building and Coding the PRIZM TaskBot 89


Activity 9: Drive in a Square
Now, we will continue to build our navigational skills with the TaskBot by giving our
robot the ability to make 90-degree turns. The ability to make 90-degree turns will
be used to make the TaskBot drive in a square.

Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act9_
Drive_Square_1. A new sketch window will open titled TaskBot_Act9_Drive_
Square_1 (Figure 22).

Figure 22

90 Building and Coding the PRIZM TaskBot


Executing the Code
Tip: This program is one that
Before we can upload the sketch to the PRIZM, remember to check our connections. could be affected by the
Upload the sketch. The green LED will light up, indicating the code is ready to charge level of the battery.
execute. When this has occurred, disconnect the USB cable and set the TaskBot on Why? The pattern of this
the floor. program is generated by
a series of delay functions,
Press the green Start button to execute the sketch. Observe the direction and and as motor rpm changes
duration of the TaskBots motion. Based on the sketch comments, did the behavior due to battery levels, the
match expectations? square pattern can change
Moving Forward somewhat as batteries
drain. That is one of the
This sketch has a lot to it, but it is made of functions that we have used before. We consequences of using dead
have just combined multiple sequential behaviors to create one larger behavior in reckoning as a method of
this case, a square. control.
An important thing to understand about this program is that we are using dead
reckoning to program the robots square driving path. Dead reckoning is simply
using time as the basis for controlling a motor. For instance, to make a right-hand
turn, we are commanding the motors to turn on in opposite directions at 50% Tip: Remember that you
power and run for 600 milliseconds. can print and use the TETRIX
PRIZM Arduino Library
We estimate that if we spin the motors at a certain rpm for a certain time period, Functions Cheat Sheet within
we should come close to making a 90-degree right turn. However, this is not always the appendix on page 153 as
accurate because the amount that our robots battery is charged can vary and any a quick reference for all the
wheel slippage on the surface we are working on will cause errors. In essence we functions available to you.
are dead reckoning on making a right turn.
All the functions in this sketch work together to make the TaskBot move in a square.
Because we want the motors to always work together, prizm.setMotorInvert needs
to be used only in the setup. The prizm.setMotorPowers tells both motors to move Tip: Want to see this in
at different speeds in a single function. action? You can by watching
our RoboBench video series
For more detailed information about the sketch process and the PRIZM library for the PRIZM Programming
functions involved in using the DC motors, refer to the appendix: TETRIX PRIZM Guide. You can find the
Arduino Library Functions: entire series at video.
tetrixrobotics.com or on
Pages 137-142 about DC motor functions
the TETRIXrobotics YouTube
Real-World Connection channel.
Some vehicles take the same route time after time. For instance, a vehicle for
transporting packages from three different locations to a final destination within
a warehouse could have a route that would look like a square. This vehicle is
programmed to go an exact distance forward before it turns to go that same
distance at a 90-degree angle. After repeating this four times, the vehicle is back
where it started, ready for another cycle of package transport.

Building and Coding the PRIZM TaskBot 91


STEM Extensions
Tip: In the sketch window
Science the PRIZM library functions
Relationship among time, distance, and velocity change color when they
Technology are typed or spelled
correctly. Consequently,
How time is measured within a microprocessor
if spelled incorrectly they
Measuring turning radius will not change. In Arduino,
Engineering the PRIZM functions are
Program design for path following recognized by the software
as keywords and will turn
Math orange when the syntax is
Relationship of sides and angles of a square correct.
Measurement of angles

Hacking the Code Activity Tip: An example library of


code to help you get started
With the example as a reference, try creating a new sketch to move the TaskBot in
with this challenge can be
a square. Remember what we learned from our previous activities and experiment
found in the appendix. If you
with different parameters to make the square larger or smaller.
have a digital copy of this
Challenge yourself to create complex paths beyond a square. Try tracing out some guide, you can simply copy
of the alphabet or navigate through a simple maze using dead reckoning. and paste the sample code
for each activity into your
sketch window. A digital
download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

Note: Keep this activity in


mind. We will revisit this
behavior in the next activity
in order to flex our coding
muscles.

92 Building and Coding the PRIZM TaskBot


Activity 10: Simplify the Square
Remember the square activity we did in Activity 9? For our next activity we want to
show you a more efficient way to code the same behavior.

Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act10_
Drive_Square_2. A new sketch window will open titled TaskBot_Act10_Drive_
Square_2 (Figure 23).

Figure 23

Building and Coding the PRIZM TaskBot 93


Executing the Code
Tip: A function can be
Before we can upload the sketch to the PRIZM, remember to check our connections.
defined in one area of your
Upload the sketch. The green LED will light up, indicating the code is ready to sketch and then referenced,
execute. When this has occurred, disconnect the USB cable and set the TaskBot on or called, in another area.
the floor. This creates subroutines
that help keep your sketch
Press the green Start button to execute the sketch. Observe the direction and concise.
duration of the TaskBots motion. Based on the sketch comments, did the behavior
match expectations?
Moving Forward
This sketch implements one new program structure in the form of the for
statement, and it adds a new way to implement functions with called functions.
Both of these will combine to make a simpler, easier-to-read sketch.
The for statement is used to repeat a block of code enclosed in curly braces. An
increment counter is used to count the iterations and terminate the loop. The for
statement is useful for limiting the number of times a loop will execute.
Called functions occur outside the setup() and loop() functions. They are used most
often when the same action needs to occur multiple times in a sketch.
When we made the TaskBot drive in a square in Activity 9 we listed each action
line by line, resulting in excessive code that looked more complicated than it was.
We can identify specific actions that were repeated several times in the complete
behavior. We can define those identified actions in a called function outside the
loop(). After the called functions are defined outside the loop(), we can use them in
the loop() to simplify the code.
For this sketch we defined a forward() and a rightTurn() outside the loop(). Because
we defined these two functions, we can now call them in the loop(). We start the
loop() with the for statement to define how many times we want the loop to
repeat.
There are three parts to the for statement. The first part, int x=0, is the
initialization. This will happen first and only once.
The second part, x<=3, is the condition. Each time through the loop(), the condition
is tested. If it is true, the third part, x++, the increment, is executed, and the
condition is tested again. When that condition becomes false, the loop() ends. Tip: Remember that you
can print and use the TETRIX
In simple terms, the initialization starts the loop(). The condition defines how many PRIZM Arduino Library
times we want the loop to execute, which is three times plus the first initialization. Functions Cheat Sheet within
The total times the loop() will execute is four. And the increment defines how it the appendix on page 153 as
counts each execution of the loop(), which is by one increment. a quick reference for all the
For more detailed information about the sketch process and the PRIZM library functions available to you.
functions involved in using the DC motors, refer to the appendix: TETRIX PRIZM
Arduino Library Functions:
Pages 137-142 about DC motor functions Tip: Want to see this in
Real-World Connection action? You can by watching
our RoboBench video series
In the process of learning to shoot free throws, you try to train your mind and for the PRIZM Programming
muscles to repeat a certain set of controlled actions that will cause the basketball Guide. You can find the
to go through the hoop. Repeatability, or the ability to consistently repeat an entire series at video.
operation with little or no variation in the results, is critical in many sports but is also tetrixrobotics.com or on
critical in robot design and coding. Determining a method (or code) to have a robot the TETRIXrobotics YouTube
precisely follow a square pattern provides evidence that the robot can perform a channel.
repetitive action with little or no variation.

94 Building and Coding the PRIZM TaskBot


STEM Extensions
Tip: In the sketch window
Science the PRIZM library functions
Relationship between time, distance, and velocity change color when they
Technology are typed or spelled
correctly. Consequently,
How time is measured within a microprocessor
if spelled incorrectly they
Measuring turning radius will not change. In Arduino,
Engineering the PRIZM functions are
Program design for path following recognized by the software
as keywords and will turn
Math orange when the syntax is
Relationship of sides and angles of a square correct.
Measurement of angles

Hacking the Code Activity Tip: An example library of


code to help you get started
With the example as a reference, try creating a new sketch to move the TaskBot
with this challenge can be
in a square using the for statement and called functions. Or for an additional
found in the appendix. If you
challenge, create complex paths beyond a square such as rectangles or hexagons.
have a digital copy of this
Remember what we learned from our previous activities. The challenge from guide, you can simply copy
here is to move forward with your own unique robot builds and apply the coding and paste the sample code
knowledge you have learned in new and exciting ways. for each activity into your
sketch window. A digital
download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

Building and Coding the PRIZM TaskBot 95


Building Interlude: Make the TaskBot Smart!
Up until now our robot has been good at following instructions, but it has not been
able to make any decisions. We know from the getting started activities that sensors
enable robots to sense the environment around them. We can give robots the
ability to make decisions based on the information that sensors provide, thereby
appearing smart.
In the next coding examples, we will attach sensors to the PRIZM TaskBot. We will
work though two examples using the Line Finder Sensor and two examples using
the Ultrasonic Sensor. Instructions for attaching sensors to the PRIZM TaskBot are
shown below.

Step 1
Parts Needed

1x
Flat Bracket 39061

1x
Line Finder Sensor Pack 43056
1x
Ultrasonic Sensor Pack 43055
1x
L Bracket 39062

4x 6x 2x 2x
Stand-Off Post Socket Head Cap Screw Socket Head Cap Screw Kep Nut
6-32 x 32 mm 39107 6-32 x 1/2" 39097 6-32 x 5/16" 39098 39094

Finished assembly should look like this.

96 Building and Coding the PRIZM TaskBot


Step 1.0

Tip: This step uses 6-32 x 32 mm


Stand-Off Posts (39107).

Step 1.1

Tip: This step uses 6-32 x 32 mm


Stand-Off Posts (39107).

Building and Coding the PRIZM TaskBot 97


Step 1.2

Step 1.3

98 Building and Coding the PRIZM TaskBot


Step 1.4

Step 1.5

Building and Coding the PRIZM TaskBot 99


Step 2
Parts Needed

2x
Socket Head Cap Screw
6-32 x 5/16" 39098

Finished assembly should look like this.

100 Building and Coding the PRIZM TaskBot


Step 2.0

Building and Coding the PRIZM TaskBot 101


Activity 11: Drive to a Line and Stop
Starting simple like we have in all of our activities, we will begin by adding the Line
Finder Sensor to a basic move forward behavior. This will enable the TaskBot to stop
based on the environment around it. In this activity the TaskBot will drive forward
and stop at a line.

Parts Needed
Contrasting light and dark surface
Fully assembled PRIZM TaskBot
complete with sensor module
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act11_
Drive_To_Line. A new sketch window will open titled TaskBot_Act11_Drive_To_
Line (Figure 24).

Figure 24

102 Building and Coding the PRIZM TaskBot


Executing the Code
Troubleshooting: Be sure
Before we can upload the sketch to the PRIZM, remember to check our connections. to check that the Line
The Line Finder Sensor should be in digital sensor port 3. Finder Sensor is plugged
Upload the sketch. The green LED will light up, indicating the code is ready to into the correct sensor
execute. When this has occurred, disconnect the USB cable and set the TaskBot on port and that it is adjusted
the floor. properly to sense the line.
Some height adjustment
The TaskBot should be on a white or reflective surface pointed toward a black line might be needed, or the
or non-reflective surface approximately 36 inches or one meter away. small adjustment screw on
Press the green Start button to execute the sketch. Observe the behavior of the the back side of the sensor
robot. Press the red Reset button to end the sketch. Based on the sketch comments, module might need to
did the behavior match expectations? be tweaked. To see if the
sensor is working properly,
manually move the robot
back and forth over the black
line and white surface. The
red LED on the Line Finder
Sensor should be on when
the sensor is over the white
surface and off when it is
over the black line.

Tip: Remember that you


can print and use the TETRIX
PRIZM Arduino Library
Functions Cheat Sheet within
the appendix on page 153 as
Distance of 1 meter or a quick reference for all the
roughly 36 inches functions available to you.

Tip: Want to see this in


action? You can by watching
our RoboBench video series
for the PRIZM Programming
Guide. You can find the
entire series at video.
tetrixrobotics.com or on
the TETRIXrobotics YouTube
channel.

Building and Coding the PRIZM TaskBot 103


Moving Forward
Tip: In the sketch window
This sketch uses two if statements, a program structure we used in Activity 4, and the PRIZM library functions
it introduces a while loop, which is a new program structure. change color when they
A while loop looks at a test condition inside parentheses and will loop continually are typed or spelled
until the test condition inside the parentheses becomes false. correctly. Consequently,
if spelled incorrectly they
In this sketch the if statements are looking at the condition of the Line Finder will not change. In Arduino,
Sensor. The first if statement tells the TaskBot to move at 35% power when over the PRIZM functions are
a white or reflective surface. The second if statement contains two parts. The first recognized by the software
part tells the TaskBot to brake when over a black line or non-reflective surface. The as keywords and will turn
second part contains the while loop that locks the sketch into blinking the red LED orange when the syntax is
until the sketch is reset. correct.
For more detailed information about the sketch process and the PRIZM library
functions involved in using the DC motors, refer to www.arduino.cc and the
appendix: TETRIX PRIZM Arduino Library Functions: Tip: An example library of
Pages 134-135 about sensor port functions code to help you get started
with this challenge can be
Pages 137-142 about DC motor functions found in the appendix. If you
have a digital copy of this
Real-World Connection guide, you can simply copy
For people who drive cars, pulling up to an intersection with a crosswalk is a and paste the sample code
common occurrence. For the safety of any pedestrians, you need to stop your car at for each activity into your
or before that crosswalk. After some experience this becomes second nature to you sketch window. A digital
as a driver. However, robots do not learn in the same manner as humans, so they download can be found at
have to have automated devices and sensors to know when and where to stop. www.TETRIXrobotics.com/
PRIZMdownloads.
STEM Extensions
Science
Velocity formula
Constant acceleration in a straight line
Technology
While loops
Reading sensor values
Engineering
Applying problem-solving strategies
Math
Calculations for distance, time, and velocity
Logic statements (for example, if then)

Hacking the Code Activity


With the example as a reference, try creating a new sketch to have the TaskBot drive
to a black line and stop. Remember what we learned from our previous activities.
Challenge yourself to create different behaviors when the TaskBot meets a line such
as backing up or turning a different direction.

104 Building and Coding the PRIZM TaskBot


Activity 12: Follow a Line
For this activity we can take what we learned in the previous activity and apply it
in a slightly different way to create a new behavior. This will enable the TaskBot to
follow a line.

Parts Needed
Contrasting light and dark surface
Fully assembled PRIZM TaskBot complete
with sensor module
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Black line minimum of 2 inches wide

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act12_
Follow_A_Line. A new sketch window will open titled TaskBot_Act12_Follow_A_
Line (Figure 25).

Figure 25

Building and Coding the PRIZM TaskBot 105


Executing the Code
Troubleshooting: Be sure
Before we can upload the sketch to the PRIZM, remember to check our connections. to check that the Line
The Line Finder Sensor should be in digital sensor port 3. Finder Sensor is plugged
To run this code we will need a white or light-reflective surface on which to place into the correct sensor
a black stripe for the robot to traverse. White, glossy cardboard or several sheets of port and that it is adjusted
paper tiled together can work. properly to sense the line.
Some height adjustment
Our black stripe can be made using electrical tape stuck down to the cardboard might be needed, or the
or paper tiles. We can make a curvy path or just a straight line. There will be a small adjustment screw on
limitation on how sharp a curve our robot will be able to follow, so do not make the the back side of the sensor
curves too tight. module might need to
be tweaked. To see if the
Straight Curve S-curve sensor is working properly,
manually move the robot
back and forth over the black
line and white surface. The
red LED on the Line Finder
Line length of 1 Sensor should be on when
meter or roughly the sensor is over the white
36 inches surface and off when it is
over the black line.

Sensor Sensor Sensor

Upload the sketch. The green LED will light up, indicating the code is ready to
execute. When this has occurred, disconnect the USB cable and set the TaskBot on
the floor.
The TaskBot should be on a white or reflective surface with the Line Finder Sensor
slightly to the side of a line to follow. Press the green Start button to execute the
sketch. Observe the behavior of the robot.

Press the red Reset button to end the sketch. Based on the sketch comments, did
the behavior match expectations?

106 Building and Coding the PRIZM TaskBot


Moving Forward
Tip: Remember that you
This sketch uses two if statements, which we are becoming increasingly familiar can print and use the TETRIX
with. We have added a second action to execute based on the test condition for PRIZM Arduino Library
each if statement. Functions Cheat Sheet within
The first action of the if statements deals with the motors. The if statement tells the appendix on page 153 as
the TaskBot to turn by sending power to one motor and braking the other based a quick reference for all the
on the condition of the Line Finder Sensor. Each if statement is the opposite of the functions available to you.
other. This creates a progressive series of turns that the TaskBot uses to follow the
line.
The second action of the if statements deals with the red LED on the PRIZM. The Tip: Want to see this in
if statement tells the red LED to turn on or off based on the condition of the action? You can by watching
Line Finder Sensor. This on-and-off action is synced with the turning action of the our RoboBench video series
motors. for the PRIZM Programming
The actions in each if statement work together to make the TaskBot follow the line Guide. You can find the
while producing visual cues in the form of the blinking red LED. entire series at video.
tetrixrobotics.com or on
For more detailed information about the sketch process and the PRIZM library the TETRIXrobotics YouTube
functions involved in using the DC motors, refer to www.arduino.cc and the channel.
appendix: TETRIX PRIZM Arduino Library Functions:
Pages 134-135 about sensor port functions
Pages 137-142 about DC motor functions
Real-World Connection
German brand PUMA is taking a new approach to physical fitness with the launch
of its PUMA BeatBot. The BeatBot is a line-following robot that acts as a pacesetter
that a runner can race against. The runner is able to give the robot information
such as distance and pace through his or her smartphone. Then, its off to the track,
where the runner attempts to beat the BeatBot.
Tip: In the sketch window
STEM Extensions the PRIZM library functions
change color when they
Science are typed or spelled
Light reflection and absorption correctly. Consequently,
if spelled incorrectly they
Angle of incidence and reflection will not change. In Arduino,
Technology the PRIZM functions are
recognized by the software
Threshold values as keywords and will turn
orange when the syntax is
Black and white (analog) acting as on and off (digital) correct.
Engineering
Designing machines to work close to edges
Tip: An example library of
Math code to help you get started
Applying logic statements to solve problems with this challenge can be
found in the appendix. If you
Hacking the Code Activity have a digital copy of this
guide, you can simply copy
With the example as a reference, try creating a new sketch to have the TaskBot and paste the sample code
follow a black line. Remember what we learned from our previous activities. for each activity into your
Challenge yourself to make the TaskBot go faster and still follow the line accurately sketch window. A digital
or change the code to make the TaskBot follow the line in the opposite direction. download can be found at
www.TETRIXrobotics.com/
PRIZMdownloads.

Building and Coding the PRIZM TaskBot 107


Activity 13: Drive Toward a Wall and Stop
With this activity we will continue to build on what we have learned and apply it to
the Ultrasonic Sensor. This will enable the TaskBot to drive toward a wall and stop a
specified distance away.

Parts Needed
Fully assembled PRIZM TaskBot complete
with sensor module
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act13_
Drive_To_Wall. A new sketch window will open titled TaskBot_Act13_Drive_To_
Wall (Figure 26).

Figure 26

108 Building and Coding the PRIZM TaskBot


Executing the Code
Troubleshooting: Be sure
Before we can upload the sketch to the PRIZM, remember to check our connections. to check that the Ultrasonic
The Ultrasonic Sensor should be in digital sensor port 4. Sensor is plugged into the
Upload the sketch. The green LED will light up, indicating the code is ready to correct sensor port and
execute. When this has occurred, disconnect the USB cable and set the TaskBot on plugged in correctly. For this
the floor. sketch, the correct sensor
port is D4. Keep in mind that
The TaskBot should be pointed toward an object or wall at least 25 centimeters objects without adequate
away. Press the green Start button to execute the sketch. Observe the behavior surface size or that have
of the robot. What happens if the TaskBot or object is moved before the sketch is an irregular surface might
ended? not be detected or affect
Press the red Reset button to end the sketch. Based on the sketch comments, did the distance the sensor
the behavior match expectations? reads. To see if the sensor is
working properly, you can
always go back to Activity
5; load and run that sketch
to check the serial monitor
and verify the output from
Box or obstacle the sensor. If you need to
do this, remember to either
More than 25 physically change the sensor
centimeters port the Ultrasonic Sensor is
plugged into to match the
sketch example or change
the sketch example.

Moving Forward
Tip: Remember that you
This sketch uses an if/else statement, which is a new programming structure. can print and use the TETRIX
PRIZM Arduino Library
An if/else statement gives greater control over the flow of code than the basic if
Functions Cheat Sheet within
statement. It enables multiple tests to be grouped together so they can be run at
the appendix on page 153 as
the same time. In other words, the if part of the statement says if this condition
a quick reference for all the
is true, do this. The else part of the statement says if this condition is false, do this
functions available to you.
instead.
In this sketch the first part of the if/else statement tests the condition of the
Ultrasonic Sensor, and if the sensor is greater than 25 centimeters away from an
object, it sets the power for the motors at 50%. The second part of the if/else Tip: Want to see this in
statement uses the same test, but if it is less than 25 centimeters away from an action? You can by watching
object, it gives an alternate action and tells the motors to brake. our RoboBench video series
for the PRIZM Programming
For more detailed information about the sketch process and the PRIZM library Guide. You can find the
functions involved in using the DC motors, refer to www.arduino.cc and the entire series at video.
appendix: TETRIX PRIZM Arduino Library Functions: tetrixrobotics.com or on
the TETRIXrobotics YouTube
Pages 134-135 about sensor port functions
channel.
Pages 137-142 about DC motor functions

Building and Coding the PRIZM TaskBot 109


Real-World Connection
Tip: In the sketch window
In many newer cars, sensors are included to let drivers know when they are close the PRIZM library functions
to another car or a wall when they are pulling into a parking space. As the car gets change color when they
closer to the wall, it will beep at the driver to alert him or her to slow and stop are typed or spelled
before hitting the wall. correctly. Consequently,
STEM Extensions if spelled incorrectly they
will not change. In Arduino,
Science the PRIZM functions are
Speed of sound recognized by the software
as keywords and will turn
Composition of sound waves orange when the syntax is
correct.
Technology
Sensor calibration
Engineering Tip: An example library of
code to help you get started
Designing machines to detect obstacles with this challenge can be
Math found in the appendix. If you
have a digital copy of this
Distance computations based on the speed of sound guide, you can simply copy
and paste the sample code
Hacking the Code Activity for each activity into your
With the example as a reference, try creating a new sketch to have the TaskBot drive sketch window. A digital
toward a wall or object and stop. Remember what we learned from our previous download can be found at
activities. www.TETRIXrobotics.com/
PRIZMdownloads.
Challenge yourself by changing the direction, range, or speed. You can also test
what size and shape of object the Ultrasonic Sensor is more likely to detect and at
what range.

110 Building and Coding the PRIZM TaskBot


Activity 14: Avoiding Obstacles
For this activity we are going to expand on the if/else statement by adding actions.
This will enable the TaskBot to avoid obstacles in its path.

Parts Needed
Multiple objects to avoid
Fully assembled PRIZM TaskBot
complete with sensor module
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act14_
Avoid_Obstacle. A new sketch window will open titled TaskBot_Act14_Avoid_
Obstacle (Figure 27).

Figure 27

Building and Coding the PRIZM TaskBot 111


Executing the Code
Troubleshooting: Be sure
Before we can upload the sketch to the PRIZM, remember to check our connections. to check that the Ultrasonic
The Ultrasonic Sensor should be in digital sensor port 4. Sensor is plugged into the
Upload the sketch. The green LED will light up, indicating the code is ready to correct sensor port and
execute. When this has occurred, disconnect the USB cable and set the TaskBot on plugged in correctly. For this
the floor. sketch the correct sensor
port is D4. Keep in mind that
When running this code, set an obstacle in front of our robot at a distance well objects without adequate
outside the 25-centimeter detection range. Cardboard boxes work well for this surface size or that have
purpose. We do not want anything too heavy that could cause damage to our robot an irregular surface might
just in case our robot crashes into it. not be detected or affect
the distance the sensor
reads. To see if the sensor is
working properly, you can
always go back to Activity
Box or obstacle 5; load and run that sketch
to check the serial monitor
More than 25 and verify the output from
centimeters the sensor. If you need to
do this, remember to either
physically change the sensor
port the Ultrasonic Sensor is
More than 25 plugged into to match the
centimeters sketch example or change
the sketch example.

Box or obstacle

To begin, press the green Start button. Our robot will travel forward until it senses Tip: Remember that you
that it is within 25 centimeters of the object in its path. can print and use the TETRIX
When an object is detected, the robot will stop, back up, make a right turn, and PRIZM Arduino Library
continue. Observe the behavior of the robot. Functions Cheat Sheet within
the appendix on page 153 as
Press the red Reset button to end the sketch. Based on the sketch comments, did a quick reference for all the
the behavior match expectations? functions available to you.
Moving Forward
This sketch continues using the if/else statement from the previous activity, but we
add multiple actions to the statement. Tip: Want to see this in
action? You can by watching
Each part of the if/else statement here has multiple actions. In the if part of the our RoboBench video series
statement, the TaskBot is moving forward at 35% power with the green LED on for the PRIZM Programming
while the sensor watches for an object. In the else part of the statement when the Guide. You can find the
TaskBot detects an object, the green LED turns off and the red LED turns on, and entire series at video.
the TaskBot stops, backs up, and turns right. The loop then continues. tetrixrobotics.com or on
For more detailed information about the sketch process and the PRIZM library the TETRIXrobotics YouTube
functions involved in using the DC motors, refer to www.arduino.cc and the channel.
appendix: TETRIX PRIZM Arduino Library Functions:
Pages 134-135 about sensor port functions
Pages 137-142 about DC motor functions

112 Building and Coding the PRIZM TaskBot


Real-World Connection
Tip: In the sketch window
Cars are beginning to include sensors to detect the world around them as they the PRIZM library functions
move. Some cars now have automatic braking systems to begin the braking change color when they
process and slow the car down if they sense a situation in which a collision might are typed or spelled
occur. The electronics involved provide a quicker reaction time than is possible for correctly. Consequently,
a human. This extra amount of time can be very beneficial in minimizing the effects if spelled incorrectly they
of a collision. will not change. In Arduino,
STEM Extensions the PRIZM functions are
recognized by the software
Science as keywords and will turn
Physics of sound reflection from a flat surface orange when the syntax is
correct.
Physics of sound reflection from a round surface
Physics of sound reflection from a rough surface
Tip: An example library of
Technology code to help you get started
Interpreting data from the Ultrasonic Sensor with this challenge can be
found in the appendix. If you
Engineering have a digital copy of this
Designing machines to avoid obstacles guide, you can simply copy
and paste the sample code
Math for each activity into your
sketch window. A digital
Data graphing download can be found at
Data averaging www.TETRIXrobotics.com/
PRIZMdownloads.
Hacking the Code Activity
With the example as a reference, try creating a new sketch to have the TaskBot
avoid obstacles. Remember what we learned from our previous activities.
Challenge yourself and experiment with different objects or make an obstacle
course with multiple objects to detect. We can experiment and modify the example
code as needed to change the reaction and behavior of our robots obstacle
avoidance skills.

Building and Coding the PRIZM TaskBot 113


Building Interlude: Give the TaskBot Attitude!
Before we create more code, we are going to add a servo motor to the PRIZM TaskBot. This will bring everything together
that we used in the getting started activities. All the different kinds of motors and sensors will be used on the TaskBot to
give our robot some attitude.
We will use the servo to signal when an object has been detected in the robots path. The servo will raise a lever to signal
that an object has been detected. Follow the steps below to add a servo and lever arm to the PRIZM TaskBot.

Parts Needed

1x 1x 1x
Flat 160 mm x 16 mm Single Standard-Scale 180 Standard-Scale
39272 Servo Motor Bracket HS-485HB Servo Motor
39060 39197

1x 8x 8x 4x
Motor Hub 39079 Button Head Cap Screw Socket Head Cap Screw Kep Nut
3/8" 39111 6-32 x 5/16" 39098 39094

Finished assembly should look like this.

114 Building and Coding the PRIZM TaskBot


Step 1.0

Step 1.1

Building and Coding the PRIZM TaskBot 115


Step 1.2

Step 1.3

116 Building and Coding the PRIZM TaskBot


Step 1.4

Step 1.5

Building and Coding the PRIZM TaskBot 117


Step 1.6

Step 1.7

118 Building and Coding the PRIZM TaskBot


Finished assembly should look like this.

Building and Coding the PRIZM TaskBot 119


Activity 15: Combining the Sensors
This is a culmination of all the previous activities. The TaskBot will follow a line while
watching for obstacles. If an obstacle is detected, then the TaskBot will stop, raise its
arm, and wait for the obstacle to be moved before continuing.

Parts Needed
Contrasting light and dark surface
Obstacle
Fully assembled PRIZM TaskBot complete
with sensor module
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer

Opening the Sketch


Before we open our next example sketch, be sure to save any sketch you want to
reference later.
Open the sketch by selecting File > Examples > TETRIX_PRIZM > TaskBot_Act15_
Combining_Sensors. A new sketch window will open titled TaskBot_Act15_
Combining_Sensors (Figure 28).

Figure 28

120 Building and Coding the PRIZM TaskBot


Executing the Code
Troubleshooting the Line
Before we can upload the sketch to the PRIZM, remember to check our connections. Finder Sensor: Be sure to
The Line Finder Sensor will be in digital sensor port 3, and the Ultrasonic Sensor will check that the Line Finder
be in digital sensor port 4. Sensor is plugged into the
We will use the Line Finder Sensor for line following and the Ultrasonic Sensor for correct sensor port and
obstacle detection. This example sketch will have the robot follow the black line on that it is adjusted properly
a white surface and look for an object in its path. to sense the line. Some
height adjustment might
When the object is detected at a distance of less than 25 centimeters, the robot will be needed, or the small
stop to avoid crashing into it. The robot will raise its arm and wait until the object is adjustment screw on the
cleared and then continue. back side of the sensor
module might need to
Box or obstacle be tweaked. To see if the
More than 25 sensor is working properly,
centimeters manually move the robot
Sensor back and forth over the black
line and white surface. The
red LED on the Line Finder
Sensor should be on when
the sensor is over the white
surface and off when it is
over the black line.

Upload the sketch. The green LED will light up, indicating the code is ready to
execute. When this has occurred, disconnect the USB cable and set the TaskBot on
Troubleshooting the
the floor.
Ultrasonic Sensor: Be sure
The TaskBot should be on a white or reflective surface with the Line Finder Sensor to check that the Ultrasonic
slightly to the side of a line to follow. Sensor is plugged into the
correct sensor port and
plugged in correctly. For this
sketch, the correct sensor
port is D4. Keep in mind that
objects without adequate
surface size or that have
an irregular surface might
not be detected or affect
the distance the sensor
reads. To see if the sensor is
working properly, you can
always go back to Activity
5; load and run that sketch
to check the serial monitor
and verify the output from
the sensor. If you need to
When running this code, set an obstacle in front of our robot at a distance well do this, remember to either
outside the 25 centimeter detection range. Cardboard boxes work well for this physically change the sensor
purpose. We do not want anything too heavy that could cause damage to our robot port the Ultrasonic Sensor is
just in case our robot crashes into it. plugged into to match the
sketch example or change
Press the green Start button to execute the sketch. Observe the behavior of the the sketch example.
robot. You can also play around with moving your obstacles to see how your robot
reacts.
Press the red Reset button to end the sketch. Based on the sketch comments, did Tip: If PRIZM does not execute
the behavior match expectations? the sketch after uploading, try
cycling the robot power switch
off and on.

Building and Coding the PRIZM TaskBot 121


Moving Forward
Tip: Remember that you
This sketch uses an if/else statement and a while loop, which we have covered can print and use the TETRIX
before. This sketch is not about adding new elements. It is about combining the PRIZM Arduino Library
elements from the previous activities. Functions Cheat Sheet within
The if/else statement in this sketch does the same line-following behavior as in the appendix on page 153 as
Activity 6 with two if statements but in a more controlled way. The while loop a quick reference for all the
monitors the Ultrasonic Sensor and creates a behavior when an object is detected functions available to you.
within 25 centimeters. This new behavior stops the TaskBot and uses the servo to
raise the TaskBots arm until the object is cleared from the TaskBots path. When the
object is cleared, the TaskBot will lower its arm and continue the original behavior
of line following. Tip: Want to see this in
action? You can by watching
For more detailed information about the sketch process and the PRIZM library our RoboBench video series
functions involved in using the DC motors, refer to www.arduino.cc and the for the PRIZM Programming
appendix: TETRIX PRIZM Arduino Library Functions: Guide. You can find the
entire series at video.
Pages 134-135 about sensor port functions
tetrixrobotics.com or on
Pages 137-142 about DC motor functions the TETRIXrobotics YouTube
channel.
Pages 144-145 about servo motor functions
Real-World Connection
Electronic devices that are readily available these days (smartphones, health-
monitoring devices, and so forth) collect sensory data that we can use to determine
where we should go, what we should eat, how much we should exercise, when
Tip: In the sketch window
we should get up in the morning, and many more potential actions. Combining
the PRIZM library functions
all these sensor inputs and determining what should be done happens within
change color when they
the coding of these devices. Coding for robotics has similar applications its all a
are typed or spelled
matter of what you want your robot to do based on the sensors it has!
correctly. Consequently,
STEM Extensions if spelled incorrectly they
will not change. In Arduino,
Science the PRIZM functions are
Human senses recognized by the software
Human brain function based on sensory data as keywords and will turn
orange when the syntax is
Technology
correct.
Prioritizing sensor readings
Engineering
Integration of sensory data to solve problems Tip: An example library of
Math code to help you get started
with this challenge can be
Data distribution terms (mean, median, mode, range, cluster, outlier, skew)
found in the appendix. If you
have a digital copy of this
Hacking the Code Activity guide, you can simply copy
With the example as a reference, try creating a new sketch to have the TaskBot and paste the sample code
follow a black line and stop for obstacles. Remember what we learned from our for each activity into your
previous activities. sketch window. A digital
download can be found at
Challenge yourself to create a sketch in which the TaskBot follows a line to an www.TETRIXrobotics.com/
obstacle, goes around the object, and continues following the line instead of PRIZMdownloads.
waiting for the object to be cleared.

122 Building and Coding the PRIZM TaskBot


Build, Code, Test, Learn . . . Go!
You have built a robot, you have programmed PRIZM, and you have tested and
tweaked your design, so where do you go from here?
This guide was intended to teach the essentials of building and coding a TETRIX
MAX robot using the PRIZM controller and the Arduino Software (IDE). However, this
is only the beginning and there are plenty of additional resources available and
opportunities to build larger robots that can complete more complex tasks. Now
that you have learned the basics, you are limited only by your imagination. To help
you get started, we have listed some of our favorite resources.
Please be sure to check out the appendix of this guide, which includes a vast array
of technical documentation including detailed PRIZM technical specifications,
component overviews and pinout diagrams, descriptions, function charts, and a
cheat sheet for the PRIZM Arduino library functions. In the appendix you will also
find support for integrating additional control components such as motor encoders.
For more on the Arduino Software (IDE) and programming tips and tricks:
PRIZM is built on Arduino architecture and is thus usable with programming
languages that support Arduino. This enables PRIZM users to access the Arduino
online community, which has produced a sea of support materials to get beginners
coding quickly. Learn more at www.arduino.cc.
For more on sensor integration and support:
The PRIZM controller is designed with sensor ports that are compatible with
the Grove system of modular sensors. Currently, the PRIZM Arduino library has
integrated support for two of these sensors: the Line Finder and the Ultrasonic.
For users who would like to integrate additional Grove sensors into their robotic
builds, you can find Arduino example code for each at wiki.seeedstudio.com/wiki/
Category:Grove or wiki.seeedstudio.com/wiki/Grove_System.
To view the sample Arduino code, simply click the sensor that is of interest.
Please note: TETRIX-compatible mounting adapters for the larger family of Grove
sensors are sold separately and will be needed for securing sensors to the robot
structure. Integrated support for additional Grove sensors will be added to the
PRIZM Arduino library as further updates are released.
For more on the TETRIX building system:
The parts and pieces used in this guide are only a portion of the elements available
in the TETRIX MAX system. Visit www.TETRIXrobotics.com to learn more about the
additional structural, motion, and accessory components that can be added to your
collection; to view support videos; and to download resources.

Building and Coding the PRIZM TaskBot 123


TETRIX PRIZM Robotics Controller Technical Specifications
Microcontroller: ATmega328P with Arduino Optiboot bootloader installed
Memory: 32 KB flash programmable memory (ATmega328P)
Power: 9-18 volts DC
2 Powerpole connections; H-bridge PWM controlled; 10 amps continuous
DC motor ports: current each channel, 20-amp peak
Recommended motor: TETRIX 12-volt DC Motor (39530)
Constant power (-100% to 100%)
PID constant speed (-720 to 720 degrees per second)
PID constant speed to encoder target position and hold
DC motor control modes: PID constant speed to encoder degrees position and hold
Brake or Coast mode when stopping
DC motor current monitoring (all modes)
Recommended encoder: TETRIX MAX Motor Shaft Encoder Pack (38000)
2 quadrature, 5 volts DC, 50 mA max; Spec: 360 CPR, 1,440 PPR; ENC 1 and
Motor encoder ports: ENC 2
USB connector: USB Type B
USB driver: FTDI
Standard servo ports: 6 total: servo channels 1-6
Continuous rotation (CR) servo ports: 2 total: CR1 and CR2 channels
Total servo power limit: 6 volts DC, 6 amps max
Set servo speed (0% to 100%)
Servo control modes: Set servo position (0-180 degrees)
Set CR servo state (Spin CW/Spin CCW)
Battery voltage monitoring: 0-18 volts range
Each can be configured as digital input or output. D2 can be configured as a
Digital sensor ports (D2-D5): serial communications port.
3 analog sensor ports (A1-A3): Each can be configured as analog input or digital input/output ports.
100 Khz speed. This connection shares same I2C bus as internal DC motor
1 I2C port (I2C): and servo motor control chips. I2C addresses 0x01-0x06 reserved by the
PRIZM controller.
Additional TETRIX DC motor and servo control modules can be daisy-
Motor controller expansion port (EXP): chained to this port.
Powerpole type; additional port for daisy-chaining battery power to motor
Battery connection port: controllers added to the expansion port
1 green Start button (START) Programmable push button
1 red Stop/Reset button (RESET) Non-programmable push button
1 red LED: Programmable LED used as an indicator
1 green LED: Programmable LED used as an indicator
1 blue LED: Indicates the power is on when illuminated
2 yellow LEDs: Indicates serial data activity on the USB port
1 red and 1 green DC Motor LEDs: Indicates DC motor rotation and direction for each DC motor channel

124 Appendix
PRIZM Robotics Controller Functional Overview
The PRIZM controller is connected to a computer using a standard USB cable
connection. Power is supplied from an external TETRIX 12-Volt Rechargeable NiMH
Battery Pack. The controller has a dual high-current DC motor drive system, each
motor having quadrature encoder support for implementing precise PID DC motor
velocity and position control. In addition there are six standard control servo ports
and two continuous rotation servo ports. There are four digital sensor ports, three
analog sensor ports, and one I2C port for use with external sensors. Digital port
D2 can also be configured with Arduinos software serial library for serial data
communications. The digital sensor ports can be configured as input or output.
The analog sensor ports can be configured for analog input or digital output mode.
Also onboard are two LEDs one Red and one Green that can be used as visual
indicators.
The PRIZM controller functions are driven by an ATmega328P chip using the Optiboot
bootloader for uploading program code via a USB connection to the processor chip.
There are two additional processor chips used to control motor functions: a DC motor
control and encoder interface chip and a servo control chip. These chips communicate
with the main processor chip via I2C communication. Each motor control chip
contains firmware, which handles all of the complex DC motor, encoder, and servo
control functions, thereby freeing up the main processor for running program code.
The green Start button is used to begin running a program that has been uploaded
to the controller. The red Reset button is used to stop a running program. Pressing the
red Reset button resets the main processor chip and the DC and servo control chips
and initializes all stored memory values to 0.

Appendix 125
PRIZM Controller Component Overview
and Pinout Diagrams
PRIZM Sensor Ports
The PRIZM controller uses Arduino UNO-compatible pin assignments. The sensors that are
supported in the Arduino PRIZM coding library are set up automatically using the library
functions. Support for different types of sensors will be added as they become available.
However, the ports are all directly accessible using Arduino coding functions if you wish to
work with them. With the exception of the I2C port, all others can be configured as inputs or
outputs using the Arduino pinMode function. To learn more, visit the Language Reference
section at www.arduino.cc.

Figure 29: PRIZM Sensor Port (Pins are left to right: 1, 2, 3, 4.)

Table 1: I2C port pin assignments


Pin Function Arduino Software (IDE) pin assignment ( )
Note: The PRIZM I2C
Pin 1 Ground N/A port can be used only
Pin 2 +5 volts, 100 mA N/A in I2C mode. It may
Pin 3 SDA (I2C serial data) ADC4 input channel (A4) not be configured
for analog or digital
Pin 4 SCL (I2C serial clock) ADC5 input channel (A5)
mode.
Table 2: Analog Sensor Port (A1)
Pin Function Arduino Software (IDE) pin assignment ( )
Pin 1 Ground N/A
Pin 2 +5 volts, 100 mA N/A
Pin 3 No connect N/A
Pin 4 Analog input or digital input/output Analog input (A1) | digital I/O (15)

Table 3: Analog Sensor Port (A2)


Pin Function Arduino Software (IDE) pin assignment ( )
Pin 1 Ground N/A
Pin 2 +5 volts, 100 mA N/A
Pin 3 No connect N/A
Pin 4 Analog input or digital input/output Analog input (A2) | digital I/O (16)

Table 4 Analog Sensor Port (A3)


Pin Function Arduino Software (IDE) pin assignment ( )
Note: Analog Sensor
Pin 1 Ground N/A
Ports A1-A3 can also
Pin 2 +5 volts, 100 mA N/A be configured as
Pin 3 No connect N/A digital input/output.
Pin 4 Analog input or digital input/output Analog input (A3) | digital I/O (17)

126 Appendix
Table 5: Digital Sensor Port (D2)
Pin Function Arduino Software (IDE) pin assignment ( )
Note: Digital
Pin 1 Ground N/A Port D2 can also
Pin 2 +5 volts, 100 mA N/A be configured
Pin 3 Digital input/output Digital I/O (9) as a software-
implemented serial
Pin 4 Digital input/output Digital I/O (2)
port using the
Arduino Software (IDE)
Table 6: Digital Sensor Port (D3) Serial Library. Pins D9
and D2 can be set to
Pin Function Arduino Software (IDE) pin assignment ( ) RX/TX for serial port
Pin 1 Ground N/A communications.
Pin 2 +5 volts, 100 mA N/A
Pin 3 No connect N/A
Pin 4 Digital input/output Digital I/O (3)

Table 7: Digital Sensor Port (D4)


Pin Function Arduino Software (IDE) pin assignment ( )
Pin 1 Ground N/A
Pin 2 +5 volts, 100 mA N/A
Pin 3 No connect N/A
Pin 4 Digital input/output Digital I/O (4)

Table 8: Digital Sensor Port (D5)


Pin Function Arduino Software (IDE) pin assignment ( )
Pin 1 Ground N/A
Pin 2 +5 volts, 100 mA N/A
Pin 3 No connect N/A
Pin 4 Digital input/output Digital I/O (5)

Appendix 127
PRIZM Servo Ports
The PRIZM controller has six position-controlled servo ports labeled 1 through 6.
Each servo channel can power and control one standard hobby-type servo motor. Note: We recommend the
The TETRIX PRIZM library for the Arduino Software (IDE) does all of the complex work following servo motors,
of controlling the servo motors. which are available at www.
TETRIXrobotics.com: the
180-Degree Standard-Scale
HS-485HB Servo Motor
(39197), the Quarter-Scale
HS-785HB Winch Servo
Motor with Horn (39905),
and the Quarter-Scale
HS-755HB Servo Motor with
Horn (39904).

Figure 30: PRIZM Servo Motor Ports (Pins are left to right: 1, 2, 3.)
(Labeled as SERVOS 1-6)
Pin 1: Servo PWM signal. It is usually the yellow or white wire.
Pin 2: Servo power supply wire. It is the red wire. PRIZM supplies +6 volt power.
Pin 3: Servo ground wire. It is the black wire.
Reversing the servo connection polarity will not damage the PRIZM controller.
However, if not inserted properly, the servo motor will not function. The PRIZM
controller can supply a total of 6 amps of DC power at 6 volts DC to the servo ports.
The amount of current draw is limited by a thermal (self-resetting fuse).
PRIZM Continuous Rotation (CR) Servo Ports
The PRIZM controller has two additional specialized servo ports for connection
of two continuous rotation servo motors. Continuous rotation servo motors are
designed to rotate continuously either clockwise or counterclockwise and can
be used as smaller, lightweight DC gearbox motors. The CR1 and CR2 ports are
designed to be connected to these types of servo motors and can spin them
continuously in either direction controlled by the commands in the PRIZM library.

Figure 31: PRIZM Continuous Rotation (CR) Servo Motor Ports


(Pins are left to right: 1, 2, 3.) (Labeled as CR1 and CR2)
Pin 1: Servo PWM signal. It is usually the yellow or white wire.
Pin 2: Servo power supply wire. It is the red wire. PRIZM supplies +6 volt power.
Pin 3: Servo ground wire. It is the black wire.
Reversing the servo connection polarity will not damage the PRIZM controller.
However, if not inserted properly, the servo motor will not function. The PRIZM
controller can supply a total of 6 amps of DC power at 6 volts DC to all the servo
ports this includes both CR1 and CR2. The amount of current draw is limited by a
thermal (self-resetting fuse).

128 Appendix
PRIZM Encoder Ports
The PRIZM controller has two quadrature encoder inputs for precise control of
the DC Motor 1 and Motor 2 channels. When attached to the TETRIX DC Motors, Note: Encoders are not
encoders provide position and velocity feedback that is used in programming code included in any of the
to implement powerful and precise DC motor speed and position control. The ENC1 TETRIX MAX robotics sets. If
port provides encoder feedback for DC Motor Channel 1, while ENC2 provides you would like to add this
encoder feedback for DC Motor Channel 2. We have included code examples using element to your set, you
encoders and all PRIZM Arduino library functions associated with encoders. These can do so by purchasing the
code examples can be found in the File > Examples > TETRIX_PRIZM drop-down TETRIX MAX Motor Shaft
menu. Encoder Pack (38000) at
www.TETRIXrobotics.com.

Figure 32: PRIZM Quadrature Encoder Port


(Pins are left to right: 1, 2, 3, 4.) (Labeled as ENC1 and ENC2)

Pin 1: +5 volts DC power supply


Pin 2: Encoder count signal (A)
Pin 3: Encoder ground
Pin 4: Encoder count signal (B)
Although the connector socket is keyed to prevent reverse polarity, take care to
not force the connector into the socket in the opposite direction. Damage to the
encoder or PRIZM controller could occur if the connector is forcibly inserted in the
reverse polarity direction.

Appendix 129
PRIZM Expansion Port
Note: This port is not
The PRIZM controller has a RJ-45 modular jack, labeled EXP, connected to the compatible with LEGO
onboard I2C bus. This port can be used to connect additional TETRIX DC Motor MINDSTORMS-related
Controllers and servo controllers in a daisy-chain arrangement. Up to four devices. Do not plug in
additional DC and/or servo motor controller boxes of any combination can be LEGO MINDSTORMS sensors,
connected to the expansion port for added motor control channels. Each additional motors, the EV3 Brick, or any
motor control box will dynamically set its own I2C address depending on its other LEGO device to this
position in the daisy chain. The first box will use address 0x01, the second, 0x02, port.
the third, 0x03, and the fourth 0x04.
When using additional I2C sensors or devices plugged into either the expansion
port or the I2C sensor port, these devices may not use I2C addresses 0x01 through
0x06. These address locations are reserved by the PRIZM internal motor and servo
control processor chips and any expansion motor or servo controller daisy-chained
to PRIZM through the expansion port.

Figure 33: PRIZM Motor Controller Expansion Port


Offset tab RJ-45 modular jack

PRIZM USB Port


The PRIZM USB port is used for communication between PRIZM and a Windows,
Mac, or Linux computer. Its main use is for downloading program code to the
program memory processor. It can also be used to transfer data between PRIZM
and a computer using serial communications protocol. For example, the Arduino
Software (IDE) includes a built-in serial monitor display window used to display
data values received from the PRIZM controller. This feature can be used to display
sensor or encoder data or even other types of program data as needed.
Oftentimes, viewing data values can be very helpful when debugging program
code. The serial monitor can also be used to send information to the PRIZM
controller. An example of this would be to have the PRIZM controller moving
DC motors or servos in response to keyboard entries. Users with advanced
programming knowledge could create graphical interfaces using Java, Python, or
another coding platform for monitoring data or controlling functions.

Figure 34: PRIZM USB Programming and Communication Port

130 Appendix
PRIZM Reset Button
The red Reset button is used for two purposes. When pressed, it will terminate any
program code that is being executed and reset all sensor and encoder data values
to their initialized states. It effectively resets the entire system just as switching
power off and back on would do.

Figure 35: PRIZM Reset button. Press down to activate.

PRIZM Start Button


The green Start button is used to begin the execution of program code that is
downloaded to the PRIZM controller. When you are creating program code using
the Arduino Software (IDE), the PrizmBegin function executes all the instructions
necessary to begin running a program. Calling this function initializes necessary
parameters to their proper state and causes the program code to wait until the
green Start button is pressed before execution.

Figure 36: PRIZM Start button. Press down to activate.


PRIZM DC Motor Ports
The PRIZM controller has two DC motor connection ports labeled Motor 1 and
Motor 2. Each motor channel is used to control the speed and direction of TETRIX
DC Motors via software commands defined in the PRIZM Arduino Software (IDE)
Library. Each motor channel can provide 10 amps of continuous DC current at
12 volts. Each motor channel has a Red and Black connector, with Red being the
positive connection and Black the negative connection. The TETRIX DC Motor
Power Cables come with the mating colored connectors and should be plugged
Red to Red and Black to Black for proper operation.

Figure 37: PRIZM DC Motor Ports

Appendix 131
PRIZM Battery Connection Port
The PRIZM controller is powered by a TETRIX 12-Volt Rechargeable NiMH Battery
Pack. Included with the PRIZM controller is a power on/off switch assembly
designed to connect the battery pack connector to the PRIZM-style battery
connection port. The battery pack can be plugged into either the top or bottom
row of the port. The extra port is intended to be used to daisy-chain power
to additional motor controllers that can be optionally added to PRIZM via the
expansion port.

Figure 38: PRIZM Battery Power Inlet/Outlet Port

132 Appendix
TETRIX PRIZM Arduino Library Functions
Include Statements
The include statement is used to import the functionality of the TETRIX PRIZM Arduino Library into an Arduino sketch. The
PRIZM library is a collection of functions that simplifies programming the PRIZM controller. Think of a library as a container
filled with mini programs, each with its own distinct function name that we can use in code to carry out complex tasks. To
include a library in our Arduino sketch, use the #include statement at the beginning of our program code. For example, to
add the PRIZM library to an Arduino sketch, we would insert this statement at the top of our program.
#include <PRIZM.h>
This statement tells the Arduino compiler to include all the program code contained in the PRIZM library when we compile
our Arduino sketch.

Object Declarations
The object declaration is an important statement when using the PRIZM controller and the PRIZM library. In order to use
the functions contained in the PRIZM library, we must first declare a library object name, which is then inserted as a prefix
before each library function. We can do this by including the following statement just after the #include statement.
PRIZM myname;
The parameter myname is arbitrary it can be anything that we choose or that makes sense to us depending on the code
application that we are creating. For consistency in this programming guide, the parameter myname will be set to prizm,
meaning that the object declaration statement used in sketch code examples will look like this:
PRIZM prizm;
The statement above creates a library object named prizm used in code when calling a function from the PRIZM class in
which the object was created. In our code, whenever we want to call a PRIZM library function, we will need to prefix each
function with the defined object name. In the case of the statement above, that object would be prizm. All the library
functions below are prefixed by the parameter myname. Do not forget, when using these functions in our code, to change
the parameter myname to whatever we have named our PRIZM library object.
Also, as we mentioned, the code examples in this guide use the object name prizm. Also, for clarification, any statement
preceded by // means that it is a comment or explanation only and is not included as program code when our sketch is
compiled. Its considered good practice to be able to comment on and explain each step of code to make sure you can
understand it.

Appendix 133
Initialization Functions
The TETRIX PRIZM controller initialization functions are used to begin or, when needed, end a program.
myname.PrizmBegin();
This is always called in the setup() section of an Arduino sketch. This function initializes critical parameters and invokes the
use of the Start button on the PRIZM controller. It must always be used when creating sketches for the PRIZM controller.
Example:
prizm.PrizmBegin(); // initialize the PRIZM controller
myname.PrizmEnd();
This function ends or terminates a program, resets critical parameters, and sends the program back to start.
Example:
prizm.PrizmEnd(); // when called, will terminate the program execution
The TETRIX PRIZM sensor ports are used for connecting various types of sensors for digital and analog communication.
The digital sensors can be connected to PRIZM using ports D2-D5. Digital port D2 can also be configured as a software-
implemented serial communications port using the Arduinos software serial library. All digital ports can be configured
as either input or output. Analog sensors are connected to PRIZM using ports A1-A3. The analog sensor ports can be
configured for analog input or digital outputs. There is also one I2C sensor port. The I2C port is connected to the PRIZM I2C
communication bus. I2C addresses 0x01-0x06 are reserved internally by the PRIZM controller and may not be used for an
external sensor device. Support for sensors will be added to PRIZM continually. Please check on support and availability of
sensors at www.TETRIXrobotics.com.
myname.readLineSensor(port#);
This function reads the state of the Line Finder Sensor and returns a value of 1 or 0. The parameter port# specifies which
digital port the sensor is plugged into. The line sensors most common application is to sense the difference between black
and white surfaces. This is a very common way to program a robot to follow the edge of a dark line on a white surface or
the edge of a white line on a dark surface. It accomplishes this by detecting a beam of reflected infrared light from the
surface that it is facing. If the surface it is facing is a light color or is reflective, the light beam will bounce from it and be
detected by the sensor. If the surface is dark, or if the sensor is too far away from the facing surface, a reflected light beam
will not be detected. The Line Finder Sensor has a small adjustment potentiometer screw on the back side to adjust beam
sensitivity. If the sensor detects a non-reflective surface (it does not receive the reflected beam; there is a dark area or
line), the function will return 1, meaning line detected. If the sensor is receiving the reflected beam (there is a white or
reflective surface), the function will return 0, meaning no line detected. See the example below or the PRIZM library for a
full line follower example.
Example:
int x; // set up an integer variable named x
x = prizm.readLineSensor(2); // read the state of line sensor connected to digital
// port (D2) into the variable x
or
Serial.print(prizm.readLineSensor(2)); // read the state of line sensor connected to digital
// port (D2) and print it to the
// Arduino serial monitor

134 Appendix
myname.readSonicSensorCM(port#);
This function reads and returns the distance of an object that is in front of the Ultrasonic Sensor. The parameter port#
specifies which digital sensor port the sensor is plugged into. The Ultrasonic Sensor is a non-contact distance measurement
module that can be used to give our robot vision so that it can know when objects are in its path and avoid them. The
sensors frequency is modulated at 42 kHz and has a measuring range of 3-400 centimeters. The sensor works by sending
a sonic pulse burst and then waiting on its return to the sensor as it is reflected off an object in range. The reflected sonic
pulse time period is measured to determine the distance to the object.
Example:
int x; // set up an integer variable x
x = prizm.readSonicSensorCM(2); // read and store in variable x the distance in
// centimeters of an object that is detected to be
// in front of the Ultrasonic Sensor connected to
// digital port (D2)
or
Serial.print(prizm.readSonicSensorCM(2)); // read the distance in centimeters of an
// object that is detected in front of the
// Ultrasonic Sensor connected to digital
// port (D2) and print it to the Arduino serial
// monitor
myname.readSonicSensorIN(port#);
This function reads and returns the distance of an object in inches that is in front of the Ultrasonic Sensor. The parameter
port# specifies which digital sensor port the sensor is plugged into. The Ultrasonic Sensor is a non-contact distance
measurement module that can be used to give our robot vision so that it can know when objects are in its path and avoid
them. The sensors frequency is modulated at 42 kHz and has a measuring range of 2-150 inches. The sensor works by
sending a sonic pulse burst and then waiting on its return to the sensor as it is reflected off an object in range. The reflected
sonic pulse time period is measured to determine the distance to the object.
Example:
int x; // set up an integer variable x
x = prizm.readSonicSensorIN(port#); // read and store in variable x the distance
// in inches of an object that is detected to
// be in front of the Ultrasonic Sensor
// connected to digital sensor port (D2)
or
Serial.print(prizm.readSonicSensorIN(2)); // read the distance in inches of an
// object that is detected in front of
// the Ultrasonic Sensor connected
// to digital port (D2) and print it to
// the Arduino serial monitor

Appendix 135
Battery Voltage
The TETRIX battery pack is rated at 12 volts DC at 3,000 mAh. When the battery pack is fully charged, the peak voltage could
be as high as 15 volts. The battery pack is considered discharged when the voltage drops below 12.1 volts. This pack is also
equipped with a 20-amp safety fuse that is replaceable. All TETRIX-powered devices must use the genuine TETRIX safety
fuse-equipped battery pack for ensured safety. The battery voltage can be monitored in program code by calling the read
battery voltage function.
myname.readBatteryVoltage();
This function reads and returns the voltage of the TETRIX battery pack plugged into the PRIZM battery connection port.
The value returned is an integer and can be divided by 100 to scale to actual battery voltage. For example, a value of 918 is
equal to an actual battery voltage of 9.18 volts.
Example:
int x; // set up an integer value x
x = prizm.readBatteryVoltage(); // read the battery voltage into variable x
or
Serial.print(prizm.readBatteryVoltage()); // read the battery pack voltage and print it
// to the Arduino serial port monitor

Using the Start Button


myname.readStartButton();
The green Start button can be used for purposes other than starting the execution of program code. After it is used to start
a program, it can be read again at any point to execute other tasks, giving us more control over our program. One example
would be to have two different robot behaviors sectioned into two parts of code and use the Start button to begin the first
section, and then when pressed again, to jump to the second code section. The function will return 1 when the button is
pressed and a 0 when the button is pressed again.
Example:
int x; // set up a variable named x
x = prizm.readStartButton(); // read the state of the start button into variable x
or
While (prizm.readStartButton() == 0){ // wait here in a while loop until the start
// button is pressed (returns a 1)
}

136 Appendix
Onboard LEDs
The PRIZM controller has two onboard LEDs that can be controlled in code and used as indicators. The red and green LEDs
are switched on and off by setting each HIGH or LOW.
myname.setGreenLED(HIGH); // turns on the onboard green LED indicator
myname.setGreenLED(LOW); // turns off the onboard green LED indicator

Example:
prizm.setGreenLED(HIGH); // turns the PRIZM green LED on
prizm.setGreenLED(LOW); // turns the PRIZM green LED off

myname.setRedLED(HIGH); // turns on the onboard red LED indicator


myname.setRedLED(LOW); // turns off the onboard red LED indicator

Example:
prizm.setRedLED(HIGH); // turns the PRIZM red LED on
prizm.setRedLED(LOW); // turns the PRIZM red LED off

DC Motor Functions
The TETRIX PRIZM DC motor functions are used to control TETRIX DC Motors connected to Motor 1 and Motor 2 output
ports. There are three different types of control. Power control is used to set the amount of power and also set the direction.
The speed and targeting functions require a TETRIX quadrature encoder to be installed on the motor as a feedback sensor
to precisely control the speed rate and targeting position using a PID algorithm.
myname.setMotorPower(motor#, power);
This function is used to control the power level and direction of a TETRIX DC Motor connected to the Motor 1 or Motor 2
channel ports. The parameter motor# selects the motor channel and the parameter power sets the power level. Motor# can
be a number or variable of 1 or 2. Power is the amount of power to spin the motor, which can range between 0 and 100.
This is a percentage of power, meaning 0 is fully off, and 100 is 100% power or fully on. The direction is set by the sign of
the power parameter.
Example:
prizm.setMotorPower(1, 100); // spin Motor 1 clockwise at 100% power
prizm.setMotorPower(1, -100); // spin Motor 1 counterclockwise at 100% power
Also, when you are stopping a motor, there are two modes: coast stop and brake stop. Setting power to 0 will result in the
motor coasting to a stop. Setting power to 125 results in the motor stopping with a braking action.
Example:
prizm.setMotorPower(2, 0); // stop Motor 2 with a coast to stop; 0 = coast
or
prizm.setMotorPower(2, 125); // stop Motor 2 with a brake stop; 125 = brake

Appendix 137
myname.setMotorPowers(power1, power2);
This function enables us to set the power level of Motor 1 and Motor 2 at the same time. Set parameters power1 and
power2 to a power level percentage and both channels will respond at once. Remember, the sign (+/-) of the power level
determines the direction of rotation.
Example:
prizm.setMotorPowers(50, -50); // spin Motor 1 CW at 50% power and Motor 2
// CCW at 50% power
myname.setMotorSpeed(motor#, speed);
This function can precisely control the speed of a TETRIX DC Motor using a TETRIX quadrature encoder with PID control. An
encoder must be installed on the DC motor and plugged into the PRIZM encoder socket ENC1 or ENC2 that matches the
motor being controlled in order for this function to work. The encoder sends counting data back to the PRIZM controller,
where it is read and used to implement a complex algorithm called PID, short for proportional integral derivative. The PID
algorithm is very powerful and will precisely regulate and attempt to spin the motor at a constant rate even when the load
on the motor is changing. Using PID can enable precise control and positioning movement of the wheels and mechanisms
being driven by the TETRIX DC Motors. The parameter motor# can be either 1 or 2. The parameter speed is the rate
in degrees per second (DPS) that we wish to set our motor to spin. Values can range from a minimum of about 10 to a
maximum of 720 DPS. The direction of the motor is set by the sign of the speed parameter. Values outside that range might
not perform well.
Example:
prizm.setMotorSpeed(1, 360); // spin Motor 1 clockwise at a rate of 360 DPS
or
prizm.setMotorSpeed(1, -360); // spin Motor 1 counterclockwise at a rate of 360
// DPS
myname.setMotorSpeeds(speed1, speed2);
This function implements PID control and is identical to the previous function just explained with the difference being
that it will set the speed1 and speed2 parameters of Motor 1 and Motor 2 channels at the same time. Both motors being
controlled must have a TETRIX encoder installed and plugged into their matching encoder channel sockets ENC1 and ENC2
on the PRIZM controller.
Example:
prizm.setMotorSpeeds(360, -180); // spin Motor 1 clockwise at 360 DPS and spin
// Motor 2 counterclockwise at 180 DPS

138 Appendix
myname.setMotorTarget(motor#, speed, target);
In addition to speed control, this function adds a characteristic of control by using a PID algorithm to implement both
velocity and position target PID control. The motor being controlled as set by parameter motor# must be equipped with
a TETRIX encoder plugged into the controlled motors encoder port. This function will spin the commanded DC motor to
make the current encoder value become equal to a target value. The motor will rotate toward the commanded target at
a rotation rate set in the speed parameter (degrees per second). When the motor has reached the encoder count location
set in the target parameter, it will hold its position in a servo-like mode until a new command is received. This function
ignores the positive/negative sign of the speed parameter. Instead, the direction of rotation is set by the commanded
encoder target value of the target parameter. While this function is executing, the readMotorBusy() function can be read to
indicate that the motor is busy moving to its target. When this function returns HIGH, or 1, the command motor channel
is busy (moving to the target). When the target has been reached, the readMotorBusy() function will return LOW or 0. The
encoder resolution is 1,440 counts per revolution of the motor shaft, or 1/4 degree per count.
Example:
prizm.setMotorTarget(1, 360, 1440); // spin Motor 1 clockwise at 360 DPS; stop
// and hold position when the encoder
// count equals 1440 (one revolution)

prizm.setMotorTarget(2, 180, -1440); // spin Motor 1 counterclockwise at 180


// DPS; stop and hold position when the
// encoder count equals -1440

myname.setMotorTargets(speed1, target1, speed2, target2);


This function implements velocity and position target PID control for both DC motor channels. This function sets the mode
parameters of both Motor 1 and Motor 2 at the same time for an even start. Having our motors start as closely in sync
as possible can be critical in some applications. The speed and stopping target of Motor 1 are set by parameters speed1
and target1. Likewise, the speed and stopping target of Motor 2 are set by parameters speed2 and target2. This function
implements PID control for both DC motor channels. Encoders must be installed on both motors and plugged into their
matching encoder connection ports ENC1 and ENC2 on the PRIZM controller. This function will spin both DC motors to
make their current encoder values become equal to their target values. The motors will rotate toward the commanded
targets at rotation rates set in speed1 and speed2 (degrees per second). When each motor has reached its target set in
target1 and target2, it will hold its position in a servo-like mode until a new command is received. This function ignores the
positive/negative sign value of speed1 and speed2. The direction of rotation is set by the commanded encoder target value
of target1 and target2. While this function is executing, the readMotorBusy() function can be read to indicate that the motor
channel is busy moving to its target. When this function returns HIGH or 1, the command motor channel is busy (moving
to the target). When the target has been reached, the readMotorBusy() function will return LOW or 0. The encoder
resolution is 1,440 counts per revolution of the motor shaft, or 1/4 degree per count.
Example:
prizm.setMotorTargets(360, 1440, 180, -1440); // this command will spin Motor 1
// clockwise at a rate of 360 DPS,
// stop, and hold position at encoder
// count 1440. Motor 2 will spin
// counterclockwise at a rate of 180
// DPS, stop, and hold position
// at encoder count -1440.

Appendix 139
myname.setMotorDegree(motor#, speed, degrees);
This function requires the DC motor being controlled to have an encoder installed in order to implement velocity and
position target control using PID. This function is identical to the set target functions explained previously, except the
target values are defined in degrees instead of encoder counts. Working in degrees might be a bit more intuitive than
actual encoder counts. The parameter motor# sets the function to control either Motor 1 or Motor 2. The speed parameter is
the rate in degrees per second for the commanded motor to maintain. The degrees parameter sets the position in degrees
to which the motor will rotate, stop, and hold position. When called, this function will spin the commanded motor channel
to make the current encoder degrees value to become equal to the degrees value. The motor will rotate toward the
commanded target at a rotation rate set in speed (degrees per second). When the motor has reached the target, it will hold
its position in a servo-like mode until a new command is received. This function ignores the positive/negative sign value of
speed. The direction of rotation is set by the commanded encoder target value of degrees. While this function is executing,
the readMotorBusy() function can be read to indicate that the motor is busy moving to its target. When this function returns
1, the command motor channel is busy (moving to the target). When the target has been reached, the readMotorBusy()
function will return 0. The encoder degrees resolution is 1 degree.
Example:
prizm.setMotorDegree(1, 360, 180); // spin DC Motor Channel 1 clockwise at
// 360 degrees per second to target position
// 180 degrees (1/2 revolution) and stop
// when target is reached, stop motor and
// hold position at the 180 degree mark

prizm.setMotorDegree(2, 180, -360); // spin DC Motor Channel 1 counterclockwise


// at 180 degrees per second to
// target position -360 degrees or
// (1 revolution) and stop; when target is
// reached, stop motor and hold position at
// the -360 degree mark

140 Appendix
myname.setMotorDegrees(speed1, degrees1, speed2, degrees2);
This function commands the speed and degrees target of both motor channels at the same time. This function requires
both DC motors being controlled to have an encoder installed in order to implement velocity and position target PID
control. This function is identical to the set targets functions explained previously, except the target values are defined
in degrees instead of encoder counts. Working in degrees might be a bit more intuitive than actual encoder counts. The
parameter speed1 sets the rate of rotation for Motor 1 in degrees per second. The degrees1 parameter is the position in
degrees to which the Motor 1 will rotate, stop, and hold position. The speed2 parameter sets the rate of rotation of Motor
2 in degrees per second. The parameter degrees2 sets the position in degrees to which Motor 2 will rotate, stop, and hold
position. When called, this function will spin the commanded motor channel to make the current encoder degrees value
become equal to the degrees value. The motors will rotate toward their commanded targets at rotation rates set in speed1
and speed2 (degrees per second). When a motor has reached the target, it will hold its position in a servo-like mode until
a new command is received. This function ignores the positive/negative sign values of speed1 and speed2. The direction
of rotation is set by the commanded encoder target value of degrees1 and degrees2. While this function is executing, the
readMotorBusy() function can be read to indicate that a motor is busy moving to its target. When this function returns
1, the command motor channel is busy (moving to the target). When the target has been reached, the readMotorBusy()
function will return 0. The encoder degrees resolution is 1 degree.
Example:
prizm.setMotorDegrees(360, 360, 180, -360); // spin Motor 1 clockwise at 360
// degrees per second and stop at
// encoder target position of 360
// degrees; spin Motor 2 counterclockwise
// at 180 degrees per
// second and stop at encoder target
// position of -360 degrees; each
// motor will hold its position when it
// has reached its target value
myname.setMotorInvert(motor#, invert);
This function can be used to invert the forward/reverse direction mapping for both DC motor channels and their encoder
inputs. This function is intended to harmonize the forward and reverse directions for motors on opposite sides of a
skid-steer robot chassis. The motor# parameter sets the motor channel of 1 or 2. The value of the invert parameter sets
the condition; invert = 0 means no invert, while invert = 1 inverts the motor and encoder set by channel. The default on
controller power-up is no invert. This function needs to be called only once in the setup section of an Arduino sketch.
However, it can be called again and changed at any time in program code.
Example:
prizm.setMotorInvert(1, 1); // invert the rotation direction of Motor 1
prizm.setMotorPower(1, 50); // normally, this function would cause Motor 1 to
// spin at 50% power in the clockwise direction
// but because we called the invert function, the
// actual resulting rotation of Motor 1 will be
// counterclockwise (-50); the same mapping of
// direction applies to all the motor power,
// speed, and targeting functions when invert is
// called

Appendix 141
myname.readMotorBusy(motor#);
This function will return a value equal to 0 or 1 depending on the status of any DC motor channel that is in targeting
mode. That is, any DC motor that is being controlled using an encoder for rotation to an encoder count or degree target will
return its status when polled with this function. The parameter motor# sets the motor channel to 1 or 2. The function will
return 1 when the motor is moving toward its target. The function will return 0 when the motor has reached its target.
Being able to poll the status of a targeting motor can be very useful in code to wait for a motor to reach its target before
continuing on to the next instruction.
Example:
int x; // define an integer variable x
x = prizm.readMotorBusy(1); // read the returned status of Motor 1 into x
or
x = prizm.readMotorBusy(2); // read the returned status of Motor 2 into x
or
Serial.print(prizm.readMotorBusy(1)); // print the status of Motor 1 to the Arduino
// serial monitor
myname.readMotorCurrent(motor#);
This function reads the amount of DC current being drawn by a TETRIX DC Motor connected to either the Motor 1 or Motor
2 port. The motor# parameter sets the motor channel to 1 or 2. The amount of current that a motor draws will depend on
the amount of load that the motor is moving. A heavier load on our motor will result in larger current draws. In order to
keep from damaging a motor, it is important to not subject a motor to continuous high loads. High current draws will cause
a DC motor to heat and eventually might result in failure. This function can be used to set a limit on how much current a
motor can be allowed to draw. See the example program that came with the PRIZM library for a full code example of how
to implement current limiting.
Example:
int x; // create an integer variable x
x = prizm.readMotorCurrent(1); // read the Motor 1 current and store it in x
or
Serial.print(prizm.readMotorCurrent(2)); // read Motor 2 current and print it to the
// serial monitor; the value that this
// function returns is an integer and is the
// current in milliamps; for example, a value
// of 1500 equals 1.5 amps

142 Appendix
Encoder Functions
The TETRIX quadrature motor encoders enable precise speed and position control of TETRIX DC Motors. The PRIZM
controller will read the counting data of the encoder and implement an internal PID algorithm. The PRIZM controllers
PID control algorithm has been tuned for TETRIX DC Motors in combination with the TETRIX quadrature encoders. TETRIX
quadrature encoders (38000) produce 360 counts per revolution (CPR) and 1,440 pulses per revolution (PPR). This translates
to 1/4 degree of DC motor shaft rotational resolution.
myname.readEncoderCount(enc#);
This function reads and returns the encoder count of a TETRIX quadrature encoder attached to encoder port ENC1 or ENC2.
The count value returned is a long-type value. The parameter enc# can be 1 or 2. A value of 1 will return the count value of
ENC1. An enc# value of 2 will return the count value of ENC2. For reference, 1,440 counts = 1 motor shaft revolution.
Example:
long x; // set up a long-type variable x
x = prizm.readEncoderCount(1); // read and store the count of ENC1 into the long
// variable x
or
Serial.print(prizm.readEncoderCount(2); // print the count value of ENC2 to the
// Arduino serial monitor
myname.readEncoderDegrees(enc#);
This function reads and returns the encoder count in degrees of a TETRIX quadrature encoder attached to encoder port
ENC1 or ENC2. The value returned is a long-type value. The parameter enc# can be 1 or 2. A value of 1 will return the degree
position of ENC1. A value of 2 will return the degree position of ENC2. Using the degrees function to track motor rotation
might be more intuitive than using the raw encoder count values. Although the raw count value will yield 1/4-degree
resolution, the degrees function resolution is limited to 1-degree resolution.
Example:
long x; // set up a long-type variable x
x = prizm.readEncoderDegrees(1); // set up a long-type variable x; read and
// store the degree value of ENC1 into the
// long variable x
or
Serial.print(prizm.readEncoderDegrees(2)); // print the degree value of ENC2 to
// the Arduino serial monitor
myname.resetEncoder(enc#);
When called, this function resets the encoder count set in parameter enc# (1 or 2) to 0.
Example:
prizm.resetEncoder(1); // reset Encoder count 1 (ENC1) to 0
prizm.resetEncoder(2); // reset Encoder count 2 (ENC2) to 0

myname.resetEncoders();
When called, this function resets the encoder count of Encoders 1 and 2 to 0.
Example:
prizm.resetEncoders(); // reset both encoders (1 and 2) to 0

Appendix 143
Servo Motor Functions
TETRIX servo motors come in two types. A standard servo can be commanded to rotate to any position between 0 and
180 degrees. Once there, it will hold its position and resist being moved. The PRIZM controller has six servo ports marked
1-6. Each port can control one standard servo for six independent channels of servo control. A different type of servo is a
continuous rotation (CR) servo. A CR servo is designed to rotate clockwise or counterclockwise in the same way that a DC
motor can. The PRIZM controller has two specialized ports marked CR1 and CR2 that are designed to spin CR servo motors
in either direction.
A CR motor can be helpful when we want a DC motor to fit into a small space or for light-duty loads. When you are
plugging servos into a PRIZM servo port, the servo red wire matches the + mark, and the black wire matches the - mark.
myname.setServoSpeed(servo#, speed);
This function sets the speed of a standard servo while it is rotating to its commanded position. The parameter servo#
selects the servo channel. The parameter speed sets the speed of rotation. This function needs to be called only once at the
beginning of a program. However, it can be called at any time in program code to change speeds. Speed range is between
0 and 100, equating to speeds of 0-100%. If this function is not called, servo speed will always default to 100.
Example:
prizm.setServoSpeed(1, 25); // set the speed of Servo channel 1 to 25%

myname.setServoSpeeds(speed1, speed2, speed3, speed4, speed5, speed6);


This function enables setting all six servo channels within one function statement. The parameters speed(#) can range
between 0 and 100. Each speed can be different. Please note that when using the setServoSpeeds functions, values must
be set for all six ports, even if you are not using all six.
Example:
prizm.setServoSpeeds(25, 25, 25, 25, 25, 25); // set all six servo channel speeds to 25%

myname.setServoPosition(servo#, position);
This function rotates a servo to a specified target position. The parameter servo# refers to the servo that will change
position. The position parameter specifies the target position to move toward. Position can be anywhere between 0 and
180. The position parameter represents the position rotational target of the servo in degrees. Be careful not to try to rotate
a servo past its internal mechanical limit. This limit can vary slightly from servo to servo. When rotating toward an extreme
value of 0 or 180, listen to the servo to be sure it is not making a buzzing noise, meaning it could be against a mechanical
stop. This might cause damage to the servo and waste battery power.
Example:
prizm.setServoPosition(1, 180); // rotate Servo 1 to position 180 degrees

myname.setServoPositions(position1, position2, position3, position4, position5, position6);


This function enables rotation of all six servo channels at once to target positions. The parameter position(#) sets the target
position of each channel in order of 1-6. The target position can be any value of 0-180, which represents degrees. Each
target position may be different. Please note that when using the setServoPositions functions, values must be set for all six
ports, even if you are not using all six.
Example:
prizm.setServoPositions(90,90,90,90,90,90); // rotate all six servos to 90 degrees

144 Appendix
myname.readServoPosition(servo#);
This function reads and returns the current position of a servo channel. The parameter servo# can be any channel 1-6. The
value returned is an integer type of value 0-180.
Example:
int x; // set up an integer variable named x
x = prizm.readServoPosition(1); // read the current rotation position of Servo
// channel 1 and store it in variable x
or

Serial.print(prizm.readServoPosition(2)); // read the position of Servo channel 2 and


// print it to the Arduino serial monitor
myname.setCRServoState(CRservo#, state);
This function controls the on/off state of a continuous rotation (CR) servo motor. The parameter CRservo# refers to the CR
motor channel to be controlled. The state parameter sets the on/off state and the direction of rotation. The state parameter
can be 100 for clockwise rotation, -100 for counterclockwise rotation, and 0 for off, or stop.
Example:
prizm.setCRServoState(1, 100); // turn CR1 on to rotate continuous in a clockwise
// direction

prizm.setCRServoState(1, 0); // turn CR1 off and stop

prizm.setCRServoState(1, -100); // turn CR1 on to rotate continuous in a


// counterclockwise direction

Appendix 145
TETRIX PRIZM Arduino Library Functions Chart
Description Function Coding Example
Prizm Begin PrizmBegin(); PrizmBegin();
Is called in the Arduino code setup() Reset and initialize PRIZM controller.
loop. Initializes the PRIZM controller. Data Type: None
PrizmEnd PrizmEnd(); PrizmEnd();
When called, immediately terminates Terminate a PRIZM program and reset
a program and resets the PRIZM Data Type: None controller.
controller.
Set Red LED setRedLED(state); setRedLED(HIGH);
Sets the PRIZM red indicator LED to on Data Type: or
or off. state = integer setRedLED(1);
Turn red LED on.
Data Range: setRedLED(LOW);
state = 1 or 0 or
or setRedLED(0);
state = HIGH or LOW Turn red LED off.
Set Green LED setGreenLED(state); setGreenLED(HIGH);
Sets the PRIZM green indicator LED to or
on or off. Data Type: setGreenLED(1);
state = integer Turn green LED on.
setGreenLED(LOW);
Data Range: or
state = 1 or 0 setGreenLED(0);
or Turn green LED off.
state = HIGH or LOW
Set DC Motor Power setMotorPower(motor#, power); setMotorPower(1, 50);
Sets the power level and direction of Data Type: Spin Motor 1 clockwise at 50% power.
a TETRIX DC Motor connected to the motor# = integer setMotorPower(2, -50%);
PRIZM DC motor ports. Power level power = integer Spin Motor 2 counterclockwise at 50%
range is 0 to 100. Direction is set by power.
the sign (+/-) of the power level. Power Data Range: setMotorPower(1, 0);
level 0 = stop in coast mode. Power motor# = 1 or 2 Turn off Motor 1 in coast mode.
level 125 = stop in brake mode. power = -100 to 100 setMotorPower(2, 125);
or Turn off Motor 2 in brake mode.
power = 125 (brake mode)
Set DC Motor Powers setMotorPowers(power1, power2); setMotorPowers(50, 50);
Simultaneously sets the power level Data Type: Spin Motor 1 and Motor 2 clockwise at
and direction of both TETRIX DC power1 = integer 50% power.
Motors connected to the PRIZM motor power2 = integer setMotorPowers(-50, 50);
ports. Both PRIZM Motor 1 and Motor Spin Motor 1 counterclockwise and
2 channel parameters are set with Data Range: Motor 2 clockwise at 50% power.
a single statement. The power level power1 = -100 to 100 setMotorPowers(0, 0);
range is 0 to 100. Direction is set by power2 = -100 to 100 Turn off Motor 1 and Motor 2 in coast
the sign (+/-) of the power level. Power or mode.
level 0 = stop in coast mode. Power power1 = 125 (brake mode) setMotorPowers(125, 125);
level 125 = stop in brake mode. power2 = 125 (brake mode) Turn off Motor 1 and Motor 2 in brake
mode.
Set DC Motor Speed setMotorSpeed(motor#, speed); setMotorSpeed(1, 360);
Uses velocity PID control to set the Data Type: Spin Motor 1 clockwise at a constant
constant speed of a TETRIX DC Motor motor# = integer speed of 360 DPS.
with a TETRIX motor encoder installed. speed = integer setMotorSpeed(1, -360);
The speed parameter range is 0 to 720 Spin Motor 1 counterclockwise at a
degrees per second (DPS). The sign Data Range: constant speed of 360 DPS.
(+/-) of the speed parameter controls motor# = 1 or 2
direction of rotation. speed = -720 to 720

146 Appendix
Description Function Coding Example
Set DC Motor Speeds setMotorSpeeds(speed1, speed2); setMotorSpeed(360, 360);
Uses velocity PID control to Spin Motor 1 and Motor 2 clockwise at a
simultaneously set the constant Data Type: constant speed of 360 DPS.
speeds of both TETRIX DC Motor speed1 = integer setMotorSpeed(360, -360);
channels with TETRIX motor encoders speed2 = integer Spin Motor 1 clockwise and Motor 2
installed. Both PRIZM Motor 1 and counterclockwise at a constant speed of
Motor 2 channel parameters are set Data Range: 360 DPS.
with a single statement. The speed speed1 = -720 to 720 setMotorSpeed(360, -180);
parameter range is 0 to 720 degrees speed2 = -720 to 720 Spin Motor 1 clockwise and Motor 2
per second (DPS). The sign (+/-) of the counterclockwise at a constant speed of
speed parameter controls direction 180 DPS.
of rotation.
Set DC Motor Target setMotorTarget(motor#, speed, setMotorTarget(1, 360, 1440);
Implements velocity and positional target); Spin Motor 1 at a constant speed of 360
PID control to set the constant speed DPS until encoder 1 count equals 1,440.
and the encoder count target holding Data Type: When at encoder target count, hold
position of a TETRIX DC Motor with a motor# = integer position in a servo-like mode.
TETRIX encoder installed. The speed speed = integer setMotorTarget(2, 180, -1440);
parameter range is 0 to 720 degrees target = long Spin Motor 2 at a constant speed of 180
per second (DPS). The encoder count DPS until encoder 2 count equals -1,440
target position is a signed long integer Data Range: (1 revolution). When at encoder target
from -2,147,483,648 to 2,147,483,647. motor# = 1 or 2 count, hold position in a servo-like mode.
Each encoder count = 1/4-degree speed = 0 to 720
resolution. target = -2147483648 to 2147483647
Set DC Motor Targets setMotorTargets(speed1, target1, setMotorTargets(360, 1440, 360,
Implements velocity and positional speed2, target2); 1440);
PID control to simultaneously set the Spin Motor 1 and Motor 2 at a constant
constant speeds and the encoder Data Type: speed of 360 DPS until each motor
count target holding positions of speed1 = integer encoder count equals 1,440. When a
both TETRIX DC Motor channels each target1 = long motor reaches its encoder target count,
with TETRIX encoders installed. Both speed2 = integer hold position in a servo-like mode.
PRIZM Motor 1 and Motor 2 channel target2 = long setMotorTargets(360, 1440, 180,
parameters are set with a single 2880);
statement. The speed parameter Data Range: Spin Motor 1 at a constant speed of 360
range is 0 to 720 degrees per second speed1 = 0 to 720 DPS until encoder 1 count equals 1,440.
(DPS). The encoder count target target1 = -2147483648 to 2147483647 Spin Motor 2 at a constant speed of 180
position is a signed long integer speed2 = 0 to 720 DPS until encoder 2 equals 2,880. Each
from -2,147,483,648 to 2,147,483,647. target2 = -2147483648 to 2147483647 motor will hold its position in a servo-
Each encoder count = 1/4-degree like mode when it reaches the encoder
resolution. target.

Note: One encoder count equals


1/4-degree resolution. For example, 1
motor revolution equals 1,440 encoder
counts (1,440 / 4 = 360).
Set Motor Degree setMotorDegree(motor#, speed, setMotorDegree(1, 180, 360);
Implements velocity and positional degrees); Spin Motor 1 at a constant speed of 180
PID control to set the constant DPS until encoder 1 degree count equals
speed and the degree target holding Data Type:
360. When at encoder target degree
position of a TETRIX DC Motor with a motor# = integer
count, hold position in a servo-like mode.
speed = integer
TETRIX encoder installed. The speed setMotorDegree(2, 90, 180);
parameter range is 0 to 720 degrees degrees = long
Spin Motor 2 at a constant speed of 90
per second (DPS). The encoder degrees Data Range: DPS until encoder 2 degree count equals
target position is a signed long integer motor# = 1 or 2 180. When at encoder target degree
from -536,870,912 to 536,870,911 with speed = 0 to 720 count, hold position in a servo-like mode.
a 1-degree resolution. degrees = -536870912 to 536870911

Appendix 147
Description Function Coding Example
Set Motor Degrees setMotorDegrees(speed1, degrees1, setMotorDegrees(180, 360, 180, 360);
Implements velocity and positional PID speed2, degrees2); Spin Motor 1 and Motor 2 at a constant
control to set the constant speeds and speed of 180 DPS until each motor
the degree target holding positions Data Type: encoder degree count equals 360. When
of both TETRIX DC Motor channels speed1 = integer a motor reaches its degree target count,
with TETRIX encoders installed. Both degrees1 = long hold position in a servo-like mode.
PRIZM Motor 1 and Motor 2 channel speed2 = integer setMotorDegrees(360, 720, 90, 360);
parameters are set with a single degrees2 = long Spin Motor 1 at a constant speed of 360
statement. The speed parameter range DPS until encoder 1 degree count equals
is 0 to 720 degrees per second (DPS). Data Range: 720. Spin Motor 2 at a constant speed
The encoder degree target position is a speed1 = 0 to 720 of 90 DPS until encoder 2 degree equals
signed long integer from -536,870,912 degrees1 = -536870912 to 536870911 360. Each motor will hold its position in
to 536,870,911 with a 1-degree speed2 = 0 to 720 a servo-like mode when it reaches the
resolution. degrees2 = -536870912 to 536870911 encoder target.

Set Motor Direction Invert setMotorInvert(motor#, invert); setMotorInvert(1, 1);


Inverts the forward/reverse direction Invert the spin direction mapping of
mapping of a DC motor channel. This Data Type: Motor 1.
function is intended to harmonize motor# = integer setMotorInvert(2, 1);
the forward and reverse directions invert = integer Invert the spin direction mapping of
for motors on opposite sides of a Motor 2.
skid-steer robot chassis. Inverting Data Range: setMotorInvert(1, 0);
one motor channel can make coding motor# = 1 or 2 Do not invert the spin direction mapping
opposite-facing DC motors working invert = 0 or 1 of Motor 1.
in tandem more intuitive. An invert setMotorInvert(2, 0);
parameter of 1 = invert. An invert Do not invert the spin direction mapping
parameter of 0 = no invert. The default of Motor 2.
is no invert.
Note: Non-inverting is the default on
PRIZM power-up or reset.

Read Motor Busy Status readMotorBusy(motor#); readMotorBusy(1);


The busy flag can be read to check Return the busy status of Motor 1.
on the status of a DC motor that is Data Type: readMotorBusy(2);
operating in positional PID mode. The motor# = integer Return the busy status of Motor 2.
motor busy status will return 1 if it
is moving toward a positional target Data Range:
(degrees or encoder count). When it motor# = 1 or 2
has reached its target and is in hold
mode, the busy status will return 0. Data Type Returned:
value = integer
Read DC Motor Current readMotorCurrent(motor#); readMotorCurrent(1);
Reads the DC motor current of each Read the motor load current of the
TETRIX DC Motor attached to PRIZM Data Type: PRIZM Motor 1 channel.
Motor 1 and Motor 2 ports. The integer motor# = integer readMotorCurrent(2);
value that is returned is motor load Read the motor load current of the
current in milliamps. Data Range: PRIZM Motor 2 channel.
motor# = 1 or 2
Example: 1500 = 1.5 amps
Data Type Returned:
value = integer

148 Appendix
Description Function Coding Example
Read Encoder Count readEncoderCount(enc#); readEncoderCount(1);
Reads the encoder count value. The Read the current count value of encoder
PRIZM controller uses encoder pulse Data Type: 1 (ENC 1 port).
data to implement PID control of a enc# = integer readEncoderCount(2);
TETRIX DC Motor connected to the Read the current count value of encoder
motor ports. The PRIZM controller Data Range: 2 (ENC 2 port).
counts the number of pulses produced enc# = 1 or 2
over a set time period to accurately
control velocity and position. Each 1/4 Data Type Returned:
degree equals one pulse, or count, or value = long
1 degree of rotation equals 4 encoder
counts. The current count can be
read to determine a motors shaft
position. The total count accumulation
can range from -2,147,483,648 to
2,147,483,647. A clockwise rotation
adds to the count value, while a
counterclockwise rotation subtracts
from the count value. The encoder
values are set to 0 at power-up and
reset.
Read Encoder Degrees readEncoderDegrees(enc#); readEncoderDegrees(1);
Reads the encoder degree value. The Read the current degree count value of
PRIZM controller uses encoder pulse Data Type: encoder 1 (ENC 1 port).
data to implement PID control of a enc# = integer readEncoderDegrees(2);
TETRIX DC Motor connected to the Read the current degree count value of
motor ports. The PRIZM controller Data Range: encoder 2 (ENC 2 port).
counts the number of pulses produced enc# = 1 or 2
over a set time period to accurately
control velocity and position. This Data Type Returned:
function is similar to the encoder count value = long
function, but instead of returning the
raw encoder count value, it returns
the motor shaft position in degrees.
The total degree count accumulation
can range from -536,870,912 to
536,870,911. A clockwise rotation
adds to the count value, while a
counterclockwise rotation subtracts
from the count value. The encoder
values are set to 0 at power-up and
reset.
Reset Each Encoder resetEncoder(enc#); resetEncoder(1);
Calling this function will reset the Reset the encoder 1 count to 0.
encoder count accumulator to 0. Data Type: resetEncoder(2);
enc# = integer Reset the encoder 2 count to 0.

Data Range:
= 1 or 2
Reset Both Encoders (1 and 2) resetEncoders(); resetEncoders();
Calling this function will reset encoder Reset the encoder 1 count to 0 and
1 and encoder 2 count accumulators Data Type: None encoder 2 count to 0.
to 0.

Appendix 149
Description Function Coding Example
Read Line Sensor Output readLineSensor(port#); readLineSensor(2);
Reads the digital output of the Line Read the digital value of a Line Finder
Finder Sensor connected to a PRIZM Data Type: Sensor on digital sensor port D2.
sensor port. The value read is 0 when port# = integer
reflected light is received (detecting Note: The Line Finder Sensor can be
a light-colored surface) and 1 when Data Range: connected to any digital port D2-D5, or
light is not received (detecting a dark- port# (See note in adjacent column.) analog ports A1-A3 configured as digital
colored surface, such as a line). input. See technical section on sensor
Data Type Returned: ports for a more detailed explanation of
value = integer (0 or 1) sensor ports and pinouts.
Read Ultrasonic Sensor in readSonicSensorCM(port#); readSonicSensorCM(3);
Centimeters Read the distance in centimeters of an
Reads the distance in centimeters of an Data Type: object placed in front of the Ultrasonic
object placed in front of the Ultrasonic port# = integer Sensor connected to digital sensor port
Sensor. The sensor is modulated at D3.
42 kHz and has a range of 3 to 400 Data Range:
centimeters. The value read is an port# (See note in adjacent column.) Note: The Ultrasonic Sensor can be
integer. connected to any digital port D2-D5, or
Data Type Returned: analog ports A1-A3 configured as digital
value = integer (3 to 400) input. See technical section on sensor
Min and max might slightly vary. ports for a more detailed explanation of
sensor ports and pinouts.
Read Ultrasonic Sensor in Inches readSonicSensorIN(port#); readSonicSensorIN(4);
Reads the distance in inches of an Read the distance in inches of an object
object placed in front of the Ultrasonic Data Type: placed in front of the Ultrasonic Sensor
Sensor. The sensor is modulated at 42 port# = integer connected to digital sensor port D4.
kHz and has a range of 2 to 150 inches.
The value read is an integer. Data Range: Note: The Ultrasonic Sensor can be
port# (See note in adjacent column.) connected to any digital port D2-D5, or
analog ports A1-A3 configured as digital
Data Type Returned: input.
value = integer (2 to 150)
Min and max might slightly vary.
Read Battery Pack Voltage readBatteryVoltage(); readBatteryVoltage();
Reads the voltage of the TETRIX Read the voltage of the TETRIX battery
battery pack powering the PRIZM Data Type: None pack powering the PRIZM controller.
controller. The value read is an integer.
Data Type Returned: Example: A value of 918 equals 9.18
value = integer volts.

Read Start Button State readStartButton(); readStartButton();


Reads the state of the green PRIZM Read the Start button. A value of 1
Start button. A returned value of 1 Data Type: None means button is pressed. A value of 0
indicates a pressed state. A returned means button is not pressed.
value of 0 indicates a not-pressed Data Type Returned:
state. value = integer (0 or 1)

150 Appendix
Description Function Coding Example
Set Speed of a Servo Motor setServoSpeed(servo#, speed); setServoSpeed(1, 25);
This function sets the speed of a servo Set the speed of servo channel 1 to 25%.
motor connected to a PRIZM servo Data Type: setServoSpeed(2, 50);
port 1-6. The speed parameter can be servo# = integer Set the speed of servo channel 2 to 50%.
0 to 100%. The servo motor channel speed = integer
parameter can be any number 1 to 6.
If not specified, the speed of a servo Data Range:
defaults to 100 (maximum speed). servo# = 1 to 6
When a servo speed has been set, it speed = 0 to 100
will always move at the set speed until
changed. Unless we are changing
speeds, it will need to be called only
once at the beginning of a program.
Set Speeds of All Servo Motors setServoSpeeds(speed1, speed2, setServoSpeeds(25, 25, 25, 25, 25, 25);
This function will set the speeds of all speed3, speed4, speed5, speed6); Set the speeds of all six servo channels
six servo channels simultaneously with to 25%.
a single command. All six speeds are in Data Type: setServoSpeeds(25, 35, 45, 55, 65, 75);
sequential order and can be 0 to 100%. speed1-speed6 = integer Servo 1 speed = 25%
All six servo speeds may be the same Servo 2 speed = 35%
or different. Data Range: Servo 3 speed = 45%
speed1-speed6 = 0 to 100 Servo 4 speed = 55%
Servo 5 speed = 65%
Servo 6 speed = 75%
Set Position of a Servo Motor setServoPosition(servo#, position); setServoPosition(1, 90);
Set the angular position of a servo Set the angular position of Servo Motor 1
motor connected to a PRIZM servo Data Type: to 90 degrees.
motor port 1-6. The position parameter servo# = integer setServoPosition(2, 130);
can be any value between 0 and position = integer Set the angular position of Servo Motor 2
180 degrees. Any value outside this to 130 degrees.
range is ignored. Not all servos are the Data Range:
same, so be careful when operating servo# = 1 to 6
a servo motor at the extreme ranges. position = 0 to 180
Listen closely; if a servo is buzzing, it is
pressing against its mechanical stop,
which might damage the motor. If
this happens, limit the range to values
slightly greater than 0 and slightly less
than 180 to avoid damage to the servo
motor.
Set Positions of All Servo Motors setServoPositions(position1, setServoPositions(90, 90, 90, 90, 90,
Set the angular positions of all six position2, position3, position4, 90);
servo motors connected to the position5, position6); Set the angular positions of all six servo
PRIZM servo motor ports 1-6. The motors connected to PRIZM servo ports
position parameter can be any value Data Type: 1-6 to 90 degrees.
between 0 and 180 degrees. Any value position1-position6 = integer setServoPositions(10, 20, 30, 40, 50,
outside this range is ignored. Not all 60);
servos are the same, so be careful Data Range: Set the angular positions of all six servo
when operating a servo motor at the position1-position6 = 0 to 180 motors connected to PRIZM servo ports
extreme ranges. Listen closely; if a 1-6.
servo is buzzing, it is pressing against Servo 1 position = 10 degrees
its mechanical stop, which might Servo 2 position = 20 degrees
damage the motor. If this happens, Servo 3 position = 30 degrees
limit the range to values slightly Servo 4 position = 40 degrees
greater than 0 and slightly less than Servo 5 position = 50 degrees
180 to avoid damage to the servo Servo 6 position = 60 degrees
motor.

Appendix 151
Description Function Coding Example
Read a Servo Position readServoPosition(servo#); readServoPosition(1);
Reads the most recent commanded Read the most recent commanded
position of a servo motor connected Data Type: position of Servo 1.
to PRIZM servo ports 1-6. The value servo# = integer readServoPosition(2);
returned will be 0-180. Read the most recent commanded
Data Range: position of Servo 2.
servo# = 1 to 6

Data Type Returned:


value = integer (0 to 180)
Set Continuous Rotation (CR) State setCRServoState(CRservo#, state); setCRServoState(1, 100);
Sets the on/off and direction state of Spin CR1 servo continuously clockwise.
a CR servo connected to PRIZM CR1 Data Type: setCRServoState(1, 0);
and CR2 ports. A state parameter of CRservo# = integer Stop CR1 servo.
-100 equals spin counterclockwise. state = integer setCRServoState(1, -100);
A state parameter of 100 equals spin Spin CR1 servo continuously
clockwise. A state parameter of 0 is off Data Range: counterclockwise.
or stop. CRservo# parameter can be 1 CRservo# = 1 or 2
or 2 commanding either CR1 or CR2 state = -100, 0, 100
servo ports.

152 Appendix
TETRIX PRIZM Arduino Library Functions Cheat Sheet
Tip: When using these
Below is a reference of each function statement in the TETRIX PRIZM Robotics
functions in our code, be
Controller Library for Arduino.
sure to prefix each function
with the PRIZM object name
that we choose using the
PrizmBegin(); PRIZM myname statement.
PrizmEnd();
setRedLED(HIGH/LOW);
setGreenLED(HIGH/LOW);
setMotorPower(motor#, power);
setMotorPowers(power1, power2);
setMotorSpeed(motor#, speed);
setMotorSpeeds(speed1, speed2);
setMotorTarget(motor#, speed, target);
setMotorTargets(speed1, target1, speed2, target2);
setMotorDegree(motor#, speed, degrees);
setMotorDegrees(speed1, degrees1, speed2, degrees2);
setMotorInvert(motor#, invert);
readMotorBusy(motor#);
readMotorCurrent(motor#);
readEncoderCount(enc#);
readEncoderDegrees(enc#);
resetEncoder(enc#);
resetEncoders();
readLineSensor(port#);
readSonicSensorCM(port#);
readSonicSensorIN(port#);
readBatteryVoltage();
readStartButton();
setServoSpeed(servo#, speed);
setServoSpeeds(speed1, speed2, speed3, speed4, speed5, speed6);
setServoPosition(servo#, position);
setServoPositions(position1, position2, position3, position4, position5, position6);
readServoPosition(servo#);
setCRServoState(CRservo#, state);

Appendix 153
TETRIX PRIZM Sample Code Library
Example 1: GettingStarted_Act1_Blink_RedLED

/* PRIZM controller example program


* Blink the PRIZM red LED at a 1-second flash rate.
* author PWU on 08/05/2016
*/

#include <PRIZM.h> // include the PRIZM library

PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize the PRIZM controller

void loop() { // repeat this code in a loop

prizm.setRedLED(HIGH); // turn the red LED on


delay(1000); // wait here for 1,000 ms (1 second)
prizm.setRedLED(LOW); // turn the red LED off
delay(1000); // wait here for 1,000 ms (1 second)

Example 2: GettingStarted_Act2_Move_DCMotor

/* PRIZM controller example program


* Spin DC Motor Channel 1 for 5 seconds and then coast to a stop.
* After stopping, wait for 2 seconds and then spin in opposite direction.
* Continue to repeat until red Reset button is pressed.
* author PWU on 08/05/2016
*/

#include <PRIZM.h> // include the PRIZM library in the sketch


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize the PRIZM controller


}

void loop() { // repeat in a loop

prizm.setMotorPower(1,25); // spin Motor 1 CW at 25% power


delay(5000); // wait while the motor runs for 5 seconds
prizm.setMotorPower(1,0); // stop motor (coast to stop)
delay(2000); // after stopping, wait here for 2 seconds
prizm.setMotorPower(1,-25); // spin Motor 1 CCW at 25% power
delay(5000); // wait while the motor runs for 5 seconds
prizm.setMotorPower(1,0); // stop motor (coast to stop)
delay(2000); // after stopping, wait here for 2 seconds and then repeat

154 Appendix
Example 3: GettingStarted_Act3_Move_Servo

/* PRIZM controller example program


* This program sets the speed of Servo 1 to 25%.
* Servo 1 is then rotated back and forth between 0- and 180-degree range.
* author PWU on 08/05/2016
*/

#include <PRIZM.h> // include the PRIZM library in the sketch


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize the PRIZM controller


prizm.setServoSpeed(1,25); // set Servo 1 speed to 25%

void loop() { // repeat in a loop

prizm.setServoPosition(1,180); // rotate Servo 1 to 180 degrees


delay(3000); // wait for 3 seconds to give Servo 1 time
// to get to position 180
prizm.setServoPosition(1,0); // rotate Servo 1 to 0 degrees
delay(3000); // wait for 3 seconds to give Servo 1 time
// to get to position 0

Example 4: GettingStarted_Act4_Intro_LineFinder

/* PRIZM controller example program


* This program will read the digital signal of the Line Finder Sensor attached to digital port D3.
* If the sensor is facing a reflective surface and receiving a reflected IR beam, the PRIZM red LED
* will switch on. If the sensor is facing a dark surface or is too far away from a reflective surface,
* the red LED will be switched off.
*/

#include <PRIZM.h> // include the PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() { // this code runs once

prizm.PrizmBegin(); // initialize PRIZM

void loop() { // this code repeats in a loop

if(prizm.readLineSensor(3) == HIGH) {prizm.setRedLED(LOW);} // LED off

if(prizm.readLineSensor(3) == LOW) {prizm.setRedLED(HIGH);} // LED on

delay(50); // slow the loop down

Appendix 155
Example 5: GettingStarted_Act5_Intro_UltraSonic

/* PRIZM controller example program


* This example program will read the digital signal of the
* Ultrasonic Sensor attached to the digital sensor port D3.
* The distance in centimeters of an object placed in front
* of the sensor will be sent to the serial monitor window.
*/

#include <PRIZM.h> // include the PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() { // this code runs once

prizm.PrizmBegin(); // initialize PRIZM

Serial.begin(9600); // configure the serial monitor for 9600 baud rate

void loop() { // this code repeats in a loop

Serial.print(prizm.readSonicSensorCM(3)); // print the cm distance to the serial monitor

Serial.println(" Centimeters"); // print " Centimeters"

delay(200); // slow down the loop. wait for 200 ms to keep from printing to serial monitor too
fast
}

Example 6: TaskBot_Act7_Drive_Forward

/* PRIZM controller example program


* This program will move the PRIZM TaskBot forward for 3 seconds, stop, and end program.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM

prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1


// to harmonize the direction of opposite-facing drive motors
}

void loop() {

prizm.setMotorPowers(50,50); // turn Motors 1 and 2 on at 50% power


delay(3000); // wait here for 3 seconds while motors are spinning
prizm.PrizmEnd(); // end program and reset PRIZM

156 Appendix
Example 7: TaskBot_Act8_Drive_Circle

/* PRIZM controller example program


* This program will cause the PRIZM TaskBot to drive in a continuous circle.
* Press the red Reset button to stop the program.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM


prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1 to harmonize direction

void loop() {

prizm.setMotorPowers(50,25); // spin Motor 1 at 50% power and Motor 2 at 25% power,


// resulting in the robot driving in a clockwise circle
}

Appendix 157
Example 8: TaskBot_Act9_Drive_Square_1

/* PRIZM controller example program


* This program will move the PRIZM TaskBot in a square driving pattern.
* author PWU 08/05/2016
*/
#include <PRIZM.h> // include PRIZM library
PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {
prizm.PrizmBegin(); // initialize PRIZM
prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1 to harmonize the direction
}
void loop() {
prizm.setMotorPowers(50,50); // go forward at 50% power
delay(3000); // wait here for 3 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,-50); // make a right turn
delay(600); // wait here for 0.6 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,50); // go forward at 50% power
delay(3000); // wait here for 3 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,-50); // make a right turn
delay(600); // wait here for 0.6 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,50); // go forward at 50% power
delay(3000); // wait here for 3 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,-50); // make a right turn
delay(600); // wait here for 0.6 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.setMotorPowers(50,50); // go forward at 50% power
delay(3000); // wait here for 3 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second
prizm.PrizmEnd(); // end program and reset PRIZM
}

158 Appendix
Example 9: TaskBot_Act10_Drive_Square_2

/* PRIZM controller example program


* This program will move the TaskBot in a square driving pattern using a forward and right turn function.
* author PWU 08/05/2016
*/
#include <PRIZM.h> // include PRIZM library
PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM


prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1 to harmonize direction

void loop() {

for(int x=0; x<=3; x++) { // do this four times, increment x by +1


forward();
rightTurn();
}
prizm.PrizmEnd();
}

void forward() { // function to go forward


prizm.setMotorPowers(50,50); // go forward at 50% power
delay(3000); // wait here for 3 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second

void rightTurn() { // function for a right turn


prizm.setMotorPowers(50,-50); // make a right turn
delay(600); // wait here for 0.6 seconds while motors spin
prizm.setMotorPowers(125,125); // stop both motors in brake mode
delay(1000); // wait here for 1 second

Appendix 159
Example 10: TaskBot_Act11_Drive_To_Line

/* PRIZM controller example program


* This program will move the PRIZM TaskBot forward on a white surface until it detects a black
* line. When the line is detected, the robot will stop.
* Connect the Line Finder Sensor to digital port D3.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM

prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1


// to harmonize the direction of
// opposite-facing drive motors
}

void loop() {

if(prizm.readLineSensor(3) == 0) { // when sensor is receiving reflected light beam


prizm.setMotorPowers(35,35); // turn Motors 1 and 2 on at 35% power if light beam is detected

if(prizm.readLineSensor(3) == 1) { // when sensor detects black stripe


prizm.setMotorPowers(125,125); // stop motors in brake mode if black line is detected

while(1) { // infinite loop stays locked in this loop until Reset is pressed
prizm.setRedLED(HIGH); // flash PRIZM red LED on and off until reset
delay(500);
prizm.setRedLED(LOW);
delay(500);
}
}
}

160 Appendix
Example 11: TaskBot_Act12_Follow_A_Line

/* PRIZM controller example program


* This program implements line following with the PRIZM TaskBot. Using the Line Finder Sensor
* on sensor port D3, the robot will follow the edge of a black stripe on a white surface.
* The program will power one motor at a time, resulting in back-and-forth forward motion to
* keep the robot traversing the line edge.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM

prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1


// to harmonize the direction of
// opposite-facing drive motors
}

void loop() {

// beam reflected, no line detected


if(prizm.readLineSensor(3) == 0) {prizm.setMotorPowers(125,30); prizm.setRedLED(LOW);}

// no reflected beam, line detected


if(prizm.readLineSensor(3) == 1) {prizm.setMotorPowers(30,125); prizm.setRedLED(HIGH);}

Appendix 161
Example 12: TaskBot_Act13_Drive_To_Wall

/* PRIZM controller example program


* This program uses the Ultrasonic Sensor connected to
* sensor port D4 to detect an obstacle in its driving path
* within 25 cm. When detected, the robot will stop and wait
* for the object blocking its path to be cleared.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM

prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1


// to harmonize the direction of
// opposite-facing drive motors
}

void loop() {

if(prizm.readSonicSensorCM(4) > 25)


{
prizm.setMotorPowers(50,50); // if distance greater than 25 cm, do this
}
else
{
prizm.setMotorPowers(125,125); // if distance less than 25 cm, do this
}
}

162 Appendix
Example 13: TaskBot_Act14_Avoid_Obstacle

/* PRIZM controller example program


* This program uses the Ultrasonic Sensor connected to sensor port D4 to detect objects in its
* driving path. When an object is detected, the robot will stop, back up, make a right turn, and continue.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM

prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1


// to harmonize the direction of
// opposite-facing drive motors
}

void loop() {

if(prizm.readSonicSensorCM(4) > 25) // obstacle sense range set at 25 centimeters


{
prizm.setMotorPowers(35,35); // forward while no obstacle detected
prizm.setRedLED(LOW); // turn off red LED
prizm.setGreenLED(HIGH); // turn on green LED
}
else
{
prizm.setGreenLED(LOW); // turn off green LED
prizm.setRedLED(HIGH); // detected obstacle, turn on red LED
prizm.setMotorPowers(125,125); // stop, obstacle detected
delay(500);
prizm.setMotorPowers(-35,-35); // back up
delay(1000);
prizm.setMotorPowers(125,125); // stop
delay(500);
prizm.setMotorPowers(35,-35); // make a right turn
delay(500);

}
}

Appendix 163
Example 14: TaskBot_Act15_Combining_Sensors

/* PRIZM controller example program


* This program uses the Line Finder and the Ultrasonic Sensors at the same time.
* The robot will follow the edge of a black line (stripe) on a white surface and scan for an object
* in its path. When an obstacle is detected, it will wait for the obstacle to be cleared and then continue.
* The line sensor is connected to digital port D4. The Ultrasonic Sensor is connected to
* digital port D4. The red LED shows the line sensor status. The green LED shows the
* sonic Sensor status.
* author PWU 08/05/2016
*/

#include <PRIZM.h> // include PRIZM library


PRIZM prizm; // instantiate a PRIZM object prizm so we can use its functions

void setup() {

prizm.PrizmBegin(); // initialize PRIZM


prizm.setMotorInvert(1,1); // invert the direction of DC Motor 1
// to harmonize the direction of
// opposite-facing drive motors
prizm.setServoSpeed(1,50); // set Servo 1 speed to 50
}

void loop() {
if(prizm.readLineSensor(3) == 1) {
prizm.setMotorPowers(30,125); // line detected
prizm.setRedLED(HIGH);
}
else
{
prizm.setMotorPowers(125,30); // no line detected
prizm.setRedLED(LOW);
}

while(prizm.readSonicSensorCM(4) < 25) { // object is in path, loop here until cleared


prizm.setGreenLED(HIGH); // turn on green LED
prizm.setMotorPowers(125,125); // stop, obstacle detected
prizm.setServoPosition(1,0); // raise detection flag!
}

prizm.setGreenLED(LOW); // turn off green LED


prizm.setServoPosition(1,90); // lower flag position
}

164 Appendix
TETRIX PRIZM Robotics Controller
Programming Guide

Call Toll-Free Visit Us Online at


8008350686 TETRIXrobotics.com

You might also like