Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from jochasinga/pot.js
Last active July 27, 2026 16:18
Show Gist options
  • Select an option

  • Save rwaldron/22095a9b759f70edef22 to your computer and use it in GitHub Desktop.

Select an option

Save rwaldron/22095a9b759f70edef22 to your computer and use it in GitHub Desktop.
// Require modules
var Firebase = require("firebase");
var five = require("johnny-five");
// Create a new instance of Firebase db
var firebaseRef = new Firebase(
// Fictional URL: replace this with your own from firebase
"https://burning-limbo-666.firebaseio.com/colors"
);
// Create a new instance of the Arduino board
five.Board().on("ready", function() {
var maxValue = 511;
var colRange = 6;
var offset = maxValue / colRange;
// Create a new pot instance
var pot = new five.Sensor({
pin: "A0",
freq: 250
});
// Create a new led array based on pin number
var ledArray = new five.Led.Array([9, 10, 11]);
// Listen on "data" change
pot.on("data", function() {
var self = this.value;
// Print pot output value
console.log(self);
// Implement new map method to emulate map() in Arduino
Number.prototype.map = function(fromLow , fromHigh , toLow , toHigh) {
return (this - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
}
// Map dynamic color brightness to pot value.
// Numbers in JavaScript are all floating point numbers,
// thus the Math.round().
// RED - VIOLET - BLUE
var redDec = Math.round(self.map(offset, offset*2, 255, 0));
var blueInc = Math.round(self.map(0, offset, 0, 255));
// BLUE - CYAN - GREEN
var blueDec = Math.round(self.map(offset*4, offset*5, 255, 0));
var greenInc = Math.round(self.map(offset*2, offset*3, 0, 255));
// GREEN - YELLOW - RED
var greenDec = Math.round(self.map(offset*5, offset*6, 255, 0));
var redInc = Math.round(self.map(offset*4, offset*5, 0, 255));
// Adjusting color brightness conditionally based on
// the location of the pot output value.
switch (true) {
case (self >= 0 && self <= offset):
console.log("1st loop");
ledArray[0].brightness(255);
ledArray[2].brightness(blueInc);
ledArray[1].brightness(0);
// Update the child data of colors on Firebase
firebaseRef.set({"r": 255, "b": blueInc, "g": 0});
break;
case (self > offset && self <= offset*2):
console.log("2nd loop");
ledArray[0].brightness(redDec);
ledArray[2].brightness(255);
ledArray[1].brightness(0);
// Update the child data of colors on Firebase
firebaseRef.set({"r": redDec, "b": 255, "g": 0});
break;
case (self > offset*2 && self <= offset*3):
console.log("3rd loop");
ledArray[0].brightness(0);
ledArray[2].brightness(255);
ledArray[1].brightness(greenInc);
// Update the child data of colors on Firebase
firebaseRef.set({"r": 0, "b": 255, "g": greenInc});
break;
case (self > offset*3 && self <= offset*4):
console.log("4th loop");
ledArray[0].brightness(0);
ledArray[2].brightness(blueDec);
ledArray[1].brightness(255);
// Update the child data of colors on Firebase
firebaseRef.set({"r": 0, "b": blueInc, "g": 0});
break;
case (self > offset*4 && self <= offset*5):
console.log("5th loop");
ledArray[0].brightness(redInc);
ledArray[2].brightness(0);
ledArray[1].brightness(255);
// Update the child data of colors on Firebase
firebaseRef.set({"r": redInc, "b": 0, "g": 255});
break;
case (self > offset*5 && self <= offset*6):
console.log("6th loop");
ledArray[0].brightness(255);
ledArray[2].brightness(0);
ledArray[1].brightness(greenDec);
// Update the child data of colors on Firebase
firebaseRef.set({"r": 255, "b": 0, "g": greenDec});
break;
default:
console.log("Out of range");
ledArray[0].brightness(255);
ledArray[2].brightness(0);
ledArray[1].brightness(0);
// Update the child data of colors on Firebase
firebaseRef.set({"r": 255, "b": 0, "g": 0});
}
});
});
@rwaldron

Copy link
Copy Markdown
Author

This line: https://gist.github.com/rwaldron/22095a9b759f70edef22#file-pot-js-L33-L36 is actually unnecessary, you can get this function from five.Fn.map :)

Aside from that—totally awesome!

@jochasinga

Copy link
Copy Markdown

@rwaldron I was looking for exactly that! Thank you!

@giroudoliver997-oss

Copy link
Copy Markdown

Magnalite Cookware is renowned for its premium cast aluminum construction, delivering exceptional heat distribution for authentic Cajun cooking and family-favorite recipes. Designed for durability and reliable performance, this iconic cookware helps create flavorful meals with consistent cooking results every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment