Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Play with 74HC595 Shift Register

TuesdayIOTTuesdayIOT France
edited October 9 in Mongoose OS

Hello,

I share with you a minimal application to use with a shift register and 8 Leds on ESP8266.
I used D2 (GPIO4) D3 (GPIO0) D4 (GPIO2).

_load('api_gpio.js');
load('api_timer.js');

let dataPin =   4;
let clockPin =  2;
let latchPin =  0;
let value = 0;

let train = ([0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0]);

GPIO.set_mode(dataPin, GPIO.MODE_OUTPUT);
GPIO.set_mode(clockPin, GPIO.MODE_OUTPUT);
GPIO.set_mode(latchPin, GPIO.MODE_OUTPUT);

function next(i) {
    GPIO.write(latchPin, 0);
    shiftOut(dataPin, clockPin, i);
    GPIO.write(latchPin, 1);
  }

function shiftOut(dataPin, clockPin, value) {
// much thanks to http://www.sqlskills.com/blogs/paulselec/post/Arduino-figuring-out-shift-registers.aspx
for (let mask = 128; mask > 0; mask = mask >> 1) {
  GPIO.write(clockPin, 0);
  GPIO.write(dataPin, value & mask ? 1 : 0);
  GPIO.write(clockPin, 1);
  }
}

// Display Train Progression each x MilliSeconds
Timer.set(70 /* 1 sec */, Timer.REPEAT, function() {
 next(train[value]);
 value++;
 if (value === train.length ) { value = 0 }
}, null);
Thanked by 1Generator
Sign In or Register to comment.