Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Last active April 17, 2016 03:23
Show Gist options
  • Save TheLarkInn/db647ed278ad3b40df5cd41ba456a4ce to your computer and use it in GitHub Desktop.
Save TheLarkInn/db647ed278ad3b40df5cd41ba456a4ce to your computer and use it in GitHub Desktop.
Code for fading RGB via color map.
var five = require("johnny-five"),
TesselIO = require("tessel-io"),
board = new five.Board({
io: new TesselIO()
});
// Initial Red color to start with.
// Decent transitions are easier with RGB vs HEX.
var startingColorObject = {
r: 255,
g: 0,
b: 0
}
board.on("ready", function() {
var index = 0,
// On tessel 2 the pins have a port prefix when using with johnny-five
RGBLED = new five.Led.RGB({
pins: {
red: "A5", // PWM Pin
green: "A6", // PWM Pin
blue: "B5" // PWM Pin
},
isAnode: true
});
function fadeFromGreenToRed() {
for(var i=0; i<255; i++) {
startingColorObject.r = i;
startingColorObject.g = 255-i;
startingColorObject.b = 0;
RGBLED.color(
rgbToHex(startingColorObject)
);
}
}
function fadeFromRedToBlue() {
for(var i=0; i<255; i++) {
startingColorObject.r = 255-i;
startingColorObject.g = 0;
startingColorObject.b = i;
RGBLED.color(
rgbToHex(startingColorObject)
);
}
}
function fadeFromBlueToGreen() {
for(var i=0; i<255; i++) {
startingColorObject.r = 0;
startingColorObject.g = i;
startingColorObject.b = 255-i;
RGBLED.color(
rgbToHex(startingColorObject)
);
}
}
// Handles conversions back to hex where the ending '0' would be missing.
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(rgbObject) {
var r = rgbObject.r,
g = rgbObject.g,
b = rgbObject.b;
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function rgbFade() {
fadeFromRedToBlue();
fadeFromBlueToGreen();
fadeFromGreenToRed();
}
// Set light to initial color.
RGBLED.color(
rgbToHex(startingColorObject)
);
setInterval(rgbFade);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment