0

I have a circuit with a shift register which controls 8 LEDs: http://node-ardx.org/exercises/5. In code, I created a function cycle() cycle through each LED one at a time, but this occurs too quickly on the board. How can I add a delay inside the loop so that each LED stays on for 500ms?

Here's my code:

var five = require("johnny-five"), board, shiftRegister;

board = new five.Board();

board.on("ready", function() {
  shiftRegister = new five.ShiftRegister({
    pins: {
      data: 2,
      clock: 3,
      latch: 4
    }
  });

  var bytes = [
    00000001,
    00000010,
    00000100,
    00001000,
    00010000,
    00100000,
    01000000,
    10000000,
    00000000
  ];

  function cycle() {
      for ( var i = 0; i < bytes.length; i++ ) {
          console.log(bytes[i]);
          shiftRegister.send(bytes[i]);
      }
    }

  this.repl.inject({
    sr: shiftRegister
  });

  cycle();

});
KatieK
  • 313
  • 1
  • 2
  • 11

2 Answers2

2

Insert a delay of 500ms?

function cycle() {
    for ( var i = 0; i < bytes.length; i++ ) {
        console.log(bytes[i]);
        sr.send(bits[i]);
        delay(500);
    }
  }
Mark Williams
  • 624
  • 4
  • 11
1

The answer is already provided in the article.

  var i = 0; 
  function cycle() {
    console.log(bytes[i]);
    sr.send(bits[i]);
    i++;
    if( i<bytes.length )
      setTimeout(cycle, 300);
  }
Gerben
  • 11,332
  • 3
  • 22
  • 34