Skip to content

Instantly share code, notes, and snippets.

@AnnaGerber
Created July 24, 2015 19:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnnaGerber/e5f897b745e5f96da463 to your computer and use it in GitHub Desktop.
Save AnnaGerber/e5f897b745e5f96da463 to your computer and use it in GitHub Desktop.
// =======================
// Derived from the work done by @makenai on the
// SumoBot Jr
// =======================
var five = require("johnny-five");
var keypress = require('keypress');
var RSTOPVAL = 90;
var LSTOPVAL = 90;
var opts = {};
opts.port = process.argv[2] || "";
keypress(process.stdin);
var board = new five.Board(opts);
board.on("ready", function() {
console.log("Control the bot with the arrow keys, and SPACE to stop.")
var left_wheel = new five.Servo({ pin: 5, type: 'continuous' });
var right_wheel = new five.Servo({ pin: 6, type: 'continuous' });
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
left_wheel.to(LSTOPVAL);
right_wheel.to(RSTOPVAL);
process.stdin.on('keypress', function (ch, key) {
if ( !key ) return;
if ( key.name == 'q' ) {
console.log('Quitting');
process.exit();
} else if ( key.name == 'up' ) {
console.log('Forward');
left_wheel.cw();
right_wheel.ccw();
} else if ( key.name == 'down' ) {
console.log('Backward');
left_wheel.ccw();
right_wheel.cw();
} else if ( key.name == 'left' ) {
console.log('Left');
left_wheel.ccw();
right_wheel.ccw();
} else if ( key.name == 'right' ) {
console.log('Right');
left_wheel.cw();
right_wheel.cw();
} else if ( key.name == 'space' ) {
console.log('Stopping');
left_wheel.to(LSTOPVAL);
right_wheel.to(RSTOPVAL);
}
});
});
board.on("error", function(err) {
console.log(err.message);
process.exit();
});
var five = require("johnny-five"),
onButton, dimButton, offButton, led;
five.Board().on("ready", function() {
onButton = new five.Button({
pin:"A1",
pullup: true
});
dimButton = new five.Button({
pin:"A2",
pullup: true
});
offButton = new five.Button({
pin:"A3",
pullup: true
});
led = new five.Led({
pin:10,
isAnode:true
});
onButton.on("press", function(value){
console.log("Turn LED on");
led.on();
});
dimButton.on("press", function(){
console.log("Dim LED");
led.brightness(50);
});
offButton.on("press", function(){
console.log("Turn LED off");
led.off();
});
});
var five = require("johnny-five");
var exports = {};
// override noTone function so that it sets HIGH instead of LOW
// (this is inverted because of the way that piezo is wired up with 10K resistor to 5V)
five.Piezo.prototype.noTone = function() {
this.io.digitalWrite(this.pin, 255);
if (this.timer) {
this.timer.clearInterval();
delete this.timer;
}
return this;
};
exports.five = five;
module.exports = exports;
var five = require("johnny-five"),
board, myPhotoresistor, myLed;
board = new five.Board();
board.on("ready", function() {
//myLed = new five.Led(9);
myPhotoresistor = new five.Sensor({
pin: "A4",
freq: 250
});
myPhotoresistor.on("data", function( err, value ) {
console.log("light", value)
// range of led brightness is 0 - 255
var brightnessValue = five.Fn.constrain(five.Fn.map(value, 0, 900, 0, 255), 0, 255);
//myLed.brightness(brightnessValue);
});
});
{
"name": "nbd2015",
"version": "1.0.0",
"description": "johnny-five code examples to work with HCARDU0085 expansion board for Arduino Uno",
"author": "Anna Gerber",
"license": "MIT",
"dependencies": {
"async": "^1.4.0",
"johnny-five": "^0.8.81",
"keypress": "^0.2.1",
"lodash": "^3.10.0"
}
}
var eb = require("./expansion-board.js"),
five = eb.five,
board, piezo;
board = new five.Board();
board.on("ready", function() {
piezo = new five.Piezo(3);
function playSong() {
piezo.play({
// song is composed by a string of notes
// a default beat is set, and the default octave is used
// any invalid note is read as "no note"
song: "C D F D A - A A A A G G G G - - C D F D G - G G G G F F F F - -",
beats: 1 / 4,
tempo: 100
});
}
playSong();
board.repl.inject({
piezo: piezo,
playSong: playSong
});
console.log("Try piezo.tone(160,100) or piezo.noTone() or playSong()\nHit control D to exit\n>>");
});
var five = require("johnny-five");
var myBoard, ping;
myBoard = new five.Board();
myBoard.on("ready", function() {
ping = new five.Proximity({
controller: "HCSR04",
pin: 9
});
ping.on("data", function() {
console.log("inches: ", this.inches);
console.log("cm: ", this.cm);
});
});
var five = require("johnny-five"),
board, myPotentiometer;
board = new five.Board();
board.on("ready", function() {
myPotentiometer = new five.Sensor({
pin: "A0",
freq: 500
});
myPotentiometer.on("data", function(err,data) {
console.log(data);
});
});
var eb = require("./expansion-board.js"),
five = eb.five;
var board = new five.Board();
board.on("ready", function() {
/*
This assumes the segments are as follows:
A
---
F | | B
--- <---- G
E | | C
---
D o DP
*/
var isCommonAnode = true;
var digits = [
// .GFEDCBA
parseInt("00111111", 2),
parseInt("00000110", 2),
parseInt("01011011", 2),
parseInt("01001111", 2),
parseInt("01100110", 2),
parseInt("01101101", 2),
parseInt("01111101", 2),
parseInt("00000111", 2),
parseInt("01111111", 2),
parseInt("01101111", 2),
];
var segments = {
// .GFEDCBA
a: parseInt("00000001", 2),
b: parseInt("00000010", 2),
c: parseInt("00000100", 2),
d: parseInt("00001000", 2),
e: parseInt("00010000", 2),
f: parseInt("00100000", 2),
g: parseInt("01000000", 2),
dp: parseInt("10000000", 2)
};
var piezo = new five.Piezo(3);
piezo.noTone()
var register = new five.ShiftRegister({
pins: {
data: 8,
clock: 7,
latch: 4
}
});
function invert(num) {
return ((~num << 24) >> 24) & 255;
}
function digit(num) {
clear();
register.send(
isCommonAnode ? invert(digits[num]) : digits[num]
);
}
function segment(s) {
clear();
register.send(
isCommonAnode ? invert(segments[s]) : segments[s]
);
}
function clear() {
register.send(
isCommonAnode ? 255 : 0
);
}
var i = 9;
function next() {
digit(i--);
if (i < 0) {
i = 9;
setTimeout(next, 2000);
} else {
setTimeout(next, 1000);
}
}
//next();
segment("a");
});
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var temp = new five.Temperature({
pin: "A4",
controller: "LM35",
freq: 1000
});
temp.on("data", function(err, data) {
// value: function(raw) {
// return (5.0 * raw * 100.0)/1024.0;
// }
console.log("data",data)
//console.log("Temp: %d", data.celsius);
//console.log(data.celsius + "°C", data.fahrenheit + "°F");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment