Skip to content

Commit e526cf6

Browse files
committed
Joystick: Controller refactor, Esplora support.
- Includes Sparkfun Joystick Shield example
1 parent 084b028 commit e526cf6

15 files changed

+1087
-217
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ To interactively navigate the examples, visit the [Johnny-Five examples](http://
278278

279279
### Joystick
280280
- [Joystick](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick.md)
281-
- [Joystick - Claw control](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick-claw.md)
281+
- [Joystick - Esplora](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick-esplora.md)
282+
- [Joystick - Sparkfun Shield](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick-shield.md)
282283
- [Joystick - Pan + Tilt control](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick-pantilt.md)
283-
- [Joystick - Motor control](https://github.com/rwaldron/johnny-five/blob/master/docs/joystick-motor-led.md)
284284

285285
### LCD
286286
- [LCD](https://github.com/rwaldron/johnny-five/blob/master/docs/lcd.md)

docs/breadboard/esplora.fzz

1007 Bytes
Binary file not shown.

docs/breadboard/esplora.png

182 KB
Loading
231 KB
Loading

docs/joystick-esplora.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!--remove-start-->
2+
3+
# Joystick - Esplora
4+
5+
6+
7+
Run with:
8+
```bash
9+
node eg/joystick-esplora.js
10+
```
11+
12+
<!--remove-end-->
13+
14+
```javascript
15+
var five = require("johnny-five");
16+
var board = new five.Board();
17+
18+
board.on("ready", function() {
19+
/*
20+
Be sure to reflash your Esplora with StandardFirmata!
21+
22+
In the Arduino IDE:
23+
24+
Tools > Boards > Arduino Leonard or Arduino Esplora
25+
*/
26+
var joystick = new five.Joystick({
27+
controller: "ESPLORA"
28+
});
29+
30+
joystick.on("change", function() {
31+
console.log("Joystick");
32+
console.log(" x : ", this.x);
33+
console.log(" y : ", this.y);
34+
console.log("--------------------------------------");
35+
});
36+
});
37+
38+
```
39+
40+
41+
## Illustrations / Photos
42+
43+
44+
### Joystick - Esplora
45+
46+
47+
Esplora joystick example.
48+
49+
50+
![docs/breadboard/esplora.png](breadboard/esplora.png)<br>
51+
52+
Fritzing diagram: [docs/breadboard/esplora.fzz](breadboard/esplora.fzz)
53+
54+
&nbsp;
55+
56+
57+
58+
59+
60+
&nbsp;
61+
62+
<!--remove-start-->
63+
64+
## License
65+
Copyright (c) 2012, 2013, 2014 Rick Waldron <waldron.rick@gmail.com>
66+
Licensed under the MIT license.
67+
Copyright (c) 2014, 2015 The Johnny-Five Contributors
68+
Licensed under the MIT license.
69+
70+
<!--remove-end-->

docs/joystick-pantilt.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,38 @@ node eg/joystick-pantilt.js
1212
<!--remove-end-->
1313

1414
```javascript
15-
var five = require("johnny-five"),
16-
board = new five.Board({
17-
debug: true
18-
});
15+
var five = require("johnny-five");
16+
var board = new five.Board();
1917

2018
board.on("ready", function() {
21-
var range, pan, tilt, joystick;
22-
23-
range = [0, 170];
19+
var range = [0, 170];
2420

2521
// Servo to control panning
26-
pan = new five.Servo({
22+
var pan = new five.Servo({
2723
pin: 9,
28-
range: range
24+
range: range,
25+
center: true
2926
});
3027

3128
// Servo to control tilt
32-
tilt = new five.Servo({
29+
var tilt = new five.Servo({
3330
pin: 10,
34-
range: range
31+
range: range,
32+
center: true
3533
});
3634

3735
// Joystick to control pan/tilt
3836
// Read Analog 0, 1
3937
// Limit events to every 50ms
40-
joystick = new five.Joystick({
38+
var joystick = new five.Joystick({
4139
pins: ["A0", "A1"],
4240
freq: 100
4341
});
4442

45-
// Center all servos
46-
(five.Servos()).center();
47-
48-
joystick.on("axismove", function() {
49-
50-
tilt.to(Math.ceil(170 * this.fixed.y));
51-
pan.to(Math.ceil(170 * this.fixed.x));
5243

44+
joystick.on("change", function() {
45+
tilt.to(five.Fn.scale(this.y, -1, 1, 0, 170));
46+
pan.to(five.Fn.scale(this.x, -1, 1, 0, 170));
5347
});
5448
});
5549

docs/joystick-shield.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!--remove-start-->
2+
3+
# Joystick - Sparkfun Shield
4+
5+
6+
7+
Run with:
8+
```bash
9+
node eg/joystick-shield.js
10+
```
11+
12+
<!--remove-end-->
13+
14+
```javascript
15+
var five = require("johnny-five");
16+
var board = new five.Board();
17+
18+
board.on("ready", function() {
19+
var joystick = new five.Joystick({
20+
pins: ["A0", "A1"],
21+
invertY: true
22+
});
23+
24+
joystick.on("change", function() {
25+
console.log("Joystick");
26+
console.log(" x : ", this.x);
27+
console.log(" y : ", this.y);
28+
console.log("--------------------------------------");
29+
});
30+
});
31+
32+
```
33+
34+
35+
## Illustrations / Photos
36+
37+
38+
### Joystick - Sparkfun Shield
39+
40+
41+
Sparkfun Joystick Shield example.
42+
43+
44+
![docs/breadboard/joystick-shield.png](breadboard/joystick-shield.png)<br>
45+
46+
&nbsp;
47+
48+
49+
50+
51+
52+
&nbsp;
53+
54+
<!--remove-start-->
55+
56+
## License
57+
Copyright (c) 2012, 2013, 2014 Rick Waldron <waldron.rick@gmail.com>
58+
Licensed under the MIT license.
59+
Copyright (c) 2014, 2015 The Johnny-Five Contributors
60+
Licensed under the MIT license.
61+
62+
<!--remove-end-->

docs/joystick.md

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,18 @@ board.on("ready", function() {
1919

2020
// Create a new `joystick` hardware instance.
2121
var joystick = new five.Joystick({
22-
// Joystick pins are an array of pins
23-
// Pin orders:
24-
// [ up, down, left, right ]
25-
// [ ud, lr ]
22+
// [ x, y ]
2623
pins: ["A0", "A1"]
2724
});
2825

29-
// Joystick Event API
30-
joystick.on("axismove", function(err, timestamp) {
31-
32-
// Axis data is available on:
33-
// this.axis
34-
// {
35-
// x: 0...1, ( 0 <-- L/R --> 1 )
36-
// y: 0...1 ( 0 <-- D/U --> 1 )
37-
// }
38-
//
39-
// Center is ~0.5
40-
//
41-
// console.log( "input", this.axis );
42-
// console.log( "LR:", this.axis.x, this.normalized.x );
43-
// console.log( "UD:", this.axis.y, this.normalized.y );
44-
// console.log( "MAG:", this.magnitude );
45-
46-
console.log("LR:", this.fixed.x);
47-
console.log("UD:", this.fixed.y);
48-
console.log("MAG:", this.magnitude);
49-
26+
joystick.on("change", function() {
27+
console.log("Joystick");
28+
console.log(" x : ", this.x);
29+
console.log(" y : ", this.y);
30+
console.log("--------------------------------------");
5031
});
5132
});
5233

53-
54-
// Schematic
55-
// https://1965269182786388413-a-1802744773732722657-s-sites.googlegroups.com/site/parallaxinretailstores/home/2-axis-joystick/Joystick-6.png
56-
// http://www.parallax.com/Portals/0/Downloads/docs/prod/sens/27800-Axis%20JoyStick_B%20Schematic.pdf
57-
58-
// Further Reading
59-
// http://www.parallax.com/Portals/0/Downloads/docs/prod/sens/27800-2-AxisJoystick-v1.2.pdf
60-
6134
```
6235

6336

eg/joystick-esplora.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var five = require("../lib/johnny-five.js");
2+
var board = new five.Board();
3+
4+
board.on("ready", function() {
5+
/*
6+
Be sure to reflash your Esplora with StandardFirmata!
7+
8+
In the Arduino IDE:
9+
10+
Tools > Boards > Arduino Leonard or Arduino Esplora
11+
*/
12+
var joystick = new five.Joystick({
13+
controller: "ESPLORA"
14+
});
15+
16+
joystick.on("change", function() {
17+
console.log("Joystick");
18+
console.log(" x : ", this.x);
19+
console.log(" y : ", this.y);
20+
console.log("--------------------------------------");
21+
});
22+
});

eg/joystick-pantilt.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
var five = require("../lib/johnny-five.js"),
2-
board = new five.Board({
3-
debug: true
4-
});
1+
var five = require("../lib/johnny-five.js");
2+
var board = new five.Board();
53

64
board.on("ready", function() {
7-
var range, pan, tilt, joystick;
8-
9-
range = [0, 170];
5+
var range = [0, 170];
106

117
// Servo to control panning
12-
pan = new five.Servo({
8+
var pan = new five.Servo({
139
pin: 9,
14-
range: range
10+
range: range,
11+
center: true
1512
});
1613

1714
// Servo to control tilt
18-
tilt = new five.Servo({
15+
var tilt = new five.Servo({
1916
pin: 10,
20-
range: range
17+
range: range,
18+
center: true
2119
});
2220

2321
// Joystick to control pan/tilt
2422
// Read Analog 0, 1
2523
// Limit events to every 50ms
26-
joystick = new five.Joystick({
24+
var joystick = new five.Joystick({
2725
pins: ["A0", "A1"],
2826
freq: 100
2927
});
3028

31-
// Center all servos
32-
(five.Servos()).center();
33-
34-
joystick.on("axismove", function() {
35-
36-
tilt.to(Math.ceil(170 * this.fixed.y));
37-
pan.to(Math.ceil(170 * this.fixed.x));
3829

30+
joystick.on("change", function() {
31+
tilt.to(five.Fn.scale(this.y, -1, 1, 0, 170));
32+
pan.to(five.Fn.scale(this.x, -1, 1, 0, 170));
3933
});
4034
});

0 commit comments

Comments
 (0)