Prizm Programming Guide
Prizm Programming Guide
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.
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
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.
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
Motor:
A machine that produces motion or power for completing work
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
4 Getting Started
PRIZM Setup
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.
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.
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.
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.
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.
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.
Parts Needed
PRIZM controller
USB cable
A charged TETRIX 12-Volt
Rechargeable NiMH Battery Pack
PRIZM Controller On/Off Battery
Switch Adapter
Computer
Figure 6
Figure 7
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.
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
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.
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
Figure 11
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.
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
Figure 12
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 14
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
Figure 15
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.
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.
Structural Elements
32 mm Channel 39065
96 mm Channel 39066
L Bracket 39062
Flat 64 mm x 16 mm 39274
Flat 96 mm x 16 mm 39273
Detail
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.
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!
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.
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.
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
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
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.
Step 1.1
Step 1.3
Step 1.4
Step 1.6
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
Step 2.0
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
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
Step 3.1
Step 3.3
Step 3.5
Step 3.7
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
Tip: See the setup tips on page 48 for help distinguishing between the Motor Hub and Axle Hub.
Step 4.1
Step 4.3
Step 4.7
Step 4.9
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
Step 5.1
Step 5.3
Step 5.5
8x 8x
Socket Head Cap Screw Kep Nut 39094
6-32 x 5/16" 39098
Step 6.1
Step 6.3
1x 4x 4x
PRIZM controller 43000 Socket Head Cap Screw Kep Nut 39094
6-32 x 1/2" 39097
Step 7.1
Step 7.3
Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer
Figure 20
Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer
Figure 21
Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer
Figure 22
Parts Needed
Fully assembled PRIZM TaskBot
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer
Figure 23
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
Step 1.1
Step 1.3
Step 1.5
2x
Socket Head Cap Screw
6-32 x 5/16" 39098
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
Figure 24
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
Figure 25
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?
Parts Needed
Fully assembled PRIZM TaskBot complete
with sensor module
USB cable
A charged TETRIX 12-Volt Rechargeable
NiMH Battery Pack
Computer
Figure 26
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
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
Figure 27
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
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
Step 1.1
Step 1.3
Step 1.5
Step 1.7
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
Figure 28
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.
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.)
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)
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.
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.
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.
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.
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.
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
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
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)
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
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.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
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
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.
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.
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.
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
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 prizm; // instantiate a PRIZM object prizm so we can use its functions
void setup() {
Example 2: GettingStarted_Act2_Move_DCMotor
void setup() {
154 Appendix
Example 3: GettingStarted_Act3_Move_Servo
void setup() {
Example 4: GettingStarted_Act4_Intro_LineFinder
Appendix 155
Example 5: GettingStarted_Act5_Intro_UltraSonic
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
void setup() {
void loop() {
156 Appendix
Example 7: TaskBot_Act8_Drive_Circle
void setup() {
void loop() {
Appendix 157
Example 8: TaskBot_Act9_Drive_Square_1
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
void setup() {
void loop() {
Appendix 159
Example 10: TaskBot_Act11_Drive_To_Line
void setup() {
void loop() {
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
void setup() {
void loop() {
Appendix 161
Example 12: TaskBot_Act13_Drive_To_Wall
void setup() {
void loop() {
162 Appendix
Example 13: TaskBot_Act14_Avoid_Obstacle
void setup() {
void loop() {
}
}
Appendix 163
Example 14: TaskBot_Act15_Combining_Sensors
void setup() {
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);
}
164 Appendix
TETRIX PRIZM Robotics Controller
Programming Guide