Events

Events are predefined robot functions into which you can embed conditional logic. When an event occurs, the conditional logic is called and then the program returns to the main loop where it left off. The event will be called every time it occurs by default, unless you customize it. For example, "on collision, change LED lights to red and play the Collision sound," then return to the main loop.

Note: In the basic examples below the logic in the main loop is nested in a while 1 loop. This ensures there is logic to "return" to after an event is called. If there was longer time delayed logic in the main loop, you would not need the while 1 loop.

On Collision

5 collision

onCollision() executes conditional logic when the robot collides with an object.

async function onCollision() {
    // code to execute on collision
}
registerEvent(EventType.onCollision, onCollision);

For example, below is a basic pong program where Sphero bounces off walls, or your hands/feet in perpetuity. Place the robot on the floor between two parallel walls/objects and run the program with the robot pointed perpendicularly at one wall. On collision, the program will speak "collision" and change the LED's to red, then continue in the opposite direction:

async function onCollision() {
	stopRoll();
	setMainLed({ r: 255, g: 0, b: 0 });
	await speak("Collision", false);
	setHeading((getHeading() + 180));
	await delay(0.5);
	setMainLed({ r: 255, g: 22, b: 255 });
	setSpeed(100);
}
registerEvent(EventType.onCollision, onCollision);
async function startProgram() {
	setMainLed({
		r: 255,
		g: 255,
		b: 255
	});
	setSpeed(100);
}

On Freefall

5 freefall

onFreeFall() executes conditional logic when gravity is the only force acting on the robot, such as when dropping or throwing it. Freefall is measured by an accelerometer reading of < 0.1g for => 0.1s, where 1g is resting. On earth, objects in freefall accelerate downwards at 9.81 m/s². If you are in orbit, objects appear to be at rest with a reading of 0g because they (and you) are always in freefall, but they never hit the Earth.

async function onFreefall() {
    // code to execute on freefall
}
registerEvent(EventType.onFreefall, onFreefall);


For example, to speak "freefall" and change the LED's to red on freefall use:

async function onFreefall() {
	setMainLed({ r: 255, g: 0, b: 0 });
	speak("freefall", false);
	await delay(0.5);
}
registerEvent(EventType.onFreefall, onFreefall);

async function startProgram() {
	while (true) {
		setMainLed({ r: 255, g: 255, b: 255 });
		await delay(0.1);
	}
}

On Land

5 landing

onLand executes conditional logic when the robot lands after an being in freefall. You don't need to define an onFreeFall event for the robot to experience an onLanding, but the robot must meet the conditions for freefall before land.

async function onLanding() {
    // code to execute on landing
}
registerEvent(EventType.onLanding, onLanding);


For example, to speak "landing" and change the LED's to green after landing use:

async function onLanding() {
	setMainLed({ r: 0, g: 255, b: 0 });
	speak("land", false);
	await delay(0.5);
}
registerEvent(EventType.onLanding, onLanding);

async function startProgram() {
	while (true) {
		setMainLed({ r: 255, g: 255, b: 255 });
		await delay(0.1);
	}
}

On Gyromax

5 gyro max

onGyroMax() executes conditional logic when the robot exceeds the bounds of measurable rotational velocity of -2,000° - 2,000° per second. This can be triggered by spinning the robot around like a top on a table really fast. You need to spin it around > 5.5 revolutions per second.

async function onGyroMax() {
    // code to execute on gyromax
}
registerEvent(EventType.onGyroMax, onGyroMax);


For example, to speak "gyromax" and change the LED's to red when you reach gyromax, use:

async function onGyroMax() {
	setMainLed({ r: 255, g: 0, b: 0 });
	speak("gyromax", true);
	await delay(1.0);
}
registerEvent(EventType.onGyroMax, onGyroMax);

async function startProgram() {
	setStabilization(false);
	setBackLed(255.0);
	while (true) {
		setMainLed({ r: 255, g: 255, b: 255 });
		await delay(0.5);
	}
}

On Charging

5 charging

onCharging() executes conditional logic called when the robot starts charging its battery. This can be triggered by placing your robot in it's charging cradle, or by plugging it in.

async function onCharging() {
    // code to execute on charging
}
registerEvent(EventType.onCharging, onCharging);

On Not Charging

5 not charging

onNotCharging() executes conditional logic called when the robot stops charging its battery. This can be triggered by removing your robot from it's charging cradle, or unplugging it.

async function onNotCharging() {
    // code to execute on charging
}
registerEvent(EventType.onCharging, onNotCharging);


For example, to have Sphero execute 3 different conditions for on, on charging, and on not charging, use the below.

async function onCharging() {
	setMainLed({ r: 6, g: 0, b: 255 });
	speak("charging", true);
	await delay(1.0);
	speak("remove me from my charger", true);
}
registerEvent(EventType.onCharging, onCharging);

async function onNotCharging() {
	setMainLed({ r: 255, g: 0, b: 47 });
	speak("not charging", true);
	await delay(2.0);
}
registerEvent(EventType.onNotCharging, onNotCharging);

async function startProgram() {
	speak("place me in my charger", true);
	while (true) {
		setMainLed({ r: 3, g: 255, b: 0 });
		await delay(0.5);
	}
}

On IR Message Received

on messsage

onIRMessage4(channel) executes conditional logic called when an infrared message is received on the specified channel. This can be triggered by one Sphero BOLT robot receiving a message from another Sphero BOLT. For example, to have Sphero BOLT change the matrix to red when receiving a message on channel 4:

var _messageChannels = [4];

async function startProgram() {
	listenForIRMessage(_messageChannels);
}

async function onIRMessage4(channel) {
	if (channel !== 4) return;

	setMainLed({ r: 255, g: 0, b: 0 });

	listenForIRMessage(_messageChannels);
}
registerEvent(EventType.onIRMessage, onIRMessage4);