Overview
JavaScript Functions
In addition to the built-in ECMA 5.1 JavaScript classes (outlined below), ARC includes several classes with methods to control your robot, read sensors, interact with the operating system, and more. To view built-in ARC JavaScript methods…
1) Check the intellisense checkbox.
2) Type one of the following class names, press ‘.’ and scroll through the available methods.
ADC – EZB analog to digital converter functions
Audio – streaming audio functions
COM – local serial COM port functions
Digital – EZB digital I/O functions
EZB – EZB specific functions for getting voltage and checking connection, etc.
File – writing and reading local files
I2C – EZB I2C helper functions
Movement – movement panel control of directions
Net – networking functions
Ping – EZB ultrasonic distance sensor functions
PWM – EZB pulse width modulation on digital I/O pins
Servo – EZB servo movement functions
UART – EZB hardware UART and software Serial functions
Utility – various utility functions
Supported JavaScript ECMA 5.1 Classes
The JavaScript ECMA 5.1 includes several classes for working with strings, math and various features. The following classes can be researched online at developer.mozilla.org and w3schools.com for detailed information and examples.
- Array
- Boolean
- Console (support for time, count, error, info, log)
- Date
- Error
- Iterator
- JSON
- Map
- Math
- Number
- Object
- Proxy
- Reflect
- RegExp
- Set
- String
- Symbol
Root Commands
Root commands do not belong to a class and are included for convenience to access global variables and send commands to other controls.
getVar( variableName, [default value] )
Retrieves the value from the Arc’s public global variable storage. These are variables that are published by robot skills, such as the Camera, Auto Position, Speech Recognition, etc..
{variableName} - The name of the global variable as a string
{default value} – [Optional] If specified, this value is returned if the global variable doesn’t exist.
{return} – The value of the global variable
Example:
// Get the current direction the robot is moving
var direction = getVar(“$Direction”);
// Get the value of $test, and if it doesn’t exist return false
var testVar = getVar(“$test”, false);
setVar( variableName, value )
Sets the value from of Arc’s public global variable storage. This allows the variable to be available to other controls or other scripts by using getVar().
{variableName} - The name of the global variable as a string
{value} – The value that you wish to store in the global variable
{return} – The value of the global variable
Example:
// Set a value of 5 to be accessible by other controls
setVar(“$MyValue”, 5);
varExists( variableName )
Check if the global variable exists.
{variableName} - The name of the global variable as a string
{return} – true/false Boolean if the variable exists
Example:
// print if the variable exists
print(varExists(“$MyValue”));
print( txt )
Writes the value to the console output.
{txt} – The value/data to print
Example:
// Print a string to the console output
print(“Hello World”);
sleep( timeMs )
Pauses the execution of the program for the specified time in milliseconds
{timeMS} – The time to pause
Example:
// Print a string, pause for 5 seconds and print another string
print(“Hello”);
sleep(5000);
print(“ World”);
sleepRandom( minTimeMS, maxTimeMS )
Pauses the execution of the program for a random milliseconds between the min and max.
{minTimeMS} – The minimum time to pause
{maxTimeMS} – The maximum time to pause
Example:
// Print a string, pause for random time and print another string
print(“Hello”);
sleepRandom(1000, 5000);
print(“ World”);
controlCommand( controlName, command, [parameters] )
Sends the command to the control name with the optional parameters. The cheat sheet displays all of the interrogated control commands supported by controls in the project.
Example:
// Start the camera
controlCommand(“Camera”, “StartCamera”);
print(“Camera has started”);
showControl( controlName )
Shows the user interface control. This also adds the control to the stack, which can be
Example:
// Start the camera
controlCommand(“Camera”, “StartCamera”);
print(“Camera has started”);
closeControl( controlName )
Closes the control on the user interface and displays the previous control in the display stack.
Example:
CloseControl();
Variables
JavaScript variables are private to the namespace of each script engine. That means a variable created in a JavaScript script does not exist in other JavaScript scripts. If you have variables declared in the JavaScript of the Camera control, the variables are not accessible in the JavaScript of the WiiMote control.
Public variables use ARC’s variable manager, which are global across all controls and compilers. Public variables are accessed using the getVar() and setVar() JavaScript commands.
Blockly generates JavaScript, which means the variables will be private by default. To be public, the variables can start with a $ (dollar sign).