• working with a greater distance, firing sensors individually, more stable output:

    1. var distanceR = 0;
    2. var distanceG = 0;
    3. var distanceB = 0;
    4. var running;
    5. var c=0;
    6. var sensorR = require("HC-SR04").connect(A0,A1,function(dist) {
    7. if (dist < 300 && dist > 0) distanceR = dist;
    8. });
    9. var sensorG = require("HC-SR04").connect(A2,A3,function(dist) {
    10. if (dist < 300 && dist > 0) distanceG = dist;
    11. });
    12. var sensorB = require("HC-SR04").connect(A4,A5,function(dist) {
    13. if (dist < 300 && dist > 0) distanceB = dist;
    14. });
    15. SPI2.setup({baud:3200000, mosi:B15});
    16. function runIt(){
    17. if (c===0) sensorR.trigger();
    18. if (c===1) sensorG.trigger();
    19. if (c===2) {
    20. sensorB.trigger();
    21. sendToLeds();
    22. c=0;
    23. } else c++;
    24. }
    25. function sendToLeds(){
    26. //console.log("before("+Math.floor(distanceR)+","+Math.floor(distanceG)+","+Math.floor(distanceB)+")");
    27. if(distanceR > 100 || distanceR === 0){distanceR = 0;} else {distanceR = 255-(distanceR/4)*10;}
    28. if(distanceG > 100 || distanceG === 0){distanceG = 0;} else {distanceG = 255-(distanceG/4)*10;}
    29. if(distanceB > 100 || distanceB === 0){distanceB = 0;} else {distanceB = 255-(distanceB/4)*10;}
    30. if(Math.floor(distanceR) > 0 || Math.floor(distanceG) > 0 || Math.floor(distanceB) > 0) console.log("RGB("+Math.floor(distanceR)+","+Math.floor(distanceG)+","+Math.floor(distanceB)+")");
    31. 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
    32. }
    33. function stopIt(){
    34. console.log("button pressed");
    35. clearInterval(running);
    36. }
    37. running = setInterval(runIt, 100);
    38. setWatch(stopIt, BTN1, {repeat:true, edge:"rising"});
About

Avatar for Rek @Rek started