Skip to content

Instantly share code, notes, and snippets.

@haugstrup
Forked from rmurphey/notes.md
Created September 5, 2012 05:33
Show Gist options
  • Select an option

  • Save haugstrup/3631107 to your computer and use it in GitHub Desktop.

Select an option

Save haugstrup/3631107 to your computer and use it in GitHub Desktop.

Revisions

  1. @rmurphey rmurphey revised this gist Aug 28, 2012. 1 changed file with 21 additions and 8 deletions.
    29 changes: 21 additions & 8 deletions seven-segment.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    var five = require("../lib/johnny-five.js"),
    sr;
    sr, led;

    var board = new five.Board();
    var board = new five.Board()

    board.on("ready", function() {

    @@ -54,8 +54,9 @@ board.on("ready", function() {
    }
    });

    led = new five.Led(5);

    function invert(num) {
    console.log('inverting', num);
    return ((~num << 24) >> 24) & 255;
    }

    @@ -77,10 +78,22 @@ board.on("ready", function() {
    sr.send( commonAnode ? 255 : 0 );
    };

    board.repl.inject({
    sr: sr,
    digits: digits,
    segments: segments
    });
    var 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();

    });
  2. @rmurphey rmurphey revised this gist Aug 24, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion notes.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,6 @@

    <a href="http://www.flickr.com/photos/rdmey/7848046312/" title="Untitled by rdmey, on Flickr"><img src="http://farm8.staticflickr.com/7251/7848046312_9990a6eac0.jpg" width="427" height="640" alt="seven segment display with shift register"></a>

    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.
    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.

    ![schematic](https://dl.dropbox.com/u/2916642/arduino/seven-segment.png)
  3. @rmurphey rmurphey revised this gist Aug 24, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions seven-segment.js
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ board.on("ready", function() {
    var commonAnode = true;
    var digits = [];

    // .GFEDBCA
    // .GFEDCBA
    digits[0] = parseInt('00111111', 2); // 0
    digits[1] = parseInt('00000110', 2); // 1
    digits[2] = parseInt('01011011', 2); // 2
    @@ -35,7 +35,7 @@ board.on("ready", function() {
    digits[9] = parseInt('01101111', 2); // 9

    var segments = {
    // .GFEDBCA
    // .GFEDCBA
    a : parseInt('00000001', 2),
    b : parseInt('00000010', 2),
    c : parseInt('00000100', 2),
  4. @rmurphey rmurphey revised this gist Aug 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion notes.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Johnny-Five: 7-Segment Display with Shift Register

    <a href="http://www.flickr.com/photos/rdmey/7848046312/" title="Untitled by rdmey, on Flickr"><img src="http://farm8.staticflickr.com/7251/7848046312_15b64556e5_z.jpg" width="427" height="640" alt="seven segment display with shift register"></a>
    <a href="http://www.flickr.com/photos/rdmey/7848046312/" title="Untitled by rdmey, on Flickr"><img src="http://farm8.staticflickr.com/7251/7848046312_9990a6eac0.jpg" width="427" height="640" alt="seven segment display with shift register"></a>

    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.
  5. @rmurphey rmurphey revised this gist Aug 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion notes.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Johnny-Five: 7-Segment Display with Shift Register

    ![seven segment display with shift register](http://farm8.staticflickr.com/7251/7848046312_9990a6eac0.jpg)
    <a href="http://www.flickr.com/photos/rdmey/7848046312/" title="Untitled by rdmey, on Flickr"><img src="http://farm8.staticflickr.com/7251/7848046312_15b64556e5_z.jpg" width="427" height="640" alt="seven segment display with shift register"></a>

    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.
  6. @rmurphey rmurphey revised this gist Aug 24, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions notes.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Johnny-Five: 7-Segment Display with Shift Register

    ![seven segment display with shift register](http://farm8.staticflickr.com/7251/7848046312_9990a6eac0.jpg)

    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.
  7. @rmurphey rmurphey created this gist Aug 24, 2012.
    3 changes: 3 additions & 0 deletions notes.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    ![seven segment display with shift register](http://farm8.staticflickr.com/7251/7848046312_9990a6eac0.jpg)

    See code for presumed segment layout. Segment A is attached to Q0 of the shift register, B-Q1, etc. The DP is attached to Q7.
    86 changes: 86 additions & 0 deletions seven-segment.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    var five = require("../lib/johnny-five.js"),
    sr;

    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 commonAnode = true;
    var digits = [];

    // .GFEDBCA
    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

    var segments = {
    // .GFEDBCA
    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
    }
    });

    function invert(num) {
    console.log('inverting', num);
    return ((~num << 24) >> 24) & 255;
    }

    sr.digit = function(num) {
    sr.clear();

    var val = commonAnode ? invert(digits[num]) : digits[num];
    sr.send(val);
    };

    sr.segment = function(s) {
    sr.clear();

    var val = commonAnode ? invert(segments[s]) : segments[s];
    sr.send(val);
    };

    sr.clear = function() {
    sr.send( commonAnode ? 255 : 0 );
    };

    board.repl.inject({
    sr: sr,
    digits: digits,
    segments: segments
    });

    });