user24074: Im trying to shift LEDs using 595 register. Using SN74HC595N registers. Im using 4 of the registers as i got alots of LEDs to shift. My code for shifting doesn't seem working.. i can shift some with 2 registers but not 4 registers  
// include the library code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
// RS-12, E-13, D4-8, D5-9, D6-10, D7-11
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);

int row[]={8,9,10,11};// Digital output connect directly to all rows
int col[] = {A0,A1,A2}; // Analog input with 10K pull-up to 5V.
int i,j; // Two counter variables to count inside for loop
int col_scan; // Variable to hold value of scanned columns

//Pin connected to ST_CP of 74HC595
int latchPin = 3;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 5;

//holder for infromation you're going to pass to shifting function
byte data = 0; 
byte data1 = 0x55;
byte data2 = 0xAA;


void setup()
{
  Wire.begin();
  RTC.begin();
  Serial.begin(9600);
  for(i=0;i<=3;i++)
  {
    pinMode(row[i],OUTPUT);
    //pinMode(col[i],INPUT);
    //digitalWrite(col[i],HIGH);
  } 

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Medicine Box");

  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);

  if (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }


  for (i=0;i<=4; i++)
  {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, data1);
    shiftOut(dataPin, clockPin, data1);
    digitalWrite(latchPin, 1);
    delay(500);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, data2);
    shiftOut(dataPin, clockPin, data2);
    digitalWrite(latchPin, 1);
    delay(500); 
  }
}

void loop()
{ 

  DateTime now = RTC.now();
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
  //Serial.println(now);
  //Serial.print("    ");
   /*   Serial.print(now.day(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println(" ");*/

      lcd.print(now.day(), DEC);
      lcd.print('/');
      lcd.print(now.month(), DEC);
      lcd.print('/');
      lcd.print(now.year()-2000, DEC);
      lcd.print(' ');
      if (now.hour()<10)
        lcd.print('0');
      lcd.print(now.hour(), DEC);
      lcd.print(':');
      if (now.minute()<10)
        lcd.print('0');
      lcd.print(now.minute(), DEC);
      lcd.print(':');
      if (now.second()<10)
        lcd.print('0');
      lcd.print(now.second(), DEC);
      //lcd.println(" ");

  for(i=0; i<=3; i++)
  {
    digitalWrite(row[0],HIGH);
    digitalWrite(row[1],HIGH);
    digitalWrite(row[2],HIGH);
    digitalWrite(row[3],HIGH);
    digitalWrite(row[i],LOW);

    for(j=0; j<=2; j++)
    {
      col_scan=analogRead(col[j]);
      if(col_scan < 1000)
      {
        while(analogRead(col[j])< 1000);
        keypress(i,j);
      }
    }
  }
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, data1);
  shiftOut(dataPin, clockPin, data1);
  digitalWrite(latchPin, 1);
}

void keypress(int i, int j)
{
  if(i==0&&j==0)
    Serial.println("1");
  else if(i==0&&j==1)
    Serial.println("2");
  else if(i==0&&j==2)
    Serial.println("3");
  else if(i==0&&j==3)
    Serial.println("A");
  else if(i==1&&j==0)
    Serial.println("4");
  else if(i==1&&j==1)
    Serial.println("5");
  else if(i==1&&j==2)
    Serial.println("6");
  else if(i==1&&j==3)
    Serial.println("B");
  else if(i==2&&j==0)
    Serial.println("7");
  else if(i==2&&j==1)
    Serial.println("8");
  else if(i==2&&j==2)
    Serial.println("9");
  else if(i==2&&j==3)
    Serial.println("C");
  else if(i==3&&j==0)
    Serial.println("*");
  else if(i==3&&j==1)
    Serial.println("0");
  else if(i==3&&j==2)
    Serial.println("#");
  else if(i==3&&j==3)
    Serial.println("D");
}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


user10794: Your code looks unnecessarily convoluted for merely sending data to a shift register. You can use the SPI hardware for that.
For example:
#include <SPI.h>

const byte LATCH = 10;

void setup ()
{
  SPI.begin ();
}  // end of setup

void loop ()
{
  digitalWrite (LATCH, LOW);
  SPI.transfer (0xAB);
  SPI.transfer (0xCD);
  SPI.transfer (0xEF);
  SPI.transfer (0x42);  
  digitalWrite (LATCH, HIGH);
  delay (20);
}  // end of loop

You have to use the SPI hardware pins for this to work. On a Uno these are:

Chip pin 14 (DS) goes to MOSI  (D11)
Chip pin 11 (SH) goes to SCK   (D13)
Chip pin 12 (ST) goes to SS    (D10)  (or any other pin by changing LATCH)

Example wiring:

More details on my page about the 595 chip.

24074: <p>Im trying to shift LEDs using 595 register. Using SN74HC595N registers. Im using 4 of the registers as i got alots of LEDs to shift. My code for shifting doesn't seem working.. i can shift some with 2 registers but not 4 registers  </p>

<pre><code>// include the library code:
#include &lt;LiquidCrystal.h&gt;
#include &lt;Wire.h&gt;
#include "RTClib.h"

RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
// RS-12, E-13, D4-8, D5-9, D6-10, D7-11
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);

int row[]={8,9,10,11};// Digital output connect directly to all rows
int col[] = {A0,A1,A2}; // Analog input with 10K pull-up to 5V.
int i,j; // Two counter variables to count inside for loop
int col_scan; // Variable to hold value of scanned columns

//Pin connected to ST_CP of 74HC595
int latchPin = 3;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 5;

//holder for infromation you're going to pass to shifting function
byte data = 0; 
byte data1 = 0x55;
byte data2 = 0xAA;


void setup()
{
  Wire.begin();
  RTC.begin();
  Serial.begin(9600);
  for(i=0;i&lt;=3;i++)
  {
    pinMode(row[i],OUTPUT);
    //pinMode(col[i],INPUT);
    //digitalWrite(col[i],HIGH);
  } 

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Medicine Box");

  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);

  if (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date &amp; time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }


  for (i=0;i&lt;=4; i++)
  {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, data1);
    shiftOut(dataPin, clockPin, data1);
    digitalWrite(latchPin, 1);
    delay(500);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, data2);
    shiftOut(dataPin, clockPin, data2);
    digitalWrite(latchPin, 1);
    delay(500); 
  }
}

void loop()
{ 

  DateTime now = RTC.now();
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
  //Serial.println(now);
  //Serial.print("    ");
   /*   Serial.print(now.day(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(' ');
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println(" ");*/

      lcd.print(now.day(), DEC);
      lcd.print('/');
      lcd.print(now.month(), DEC);
      lcd.print('/');
      lcd.print(now.year()-2000, DEC);
      lcd.print(' ');
      if (now.hour()&lt;10)
        lcd.print('0');
      lcd.print(now.hour(), DEC);
      lcd.print(':');
      if (now.minute()&lt;10)
        lcd.print('0');
      lcd.print(now.minute(), DEC);
      lcd.print(':');
      if (now.second()&lt;10)
        lcd.print('0');
      lcd.print(now.second(), DEC);
      //lcd.println(" ");

  for(i=0; i&lt;=3; i++)
  {
    digitalWrite(row[0],HIGH);
    digitalWrite(row[1],HIGH);
    digitalWrite(row[2],HIGH);
    digitalWrite(row[3],HIGH);
    digitalWrite(row[i],LOW);

    for(j=0; j&lt;=2; j++)
    {
      col_scan=analogRead(col[j]);
      if(col_scan &lt; 1000)
      {
        while(analogRead(col[j])&lt; 1000);
        keypress(i,j);
      }
    }
  }
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, data1);
  shiftOut(dataPin, clockPin, data1);
  digitalWrite(latchPin, 1);
}

void keypress(int i, int j)
{
  if(i==0&amp;&amp;j==0)
    Serial.println("1");
  else if(i==0&amp;&amp;j==1)
    Serial.println("2");
  else if(i==0&amp;&amp;j==2)
    Serial.println("3");
  else if(i==0&amp;&amp;j==3)
    Serial.println("A");
  else if(i==1&amp;&amp;j==0)
    Serial.println("4");
  else if(i==1&amp;&amp;j==1)
    Serial.println("5");
  else if(i==1&amp;&amp;j==2)
    Serial.println("6");
  else if(i==1&amp;&amp;j==3)
    Serial.println("B");
  else if(i==2&amp;&amp;j==0)
    Serial.println("7");
  else if(i==2&amp;&amp;j==1)
    Serial.println("8");
  else if(i==2&amp;&amp;j==2)
    Serial.println("9");
  else if(i==2&amp;&amp;j==3)
    Serial.println("C");
  else if(i==3&amp;&amp;j==0)
    Serial.println("*");
  else if(i==3&amp;&amp;j==1)
    Serial.println("0");
  else if(i==3&amp;&amp;j==2)
    Serial.println("#");
  else if(i==3&amp;&amp;j==3)
    Serial.println("D");
}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i&gt;=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut &amp; (1&lt;&lt;i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}
</code></pre>
 10794: <p>Your code looks unnecessarily convoluted for merely sending data to a shift register. You can use the SPI hardware for that.</p>

<p>For example:</p>

<pre class="lang-c++ prettyprint-override"><code>#include &lt;SPI.h&gt;

const byte LATCH = 10;

void setup ()
{
  SPI.begin ();
}  // end of setup

void loop ()
{
  digitalWrite (LATCH, LOW);
  SPI.transfer (0xAB);
  SPI.transfer (0xCD);
  SPI.transfer (0xEF);
  SPI.transfer (0x42);  
  digitalWrite (LATCH, HIGH);
  delay (20);
}  // end of loop
</code></pre>

<p>You have to use the SPI hardware pins for this to work. On a Uno these are:</p>

<ul>
<li>Chip pin 14 (DS) goes to MOSI  (D11)</li>
<li>Chip pin 11 (SH) goes to SCK   (D13)</li>
<li>Chip pin 12 (ST) goes to SS    (D10)  (or any other pin by changing LATCH)</li>
</ul>

<p>Example wiring:</p>

<p><a href="https://i.stack.imgur.com/Vmhgi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vmhgi.png" alt="595 wiring schematic"></a></p>

<p>More details on my <a href="http://www.gammon.com.au/forum/?id=11518" rel="nofollow noreferrer">page about the 595 chip</a>.</p>
