Advertisement
Guest User

chameleon lamp

a guest
Mar 11th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var linino = require('ideino-linino-lib'),
  2.  
  3.     board = new linino.Board();
  4.  
  5. var lifx = require('lifx');
  6.  
  7. var lx = lifx.init();
  8.  
  9. // Define colour sensor LED pins
  10.  
  11. var ledArray = [2, 3, 4];
  12.  
  13. // boolean to know if the balance has been set
  14.  
  15. var balanceSet = false;
  16.  
  17. //place holders for colour detected
  18.  
  19. var red = 0;
  20.  
  21. var green = 0;
  22.  
  23. var blue = 0;
  24.  
  25. //floats to hold colour arrays
  26.  
  27. var colourArray = [0, 0, 0];
  28.  
  29. var whiteArray = [0, 0, 0];
  30.  
  31. var blackArray = [0, 0, 0];
  32.  
  33. //place holder for average
  34.  
  35. var avgRead;
  36.  
  37. //delay
  38.  
  39. function sleep(time, callback) {
  40.  
  41.     var stop = new Date().getTime();
  42.  
  43.     while (new Date().getTime() < stop + time) {;
  44.  
  45.     }
  46.  
  47.     callback();
  48.  
  49. }
  50.  
  51. //convert RGB to HSV
  52.  
  53. function rgb2hsv(r, g, b) {
  54.  
  55.     r /= 255, g /= 255, b /= 255;
  56.  
  57.     var rr, gg, bb,
  58.  
  59.         h, s,
  60.  
  61.         v = Math.max(r, g, b),
  62.  
  63.         diff = v - Math.min(r, g, b),
  64.  
  65.         diffc = function (c) {
  66.  
  67.             return (v - c) / 6 / diff + 1 / 2;
  68.  
  69.         };
  70.  
  71.     if (diff == 0) {
  72.  
  73.         h = s = 0;
  74.  
  75.     } else {
  76.  
  77.         s = diff / v;
  78.  
  79.         rr = diffc(r);
  80.  
  81.         gg = diffc(g);
  82.  
  83.         bb = diffc(b);
  84.  
  85.         if (r === v) {
  86.  
  87.             h = bb - gg;
  88.  
  89.         } else if (g === v) {
  90.  
  91.             h = (1 / 3) + rr - bb;
  92.  
  93.         } else if (b === v) {
  94.  
  95.             h = (2 / 3) + gg - rr;
  96.  
  97.         }
  98.  
  99.         if (h < 0) {
  100.  
  101.             h += 1;
  102.  
  103.         } else if (h > 1) {
  104.  
  105.             h -= 1;
  106.  
  107.         }
  108.  
  109.     }
  110.  
  111.     return [h, s, v];
  112.  
  113. }
  114.  
  115. board.connect(function () {
  116.  
  117.     //setup the outputs for the colour sensor
  118.  
  119.     board.pinMode("D2", "OUTPUT");
  120.  
  121.     board.pinMode("D3", "OUTPUT");
  122.  
  123.     board.pinMode("D4", "OUTPUT");
  124.  
  125.     lx.lightsOn();
  126.  
  127.     setInterval(function () {
  128.  
  129.         checkBalance();
  130.  
  131.         checkColour();
  132.  
  133.         printColour();
  134.  
  135.     }, 1000);
  136.  
  137.     function checkBalance() {
  138.  
  139.         //check if the balance has been set, if not, set it
  140.  
  141.         if (balanceSet === false) {
  142.  
  143.             setBalance();
  144.  
  145.         }
  146.  
  147.     }
  148.  
  149.     function setBalance() {
  150.  
  151.         //set white balance
  152.  
  153.         sleep(5000, function () {});
  154.  
  155.         //delay for five seconds, this gives us time to get a white sample in front of our sensor
  156.  
  157.         //scan the white sample.
  158.  
  159.         //go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array
  160.  
  161.         for (var i = 0; i <= 2; i++) {
  162.  
  163.             board.digitalWrite("D" + ledArray[i].toString(), 1);
  164.  
  165.             sleep(100, function () {});
  166.  
  167.             getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well.
  168.  
  169.             whiteArray[i] = avgRead;
  170.  
  171.             board.digitalWrite("D" + ledArray[i].toString(), 0);
  172.  
  173.             sleep(100, function () {});
  174.  
  175.         }
  176.  
  177.         sleep(5000, function () {});
  178.  
  179.         //done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample.
  180.  
  181.         //set black balance
  182.  
  183.         //wait for five seconds so we can position our black sample
  184.  
  185.         //go ahead and scan, sets the colour values for red, green, and blue when exposed to black
  186.  
  187.         for (var j = 0; j <= 2; j++) {
  188.  
  189.             board.digitalWrite("D" + ledArray[j].toString(), 1);
  190.  
  191.             sleep(100, function () {});
  192.  
  193.             getReading(5);
  194.  
  195.             blackArray[j] = avgRead;
  196.  
  197.             //blackArray[i] = analogRead(2);
  198.  
  199.             board.digitalWrite("D" + ledArray[j].toString(), 0);
  200.  
  201.             sleep(100, function () {});
  202.  
  203.         }
  204.  
  205.         //set boolean value so we know that balance is set
  206.  
  207.         balanceSet = true;
  208.  
  209.         sleep(5000, function () {});
  210.  
  211.         //delay another 5 seconds to allow the human to catch up to what is going on
  212.  
  213.     }
  214.  
  215.     function checkColour() {
  216.  
  217.         for (var i = 0; i <= 2; i++) {
  218.  
  219.             board.digitalWrite("D" + ledArray[i].toString(), 1); //turn or the LED, red, green or blue depending which iteration
  220.  
  221.             sleep(100, function () {});
  222.  
  223.             getReading(5); //take a reading however many times
  224.  
  225.             colourArray[i] = avgRead; //set the current colour in the array to the average reading
  226.  
  227.             var greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between
  228.  
  229.             colourArray[i] = (colourArray[i] - blackArray[i]) / (greyDiff) * 255; //the reading returned minus the lowest value divided by the possible range multiplied by 255 will give us a value roughly between 0-255 representing the value for the current reflectivity(for the colour it is exposed to) of what is being scanned
  230.  
  231.             board.digitalWrite("D" + ledArray[i].toString(), 0); //turn off the current LED
  232.  
  233.             sleep(100, function () {});
  234.  
  235.         }
  236.  
  237.     }
  238.  
  239.     function getReading(times) {
  240.  
  241.         var reading;
  242.  
  243.         var tally = 0;
  244.  
  245.         //take the reading however many times was requested and add them up
  246.  
  247.         for (var i = 0; i < times; i++) {
  248.  
  249.             reading = board.analogRead("A0");
  250.  
  251.             tally = reading + tally;
  252.  
  253.             sleep(100, function () {});
  254.  
  255.         }
  256.  
  257.         //calculate the average and set it
  258.  
  259.         avgRead = (tally) / times;
  260.  
  261.     }
  262.  
  263.     //prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works.
  264.  
  265.     function printColour() {
  266.  
  267.         var r, g, b;
  268.  
  269.         if (colourArray[0] > 255) r = 255
  270.  
  271.         else if (colourArray[0] < 0) r = 0
  272.  
  273.         else r = parseInt(colourArray[0])
  274.  
  275.         if (colourArray[1] > 255) g = 255
  276.  
  277.         else if (colourArray[1] < 0) g = 0
  278.  
  279.         else g = parseInt(colourArray[1])
  280.  
  281.         if (colourArray[2] > 255) b = 255
  282.  
  283.         else if (colourArray[2] < 0) b = 0
  284.  
  285.         else b = parseInt(colourArray[2])
  286.  
  287.         console.log("R = " + r);
  288.  
  289.         console.log("G = " + g);
  290.  
  291.         console.log("B = " + b);
  292.  
  293.         var clr = rgb2hsv(r, g, b);
  294.  
  295.         var h = (parseInt(clr[0] * 65535));
  296.  
  297.         var s = (parseInt(clr[1] * 65535));
  298.  
  299.         var b = (parseInt(clr[2] * 65535));
  300.  
  301.         console.log("H = " + h);
  302.  
  303.         console.log("S = " + s);
  304.  
  305.         console.log("B = " + b);
  306.  
  307.         lx.lightsColour(h, s, l, 0x0dac, 0);
  308.  
  309.     }
  310.  
  311. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement