colorLeds[i].on()

Category:Circuit

Turns the specified colorLED on.

This method works just like led.on() and will keep whichever LED is specified on. You can turn the colorLED off with colorLeds[i].off().

By default, colorLeds[i].on() turns on all three parts of the LED (red, green, and blue) to produce white. Use colorLeds[i].color() to change the color. colorLeds[i].color() does not need to be used with the on method, it automatically turns the LED(s) on.

Examples

onBoardEvent(buttonL, "down", function(event) {
  colorLeds[0].on();
  colorLeds[9].on();
});
onBoardEvent(buttonR, "down", function(event) {
  colorLeds[0].off();
  colorLeds[9].off();
});

Alternating Lights

onBoardEvent(buttonL, "down", function(event) {
  colorLeds[0].on();
  colorLeds[1].off();
  colorLeds[2].on();
  colorLeds[3].off();
  colorLeds[4].on();
  colorLeds[5].off();
  colorLeds[6].on();
  colorLeds[7].off();
  colorLeds[8].on();
  colorLeds[9].off();
});

onBoardEvent(buttonR, "down", function(event) {
  colorLeds[0].off();
  colorLeds[1].on();
  colorLeds[2].off();
  colorLeds[3].on();
  colorLeds[4].off();
  colorLeds[5].on();
  colorLeds[6].off();
  colorLeds[7].on();
  colorLeds[8].off();
  colorLeds[9].on();
});

Alternating Lights with For Loops

Does the same thing as the previous Alternating Lights example, but this time uses for loops.

onBoardEvent(buttonL, "down", function(event) {
  for (var i = 0; i < 10; i = i+2){
    colorLeds[i].on();
  }
  for (var i = 1; i < 10; i = i+2){
    colorLeds[i].off();
  }
});

onBoardEvent(buttonR, "down", function(event) {
  for (var i = 0; i < 10; i = i+2){
    colorLeds[i].off();
  }
  for (var i = 1; i < 10; i = i+2){
    colorLeds[i].on();
  }
});

Syntax

colorLeds[index].on()

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