Generic RobotC Programming Book
Generic RobotC Programming Book
RobotC
Programming
Book
A collection of random programs
and ramblings concerning RobotC.
Anonymous
Generic
RobotC
Programming
Book
There is no copyright for this work so I will put some placeholder text so
it looks like a normal book with a block of copyright stuffs.. Lorem ipsum
dolor sit amet, consectetur adipiscing elit. Ut lobortis gravida sem vel
fermentum. Duis ac nunc justo. Donec gravida mollis velit, eu lobortis
odio imperdiet ac. Donec magna odio, congue quis ullamcorper vel,
lobortis id diam. Etiam tempor massa ac sagittis suscipit. Duis vel nunc
vestibulum, feugiat nisl eget, gravida quam. Cras ullamcorper elit elit, et
volutpat nisl consequat sit amet. Donec molestie, orci sed pulvinar
egestas, ante neque feugiat magna, at faucibus nisl. Images in this book
are probably copyrighted in some way but I am too lazy to cite.
Contents
Introduction
Things You Should Know .............................................................. 7
The “Killswitch” Task ....................................................................... 8
Braces .................................................................................................... 9
Pseudocode ........................................................................................ 9
Semicolons .......................................................................................... 9
Creation of New Tasks ................................................................... 9
While loops ....................................................................................... 10
Errors ................................................................................................... 11
Motor and Sensor Setup ............................................................. 12
Program Examples
Program 1.......................................................................................... 14
Program 2.......................................................................................... 16
Program 3.......................................................................................... 18
Program 4.......................................................................................... 20
Program 5.......................................................................................... 22
Program 6.......................................................................................... 24
Conclusion ............................................................................................. 26
Introduction
Things You Should Know
Welcome to the world of in-school robotics, where
you get to build random contraptions with people you don’t
know and write code that contains lots of errors in a
language you have never heard of. The language here being
the “Natural Language PLTW” variant of RobotC. Here is a
picture of the logo in case you haven’t seen it yet.
7
The “Killswitch” Task
When programming, you must remember to always
insert the following code into the beginning of your
program:
8
Braces
You should remember to have the same amount of
closing braces and opening braces.
Pseudocode
You may have noticed the text from the image above
contains text in the same line of the command that begins
with two back slashes. This is known as pseudocode, and it
explains what each command does. Simply press tab a few
times after the command, add two backslashes, and you can
type text that will be ignored by the “VEX ARM® Cortex®-
based Microcontroller”. You are recommended to add
pseudocode to any program you create as it makes it easier
to diagnose and evaluate by others.
Semicolons
Semicolons at the end of each command separate the
statements in your code. You do not use them when defining
tasks or creating while loops.
9
adding a space. Here is an example of what it should look
like:
While loops
While loops are used when you want a certain bit of
code to run in a loop only when a certain condition is met.
Here is an example of a while loop.
10
Errors
In the event that there are errors in your code and you
are trying to compile it, details concerning your errors
appear in the error log. They look like this:
11
Motor and Sensor Setup
To clarify how to use the Motor and Sensor Setup, I
will provide a screenshot.
12
13
Program Examples
Program 1
Write a program that will start the right and left motors at a
speed of 50 and run them for 2 seconds, then stop the
motors and reverse them both at a speed of 50 for 3 seconds
and then stop both.
14
The start of this program causes the Killswitch task to
run and causes the left and right motor of a typical VEX
testbed to start turning at a speed of 50 (see lines 15-17).
After 2 seconds, both motors stop and 0.5 seconds are
allowed for the motors to come to a complete stop (see lines
18-21). The motors start up again running backwards at a
speed of 50, and then stop again after 3 seconds (see lines
22-26). The program then ends.
15
Program 2
Write a program that will do nothing until the bump switch is
pushed and then both motors will run at a speed of 120.
16
Pressing the bump switch starts the Killswitch task and
then starts both left and right motors at a speed of 120 (lines
16-20). The motors will continue running until either the
battery runs out, the Killswitch task is triggered, the “VEX
ARM® Cortex®-based Microcontroller” is shut off, or the
program is manually stopped on the computer where the
“ROBOTC for VEX Robotics 4.x” IDE is running.
17
Program 3
Write a program that will start the left motor at a speed of 75
if the limit switch is pushed.
18
Upon pressing the limit switch, the Killswitch task is
started and the left motor will start running at a speed of 75
(lines 15-18). Like before, the motor will continue running
until the battery runs out or the user manually stops the
program as there is no command telling the program to stop
or the motor to stop.
19
Program 4
Write a program that will turn on the right motor at a speed
of 25 if the potentiometer value goes above 1,000 and will
have the right motor run at a speed of 100 if the
potentiometer value goes above 2000.
20
This program will cause the right motor to run at a
speed of 100 if the potentiometer value is greater than 2,000
(lines 19-22). If the potentiometer value drops below 2,000
but stays above 1,000, the right motor will start running at a
speed of 25 (lines 23-27). If the potentiometer goes below
1,000, the right motor stops running (line 28), but will start
running again once the value becomes higher than 1,000
because of the while loop surrounding all the other while
loops (lines 17-29). This behavior will stop once the program
is halted.
21
Program 5
Write a program that will start the right and left motors at a
speed of 50 until the line follower value goes below 500,
then both motors will stop.
22
Starting this program will launch the Killswitch task, then start
the left and right motors at a speed of 50 if the line follower value is 500
or higher (lines 20-24). If the line follower value drops below 500, both
motors will stop (lines 25-26). Once the value reaches 500 and higher,
the motors will start running again due to the loop
(lines 18-27).
23
Program 6
Create a while loop and explain how it works with a program
that will start both motors at a speed of 35 when a bump
switch is pushed and they will just keep running until the
bump switch is pushed again.
24
Once the program is started, it will wait for the bump
switched to be activated, and then start the Killswitch task
and the while loop (lines 17-20). The while loop starts the left
and right motor at a speed of 35 (lines 22-23), and if the
bump switch is ever activated again (line 24), it will stop all
the motors from running and then end the program (lines
26-28).
25
Conclusion
After looking through this book you should now have
a basic idea about how to program using ROBOTC Natural
Language PLTW. If you have any questions, you should
consider using this large and vast resource that costs $50 a
month called the Internet. And in this magical space there is
a thing called “Google” that will give you buttons that
teleport you to other remarkable places full of blocks of text
where people discuss various things using this unique
method of communication called “English” (known as
“forums”). Among these walls of text may be the answer to
your question. Otherwise, they may be hidden inside
electronically formatted books (or Portable Document
Format, or PDF) or digital presentations created by either
VEX or instructors who falsely believe presentations are the
best way to teach a class.
26
27
Notes
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
28
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
29
This book will show you
examples of ROBOTC so you
can figure out how to code by
yourself!
-A Reader