Code Solution for Volume 8, Page 19

In this challenge, help Volta and Reggie put the pieces of code back together.  Using the circuit on Page 18, you will be able to measure the voltage produced by the solar cell and light up the RGB LED accordingly.

You can change the various thresholds to suit your needs.  The current code will:

  • Light up Green if more than 600 (more than 3V or coin cell battery)
  • Light up Blue if more than 300 (1.5V or AA battery)
  • Light up Red if less than 300.
var five = require('johnny-five');
var board = new five.Board();
//--------------------------------------------
board.on('ready', function() {
//--------------------------------------------
  var green = new five.Led(10);
  var red   = new five.Led(9);
  var blue  = new five.Led(11);
//--------------------------------------------
  var solar = new five.Sensor( { pin:"A0"
                               , freq:500 } );
//--------------------------------------------
  solar.on("change", function() {
//--------------------------------------------
    var val = this.value;
    console.log("solar value=" + val);
//--------------------------------------------
    if ( val > 600 ) {
      console.log("green");
      green.on(); red.off(); blue.off();
//--------------------------------------------
    } else if ( val > 300 ) {
      console.log("blue");
      green.off(); red.off(); blue.on();
//--------------------------------------------
    } else {
      console.log("red");
      green.off(); red.on(); blue.off();
    }
//--------------------------------------------
  });
//--------------------------------------------
});