Last active
April 12, 2016 23:40
-
-
Save prashcr/06a2aafef45e4047a608 to your computer and use it in GitHub Desktop.
Super hacky Node.js script that lets you control your cursor with a joystick connected to an Arduino, using Johnny-Five and RobotJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const five = require('johnny-five') | |
const board = new five.Board() | |
const robot = require('robotjs') | |
const range = 24 | |
const center = range / 2 | |
const threshold = range / 8 | |
let count = 0 | |
robot.setMouseDelay(1) | |
board.on('ready', function() { | |
// Create a new 'joystick' hardware instance. | |
const joystick = new five.Joystick({ | |
// [ x, y ] | |
pins: ['A0', 'A1'] | |
}) | |
joystick.on('change', function() { | |
console.log(count) | |
count++ | |
console.log('Joystick') | |
console.log(' x : ', this.x) | |
console.log(' y : ', this.y) | |
console.log('--------------------------------') | |
const xReading = readAxis(this.x) | |
const yReading = readAxis(this.y) | |
const current = robot.getMousePos() | |
robot.moveMouse(current.x + xReading, current.y - yReading) | |
}) | |
}) | |
function readAxis(thisAxis) { | |
// johnny-five reading ranges from -1 to 1 | |
// Add one then scale to a (0, range) reading | |
let reading = ++thisAxis * (range / 2) | |
let distance = reading - center | |
if (Math.abs(distance) < threshold) { | |
distance = 0 | |
} | |
return distance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment