• Using 3 sensors, one for red, one for green, one for blue. Measuring up to 25 cm (full scale will be 2.55 meters) otherwise set levels to 0

    var distanceR = 0;
    var distanceG = 0;
    var distanceB = 0;
    
    var running;
    
    var sensorR = require("HC-SR04").connect(A0,A1,function(dist) {
      distanceR = dist;
    });
    var sensorG = require("HC-SR04").connect(A2,A3,function(dist) {
      distanceG = dist;
    });
    var sensorB = require("HC-SR04").connect(A4,A5,function(dist) {
      distanceB = dist;
    });
    
    SPI2.setup({baud:3200000, mosi:B15});
    
    function runIt(){
      sensorR.trigger();
      sensorG.trigger();
      sensorB.trigger();
      console.log("RGB("+Math.floor(distanceR)+","+Math.floor(distanceG)+","+Math.floor(distanceB)+")");
      
      if(distanceR > 25){distanceR = 0;} else {distanceR = distanceR*10;}
      if(distanceG > 25){distanceG = 0;} else {distanceG = distanceG*10;}
      if(distanceB > 25){distanceB = 0;} else {distanceB = distanceB*10;}
        SPI2.send4bit([distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB,distanceR,distanceG,distanceB], 0b0001, 0b0011); // white
    
    }
    
    function stopIt(){
      console.log("button pressed");
      clearInterval(running);
    }
    
    running = setInterval(runIt, 100);
    
    setWatch(stopIt, BTN1, {repeat:true, edge:"rising"});
    
About

Avatar for Rek @Rek started