onBoardEvent()

Category:Circuit

Lets the board respond to inputs on the board such as buttons and the switch.

onBoardEvent() works very similar to onEvent() in App Lab, but responds to input from the left button, right button, toggle switch, sound sensor, light sensor, temperature sensor, and accelerometer components on the board. The buttons, switch, and sensors all have different events they can respond to.

Component(s) Events
buttonL and buttonR Pressed (When the button is pressed), Down (When the button is down), Up (When the button comes back up)
toggleSwitch Open (When the switch is flipped "Off"), Closed (When the switch is flipped "On")
All Sensors Change (Fired any time the input value of the specified sensor changes), Data ( Fired every 50 milliseconds)
accelerometer Single Tap Fires when a tap is detected next to the board, Double Tap Fires only when two taps within x amount of time of each other is detected next to the board

Examples

Event Example: toggleSwitch Lightswitch

onBoardEvent(toggleSwitch, "open", function(event) {
  led.off();
});
onBoardEvent(toggleSwitch, "close", function(event) {
  led.on();
});

Event Example: Buttons L and R

onBoardEvent(buttonL, "press", function(event) {
  buzzer.note("A4");
});
onBoardEvent(buttonR, "press", function(event) {
  buzzer.stop();
});

Event Example: tempSensor Thermometer

Continuously updates the temperature with the data event.

onBoardEvent(tempSensor, "data", function(event) {
  setNumber("temperature", tempSensor.F);
});

Event Example: lightSensor Color Changer

Consistently updates the the color of the screen based on the amount of light coming through.

onBoardEvent(lightSensor, "data", function(event) {
  lightSensor.setScale(0, 255);
  var light1 = lightSensor.value;
  lightSensor.setScale(100, 150);
  var light2 = lightSensor.value;
  lightSensor.setScale(0, 200);
  var light3 = lightSensor.value;
  setProperty("screen1", "background-color", rgb(light1,light2,light3));
});

Event Example: accelerometer Tilt Detector

Lights up the color LEDs based on how the board is being tilted.

var colors = "white";

onBoardEvent(accelerometer, "data", function(event) {
  var pitch = accelerometer.getOrientation("pitch");
  var inclination = accelerometer.getOrientation("inclination");
  var roll = accelerometer.getOrientation("roll");
  
  //front to back
  if (pitch < -10) {
    colorLeds[0].on();
    colorLeds[9].on();
  }
  if (pitch > 10) {
    colorLeds[4].on();
    colorLeds[5].on();
  }
  
  //side-to-side
  if (roll > 20) {
    colorLeds[7].on();
  } 
  if(roll < -20){
    colorLeds[2].on();
  }
});

Syntax

onBoardEvent(component, event, function(event) {...});

Parameters

NameTypeRequired?Description
component string Type of input that's being listened for (`toggleSwitch`, `buttonR`, `buttonL`, etc).
event string Name of the event to listen for ("pressed", "up", "down, etc).
callback function The callback function executed in response to an event for the matching component of the matching *event*.

Found a bug in the documentation? Let us know at documentation@code.org