Functions
Like in Algebra, a function has an input and an output. For example, lets create a function called 'Drive'.
We will input a argument called 'speed', and the output will be the change in speed of the robot. Below is
what this function would look like:
function Drive( speed )
setLeftMotor( speed )
setRightMotor( speed )
end
If we wanted to call the function 'Drive', it would look something like this:
Drive(100)
Note that function names are also case-sensitive: setLeftMotor(5) will work, but
setleftmotor(5) will not.
Variables
A variable is a stored value. When the code is run, the variable names will be substituted with the
current value. So if you have a line like:
j = 0
Any instance of j will be substituted with 0 when the code is running. The value of a variable can be
reassigned any time. You can, for example, use this to store the return value of a function.
j = getFrontRange()
Just like function names, variable names are case-sensitive.
Variables are usually tied to a specific function, but a ?global variable? is a variable that exists
outside of a specific function, and can be accessed or overwritten by any function in the program.
variable assignment:
(variable name) = (value)
if Statements
The if statement is a control structure you can use to change what code executes depending on your
data. The else will occur if the above if condition is not true. Note:
else are optional.
number = getFrontRange()
if number < 5 then
DriveBackwards()
else
DriveForward()
end
Lua Visual Specific functions
void setLeftMotor(int): This will set the motor to the speed of the first argument, where positive
values are forward and negative values are backwards.
void setRightMotor(int): Same as above, but for the right motor.
int getFrontRange(): will return the value of the front range-finding sensor. Use this in an if control
structure to determine if the robot should turn or reverse.
void ChangeState(int): will change the current state of the robot to the integer value given.
int getAngle(): returns the angle the robot is currently facing.