Skip to content

Instantly share code, notes, and snippets.

@L1fescape
Created May 13, 2018 00:07
Show Gist options
  • Save L1fescape/3f21fed2222b4cb4c5862728dda09978 to your computer and use it in GitHub Desktop.
Save L1fescape/3f21fed2222b4cb4c5862728dda09978 to your computer and use it in GitHub Desktop.
and it still ain't a goddamn thing they could tell me

Glowed Up

Control LED pixels via Arduino in node

Installation

Downlaod and install the arduino library

$ npm i -g nodebots-interchange
$ interchange install git+https://github.com/ajfisher/node-pixel -a uno --firmata
$ wget https://raw.githubusercontent.com/firmata/arduino/master/examples/StandardFirmataPlus/StandardFirmataPlus.ino
$ arduino --board "arduino:avr:uno" --upload StandardFirmataPlus.ino --port /dev/ttyAMA0
$ npm i

Run

$ npm start
const five = require('johnny-five')
const pixel = require('node-pixel')
const board = new five.Board({ repl: false })
const numPixels = 150
const fps = 50
const setup = () => new Promise(resolve => {
board.on('ready', function() {
const strip = new pixel.Strip({
board: this,
controller: 'FIRMATA',
strips: [{ pin: 13, length: numPixels }],
GAmma: 1,
})
strip.on('ready', () => resolve(strip))
})
})
const hexToRGB = (hex) => {
const res = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
return res ? {
red: parseInt(res[1], 16),
green: parseInt(res[2], 16),
blue: parseInt(res[3], 16),
} : null
}
const butts = (strip, numPixels) => {
let curPixel = 0
let curColor = 0
const colors = ['#2E112D', '#E97778', '#89C7B6', '#FFD57E', '#AD84C7', '#7998C9']
return () => {
if (curPixel >= numPixels) {
curColor++
curPixel = 0
}
if (curColor >= colors.length) {
curColor = 0
}
strip.pixel(curPixel).color(colors[curColor])
strip.show()
curPixel++
}
}
setup().then(strip => {
const loopStep = butts(strip, numPixels)
setInterval(loopStep, 1000 / fps)
})
{
"name": "glowed-up",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node glowed-up.js",
"reset": "firmata-party uno --debug"
},
"author": "Andrew Kennedy",
"license": "ISC",
"dependencies": {
"firmata-party": "^1.5.9",
"johnny-five": "^0.14.3",
"node-pixel": "^0.9.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment