Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var linino = require('ideino-linino-lib'),
- board = new linino.Board();
- var lifx = require('lifx');
- var lx = lifx.init();
- // Define colour sensor LED pins
- var ledArray = [2, 3, 4];
- // boolean to know if the balance has been set
- var balanceSet = false;
- //place holders for colour detected
- var red = 0;
- var green = 0;
- var blue = 0;
- //floats to hold colour arrays
- var colourArray = [0, 0, 0];
- var whiteArray = [0, 0, 0];
- var blackArray = [0, 0, 0];
- //place holder for average
- var avgRead;
- //delay
- function sleep(time, callback) {
- var stop = new Date().getTime();
- while (new Date().getTime() < stop + time) {;
- }
- callback();
- }
- //convert RGB to HSV
- function rgb2hsv(r, g, b) {
- r /= 255, g /= 255, b /= 255;
- var rr, gg, bb,
- h, s,
- v = Math.max(r, g, b),
- diff = v - Math.min(r, g, b),
- diffc = function (c) {
- return (v - c) / 6 / diff + 1 / 2;
- };
- if (diff == 0) {
- h = s = 0;
- } else {
- s = diff / v;
- rr = diffc(r);
- gg = diffc(g);
- bb = diffc(b);
- if (r === v) {
- h = bb - gg;
- } else if (g === v) {
- h = (1 / 3) + rr - bb;
- } else if (b === v) {
- h = (2 / 3) + gg - rr;
- }
- if (h < 0) {
- h += 1;
- } else if (h > 1) {
- h -= 1;
- }
- }
- return [h, s, v];
- }
- board.connect(function () {
- //setup the outputs for the colour sensor
- board.pinMode("D2", "OUTPUT");
- board.pinMode("D3", "OUTPUT");
- board.pinMode("D4", "OUTPUT");
- lx.lightsOn();
- setInterval(function () {
- checkBalance();
- checkColour();
- printColour();
- }, 1000);
- function checkBalance() {
- //check if the balance has been set, if not, set it
- if (balanceSet === false) {
- setBalance();
- }
- }
- function setBalance() {
- //set white balance
- sleep(5000, function () {});
- //delay for five seconds, this gives us time to get a white sample in front of our sensor
- //scan the white sample.
- //go through each light, get a reading, set the base reading for each colour red, green, and blue to the white array
- for (var i = 0; i <= 2; i++) {
- board.digitalWrite("D" + ledArray[i].toString(), 1);
- sleep(100, function () {});
- getReading(5); //number is the number of scans to take for average, this whole function is redundant, one reading works just as well.
- whiteArray[i] = avgRead;
- board.digitalWrite("D" + ledArray[i].toString(), 0);
- sleep(100, function () {});
- }
- sleep(5000, function () {});
- //done scanning white, now it will pulse blue to tell you that it is time for the black (or grey) sample.
- //set black balance
- //wait for five seconds so we can position our black sample
- //go ahead and scan, sets the colour values for red, green, and blue when exposed to black
- for (var j = 0; j <= 2; j++) {
- board.digitalWrite("D" + ledArray[j].toString(), 1);
- sleep(100, function () {});
- getReading(5);
- blackArray[j] = avgRead;
- //blackArray[i] = analogRead(2);
- board.digitalWrite("D" + ledArray[j].toString(), 0);
- sleep(100, function () {});
- }
- //set boolean value so we know that balance is set
- balanceSet = true;
- sleep(5000, function () {});
- //delay another 5 seconds to allow the human to catch up to what is going on
- }
- function checkColour() {
- for (var i = 0; i <= 2; i++) {
- board.digitalWrite("D" + ledArray[i].toString(), 1); //turn or the LED, red, green or blue depending which iteration
- sleep(100, function () {});
- getReading(5); //take a reading however many times
- colourArray[i] = avgRead; //set the current colour in the array to the average reading
- var greyDiff = whiteArray[i] - blackArray[i]; //the highest possible return minus the lowest returns the area for values in between
- 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
- board.digitalWrite("D" + ledArray[i].toString(), 0); //turn off the current LED
- sleep(100, function () {});
- }
- }
- function getReading(times) {
- var reading;
- var tally = 0;
- //take the reading however many times was requested and add them up
- for (var i = 0; i < times; i++) {
- reading = board.analogRead("A0");
- tally = reading + tally;
- sleep(100, function () {});
- }
- //calculate the average and set it
- avgRead = (tally) / times;
- }
- //prints the colour in the colour array, in the next step, we will send this to processing to see how good the sensor works.
- function printColour() {
- var r, g, b;
- if (colourArray[0] > 255) r = 255
- else if (colourArray[0] < 0) r = 0
- else r = parseInt(colourArray[0])
- if (colourArray[1] > 255) g = 255
- else if (colourArray[1] < 0) g = 0
- else g = parseInt(colourArray[1])
- if (colourArray[2] > 255) b = 255
- else if (colourArray[2] < 0) b = 0
- else b = parseInt(colourArray[2])
- console.log("R = " + r);
- console.log("G = " + g);
- console.log("B = " + b);
- var clr = rgb2hsv(r, g, b);
- var h = (parseInt(clr[0] * 65535));
- var s = (parseInt(clr[1] * 65535));
- var b = (parseInt(clr[2] * 65535));
- console.log("H = " + h);
- console.log("S = " + s);
- console.log("B = " + b);
- lx.lightsColour(h, s, l, 0x0dac, 0);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement