Methods
(async) buzz(sec, freq)
Asynchronously runs the robot's buzzer at freq hertz for sec seconds.
Parameters:
Name | Type | Description |
---|---|---|
sec |
number | Time in seconds. |
freq |
number | Frequency in hertz. |
Example
// Play an A4 note (440 Hz) for one second.
await buzz(1, 440);
(async) get_acceleration() → {Promise.<Object>}
Returns a promise that resolves with the status of the acceleration sensor.
as an object.
Returns:
Status of the acceleration sensor.
- Type
- Promise.<Object>
Example
// Move forward if the robot is being tapped.
const accel = await get_acceleration();
if (accel.tap) {
await wheels(255, 255);
}
(async) get_light() → {Promise.<Array.<number>>}
Returns a promise that resolves with the status array of the light sensors.
The first element is the intensity of the light on the left sensor, and the
second element is the intensity of the light on the right sensor. Each
value is from 0 (full darkness) to 255 (full brightness).
Returns:
Status array of the light sensors.
- Type
- Promise.<Array.<number>>
(async) get_obstacles() → {Promise.<Array.<boolean>>}
Returns a promise that resolves with the status array of the obstacle
sensors. The first element is the status of the left obstacle sensor and the
second element is the status of the right obstacle sensor. Each value is
either false (no obstacle) or true (obstacle)
Returns:
The status array of the obstacle sensors.
- Type
- Promise.<Array.<boolean>>
Example
// Turn the LED red if there is an obstacle, otherwise green.
const obstacles = await get_obstacles();
if (obstacles.every(x => x)) {
await led(0, 255, 0);
} else {
await led(255, 0, 0);
}
(async) get_temperature() → {Promise.<number>}
Returns a promise that resolves with the temperature detected by the robot
in degrees Celsius.
Returns:
Temperature in degrees Celsius
- Type
- Promise.<number>
Example
// Print the temperature in degrees Fahrenheit.
const temp_c = await get_temperature();
console.log(temp_c * 1.8 + 32);
(async) halt()
Asynchronously halts the robot and stops all commands currently running on
the robot.
Example
// Move at full speed forward for one second.
await wheels(255, 255);
await wait(1);
await halt();
(async) led(r, g, b)
Sets the LED light to the color with the RGB value passed. Each component of
the RGB value is in the range of 0-255.
Parameters:
Name | Type | Description |
---|---|---|
r |
number | Red component of the RGB value. |
g |
number | Green component of the RGB value. |
b |
number | Blue component of the RGB value. |
Example
// Set the LED to magenta (RGB value 255, 0, 255).
await led(255, 0, 255);
run(prog)
Wraps an async function to assure the Finch robot is connected before and
disconnected after any code runs. Because of browser security restrictions,
this function call only be called in response to a user gesture. For
example, in the `click` event handler of a button.
Parameters:
Name | Type | Description |
---|---|---|
prog |
run_callback | An asynchronous function callback. |
Example
// Running both wheels at full speed for one second, then close the
// connection.
run(async function() {
await wheels(255, 255);
await wait(1);
});
(async) wait(sec)
Returns a promise that resolves after sec seconds.
Parameters:
Name | Type | Description |
---|---|---|
sec |
number | Time in seconds to resolve after |
Example
// Print the second string one second after the first;
console.log('string 1');
await wait(1);
console.log('string 2')
(async) wheels(left, right)
Asynchronously sets speed of each wheel. 0 is not moving, while 255 is full
speed.
Parameters:
Name | Type | Description |
---|---|---|
left |
number | Speed of the left wheel from 0 to 255. |
right |
number | Speed of the right wheel from 0 to 255. |
Example
// Turn clockwise by setting the left wheel to full speed.
await wheels(255, 0);
Type Definitions
(async) run_callback()
A callback to be run from run()