Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

seven segment display example & wiring diagram. Closes gh-70

  • Loading branch information...
commit 93431a1856ee56faf7ef2856c2ed386074915e87 1 parent 5148bb9
Rebecca Murphey rmurphey authored committed
1  README.md
View
@@ -136,6 +136,7 @@ board.on("ready", function() {
- [Led Pulse](https://github.com/rwldrn/johnny-five/blob/master/docs/led-pulse.md)
- [Led Rgb](https://github.com/rwldrn/johnny-five/blob/master/docs/led-rgb.md)
- [Led Strobe](https://github.com/rwldrn/johnny-five/blob/master/docs/led-strobe.md)
+- [Seven Segment](https://github.com/rwldrn/johnny-five/blob/master/docs/seven-segment.md)
### Magnetometer (Compass)
- [Magnetometer Log](https://github.com/rwldrn/johnny-five/blob/master/docs/magnetometer-log.md)
- [Magnetometer North](https://github.com/rwldrn/johnny-five/blob/master/docs/magnetometer-north.md)
BIN  docs/breadboard/seven-segment.fzz
View
Binary file not shown
BIN  docs/breadboard/seven-segment.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
153 docs/seven-segment.md
View
@@ -0,0 +1,153 @@
+# Seven Segment
+
+Run with:
+```bash
+node eg/seven-segment.js
+```
+
+
+```javascript
+/**
+ * This example uses a single seven-segment display (common anode) and a
+ * 74HC595 shift register. See docs/breadboard/seven-segment.png for wiring.
+ */
+var five = require("johnny-five"),
+ board;
+
+board = new five.Board();
+
+board.on("ready", function() {
+ var isCommonAnode, digits, segments, sr, led, i;
+
+ /*
+
+ This assumes the segments are as follows:
+
+ A
+ ---
+ F | | B
+ --- <---- G
+ E | | C
+ ---
+ D o DP
+
+ */
+
+ isCommonAnode = true;
+ digits = [];
+
+ // .GFEDCBA
+ digits[0] = parseInt( "00111111", 2 ); // 0
+ digits[1] = parseInt( "00000110", 2 ); // 1
+ digits[2] = parseInt( "01011011", 2 ); // 2
+ digits[3] = parseInt( "01001111", 2 ); // 3
+ digits[4] = parseInt( "01100110", 2 ); // 4
+ digits[5] = parseInt( "01101101", 2 ); // 5
+ digits[6] = parseInt( "01111101", 2 ); // 6
+ digits[7] = parseInt( "00000111", 2 ); // 7
+ digits[8] = parseInt( "01111111", 2 ); // 8
+ digits[9] = parseInt( "01101111", 2 ); // 9
+
+ 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 )
+ };
+
+ sr = new five.ShiftRegister({
+ pins: {
+ data: 2,
+ clock: 3,
+ latch: 4
+ }
+ });
+
+ led = new five.Led(5);
+
+ function invert( num ) {
+ return ((~num << 24) >> 24) & 255;
+ }
+
+ sr.digit = function( num ) {
+ sr.clear();
+
+ sr.send(
+ isCommonAnode ? invert(digits[num]) : digits[num]
+ );
+ };
+
+ sr.segment = function( s ) {
+ sr.clear();
+
+ sr.send(
+ isCommonAnode ? invert(segments[s]) : segments[s]
+ );
+ };
+
+ sr.clear = function() {
+ sr.send(
+ isCommonAnode ? 255 : 0
+ );
+ };
+
+ i = 9;
+
+ function next() {
+ led.stop();
+ sr.digit( i-- );
+
+ if ( i < 0 ) {
+ i = 9;
+ led.strobe(50);
+ setTimeout( next, 2000 );
+ } else {
+ setTimeout( next, 1000 );
+ }
+ }
+
+ next();
+});
+
+```
+
+## Breadboard
+
+<img src="https://raw.github.com/rwldrn/johnny-five/master/docs/breadboard/seven-segment.png">
+
+[docs/breadboard/seven-segment.fzz](https://github.com/rwldrn/johnny-five/blob/master/docs/breadboard/seven-segment.fzz)
+
+
+
+## Devices
+
+
+
+
+## Documentation
+
+_(Nothing yet)_
+
+
+
+
+
+
+
+
+
+## Contributing
+All contributions must adhere to the [Idiomatic.js Style Guide](https://github.com/rwldrn/idiomatic.js),
+by maintaining the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/cowboy/grunt).
+
+## Release History
+_(Nothing yet)_
+
+## License
+Copyright (c) 2012 Rick Waldron <waldron.rick@gmail.com>
+Licensed under the MIT license.
106 eg/seven-segment.js
View
@@ -0,0 +1,106 @@
+/**
+ * This example uses a single seven-segment display (common anode) and a
+ * 74HC595 shift register. See docs/breadboard/seven-segment.png for wiring.
+ */
+var five = require("../lib/johnny-five.js"),
+ board;
+
+board = new five.Board();
+
+board.on("ready", function() {
+ var isCommonAnode, digits, segments, sr, led, i;
+
+ /*
+
+ This assumes the segments are as follows:
+
+ A
+ ---
+ F | | B
+ --- <---- G
+ E | | C
+ ---
+ D o DP
+
+ */
+
+ isCommonAnode = true;
+ digits = [];
+
+ // .GFEDCBA
+ digits[0] = parseInt( "00111111", 2 ); // 0
+ digits[1] = parseInt( "00000110", 2 ); // 1
+ digits[2] = parseInt( "01011011", 2 ); // 2
+ digits[3] = parseInt( "01001111", 2 ); // 3
+ digits[4] = parseInt( "01100110", 2 ); // 4
+ digits[5] = parseInt( "01101101", 2 ); // 5
+ digits[6] = parseInt( "01111101", 2 ); // 6
+ digits[7] = parseInt( "00000111", 2 ); // 7
+ digits[8] = parseInt( "01111111", 2 ); // 8
+ digits[9] = parseInt( "01101111", 2 ); // 9
+
+ 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 )
+ };
+
+ sr = new five.ShiftRegister({
+ pins: {
+ data: 2,
+ clock: 3,
+ latch: 4
+ }
+ });
+
+ led = new five.Led(5);
+
+ function invert( num ) {
+ return ((~num << 24) >> 24) & 255;
+ }
+
+ sr.digit = function( num ) {
+ sr.clear();
+
+ sr.send(
+ isCommonAnode ? invert(digits[num]) : digits[num]
+ );
+ };
+
+ sr.segment = function( s ) {
+ sr.clear();
+
+ sr.send(
+ isCommonAnode ? invert(segments[s]) : segments[s]
+ );
+ };
+
+ sr.clear = function() {
+ sr.send(
+ isCommonAnode ? 255 : 0
+ );
+ };
+
+ i = 9;
+
+ function next() {
+ led.stop();
+ sr.digit( i-- );
+
+ if ( i < 0 ) {
+ i = 9;
+ led.strobe(50);
+ setTimeout( next, 2000 );
+ } else {
+ setTimeout( next, 1000 );
+ }
+ }
+
+ next();
+});
1  programs.json
View
@@ -73,6 +73,7 @@
"led-pulse.js",
"led-rgb.js",
"led-strobe.js",
+ "seven-segment.js",
[ "Magnetometer (Compass)" ],
Please sign in to comment.
Something went wrong with that request. Please try again.