My example on 123d.circuits.io

https://123d.circuits.io/circuits/1166640-74hc595-shift-register-exercise (:74hc959-shift-register.png?direct&700|)

My second example on 123d.circuits.io (with 2x 74hc595)


Binary counter

int clockPin = 10; //SH_CP (SHift register Clock Pulse) int latchPin = 11; //ST_CP (STorage Clock Pulse) int dataPin = 12; //DS int address = 0; byte data = 0;

void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); }

void loop() { digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting shiftOut(dataPin, clockPin, MSBFIRST, 0); //flags enzo shiftOut(dataPin, clockPin, MSBFIRST, address >> 8); //address HIGH shiftOut(dataPin, clockPin, MSBFIRST, address); //address LOW shiftOut(dataPin, clockPin, MSBFIRST, data); // data digitalWrite(latchPin, HIGH); delay(50);

data++; address++; }

# read from 74HC166 and write to 74HC959
<code c>
const byte CLOCK = 8;         // CP   on 74hc166 and SHCP on 74hc959
const byte SWITCH_SELECT = 9; // !PE  on 74hc166
const byte SWITCH_DATA = 10;  // Q7   on 74hc166
const byte LED_SELECT = 11;   // STCP on 74hc959
const byte LED_DATA = 12;     // DS   on 74hc959

void setup() {
  pinMode(CLOCK, OUTPUT);
  pinMode(LED_DATA, OUTPUT);  

void loop() {
  //read switches
  digitalWrite(SWITCH_SELECT, LOW);
  digitalWrite(CLOCK, HIGH);
  digitalWrite(SWITCH_SELECT, HIGH);
  byte al = shiftIn(SWITCH_DATA, CLOCK, MSBFIRST);
  byte ah = shiftIn(SWITCH_DATA, CLOCK, MSBFIRST);

  //write leds
  digitalWrite(LED_SELECT, LOW);
  shiftOut(LED_DATA, CLOCK, MSBFIRST, 0);  //flags
  shiftOut(LED_DATA, CLOCK, MSBFIRST, ah); //address high
  shiftOut(LED_DATA, CLOCK, MSBFIRST, al); //address low
  shiftOut(LED_DATA, CLOCK, MSBFIRST, 0);  //data
  digitalWrite(LED_SELECT, HIGH);