Skip to content

Instantly share code, notes, and snippets.

@steveruizok
Created July 12, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveruizok/b8fff2cc158f8da8baefbcf3d888c23a to your computer and use it in GitHub Desktop.
Save steveruizok/b8fff2cc158f8da8baefbcf3d888c23a to your computer and use it in GitHub Desktop.
The Arduino project's file for the RGB LED example.
var { connectToHost } = require('./client')
var five = require('johnny-five')
var board = new five.Board()
var rgbHex = require('rgb-hex')
function main() {
// Connection
const serverUrl = 'http://localhost:8080'
const responses = {
CONNECT: () => console.log('Connected'),
DISCONNECT: () => console.log('Disconnected'),
CHANGE_COLOR: (data) => updateColor(data.color, data.value),
}
const dispatch = connectToHost(serverUrl, responses)
// Components
const led = new five.Led.RGB({
pins: [6, 5, 3],
})
board.repl.inject({
led: led,
})
led.on()
// State
let rgb = {
red: 255,
green: 255,
blue: 255,
}
// State changes
const updateColor = (color, value) => {
let intColor = Math.round((value / 100) * 255)
let hexColor = rgbHex(rgb.red, rgb.green, rgb.blue)
rgb[color] = intColor
led.color(hexColor)
}
}
// When the board is ready, run main
board.on('ready', main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment