0

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);
}
2
  • 1
    My code for shifting doesn't seem working. - this is not a helpful description. What happens? Too many LEDs light up? Too few? They change randomly? The program stops doing anything? "Not working" can mean anything. Try ringing your local garage and saying your car is "not working" - they will soon start asking you if you are having trouble starting it, steering it, if the radio is working, if the tyres are flat, etc.
    – Nick Gammon
    Jul 21 '16 at 7:09
  • Why implement your own shiftOut, when it already exists and is more efficient than yours, besides? Theres no need to 'zero out' the data pin after raising the clock HIGH; how do you even know that the register has read the bit you just sent? Read about 'Hold time'. Jul 21 '16 at 12:17
1

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:

595 wiring schematic

More details on my page about the 595 chip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.