Communication between Arduino and IC 74hc595

Hallo every one

I have some trouble communicating with my ICs 74hc595.

I have designed a sketch which I already rebuild, but I have no experience how should I control them.

Thanks

P.S. If this post is in a wrong topic, I'm sorry, I'm new here

Here I post my code that has been worked well with
3 lines of 7segment, each lines consist of 4 7segment

line 1: 7 7 7 7
line 2: 7 7 7 7
line 3: 7 7 7 7
it means, each line using 4 IC 74hc595.

Hope you may understand. :smiley:

void setup();
void loop();
void increment (int line);
void show(int numberToDisplay,int fromLine);
void sendSerialData (byte registerCount,byte *pValueArray,int fromLine);
void sendSerialDatainitial(byte registerCount,byte *pValueArray,int fromLine);
int switchPin = A5;
int no1=1;
int no2=1;
int no3=1;


// Which pins are connected to which LED
const byte redLED0 = 2;

const byte redLED1 = 3;
const byte redLED2 = 4;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long redLED0interval = 2000;
const unsigned long redLED1interval = 3000;
const unsigned long redLED2interval = 4000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long redLED0timer;
unsigned long redLED1timer;
unsigned long redLED2timer;

// This pin gets sets low when I want the 595s to listen
const int  pinCommLatch1 = 5;
const int  pinCommLatch2 = 6;
const int  pinCommLatch3 = 7;

// This pin is used to pass the next bit
const int  pinData1    = 8;
const int  pinData2    = 9;
const int  pinData3    = 10;

// This pin is used by ShiftOut to toggle to say there's another bit to shift
const int  pinClock1     = 11;
const int  pinClock2     = 12;
const int  pinClock3     = 13;

// Definitions of the 7-bit values for displaying digits
byte g_digits [10];

//Machine Config in integer array
int MachineConfig[3][3];

// Current number being displayed
int g_numberToDisplay = 0;

// Number of shift registers in use
const int g_registers = 4;

// Array of numbers to pass to shift registers
byte g_registerArray [g_registers];

void setup()
{
  pinMode (redLED0, OUTPUT);
  pinMode (pinCommLatch1, OUTPUT);
  pinMode (pinData1,OUTPUT);
  pinMode (pinClock1,OUTPUT);
  //config for line 1
  MachineConfig[0][0]=5;   //pinCommLatch line   1
  MachineConfig[0][1]=8;   //pinData line        1
  MachineConfig[0][2]=11;  //pinClock line       1
 
  //config for line 2
  MachineConfig[1][0]=6;   //pinCommLatch line   2
  MachineConfig[1][1]=9;   //pinData line        2
  MachineConfig[1][2]=12;  //pinClock line       2
  
  //config for line 3
  MachineConfig[2][0]=7;   //pinCommLatch line   3
  MachineConfig[2][1]=10;  //pinClock line       3
  MachineConfig[2][2]=13;  //pinData line        3
  
  // Setup the digits array
  g_digits [0] = 1 + 2 + 4 + 8 + 16 + 32 + 00;
  g_digits [1] = 0 + 2 + 4 + 0 + 00 + 00 + 00;
  g_digits [2] = 1 + 2 + 0 + 8 + 16 + 00 + 64;
  g_digits [3] = 1 + 2 + 4 + 8 + 00 + 00 + 64;         
  g_digits [4] = 0 + 2 + 4 + 0 + 00 + 32 + 64;              
  g_digits [5] = 1 + 0 + 4 + 8 + 00 + 32 + 64;         
  g_digits [6] = 1 + 0 + 4 + 8 + 16 + 32 + 64;
  g_digits [7] = 1 + 2 + 4 + 0 + 00 + 00 + 00;                   
  g_digits [8] = 1 + 2 + 4 + 8 + 16 + 32 + 64;
  g_digits [9] = 1 + 2 + 4 + 8 + 00 + 32 + 64;     
  //for loop 0-2 int
  for(int i=0;i<3;i++){
    sendSerialDatainitial(g_registers, g_registerArray,i);
  }
}

void sendSerialDatainitial(   
  byte registerCount,  // How many shift registers?
  byte *pValueArray,int fromLine)   // Array of bytes with LSByte in array [0]
{ 
   // g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [0];
    
    int PINCommLatch=MachineConfig[fromLine][0];
    int PINData=MachineConfig[fromLine][1];
    int PINClock=MachineConfig[fromLine][2];
      
    pinMode (PINCommLatch, OUTPUT);
    pinMode (PINData, OUTPUT);  
    pinMode (PINClock, OUTPUT);
    
  // Signal to the 595s to listen for data
  digitalWrite (PINCommLatch, LOW);
  
  for (byte reg = registerCount; reg > 0; reg--)
  {
    byte value = pValueArray [reg-1];

    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (PINClock, LOW);
    
      digitalWrite (PINData, value & bitMask ? HIGH : LOW);
        
      digitalWrite (PINClock, HIGH);
    }
  }
  // Signal to the 595s that I'm done sending
  digitalWrite (PINCommLatch, HIGH);
}  // sendSerialData


// Simple function to send serial data to one or more shift registers by iterating backwards through an array.
// Although g_registers exists, they may not all be being used, hence the input parameter.
void sendSerialData (
  byte registerCount,  // How many shift registers?
  byte *pValueArray, // Array of bytes with LSByte in array [0]
  int  fromLine)   
{
  int PINCommLatch=MachineConfig[fromLine][0];
  int PINData=MachineConfig[fromLine][1];
  int PINClock=MachineConfig[fromLine][2];
  
  // Signal to the 595s to listen for data
  digitalWrite (PINCommLatch, LOW);
  
  for (byte reg = registerCount; reg > 0; reg--)
  {
    byte value = pValueArray [reg - 1];

    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (PINClock, LOW);
    
      digitalWrite (PINData, value & bitMask ? HIGH : LOW);
        
      digitalWrite (PINClock, HIGH);
    }
  }
  // Signal to the 595s that I'm done sending
  digitalWrite (PINCommLatch, HIGH);
}  // sendSerialData


void loop()
{  
  if (digitalRead(switchPin))
  {
    blinks();
  }
}

void blinks() 
{
  // Handling the blink of one LED.
  if ( (millis () - redLED0timer) >= redLED0interval)
     {
     digitalWrite (redLED0, HIGH);
     delay (1000);
     digitalWrite (redLED0, LOW);
     increment (0);
     redLED0timer = millis ();
     }
   
   if ( (millis () - redLED1timer) >= redLED1interval)
     {
     digitalWrite (redLED1, HIGH);
     digitalWrite (redLED1, LOW);
     increment (1);
     redLED1timer = millis ();
     }
     
   if ( (millis () - redLED2timer) >= redLED2interval)
     {
     digitalWrite (redLED2, HIGH);
     digitalWrite (redLED2, LOW);
     increment (2);
     redLED2timer = millis ();
     }
    
}
 /* int line1 = random (1,3);
  int line2 = random (1,4);
  int line3 = random (1,6);
    
  int k=0;
  if(line1==1)
  {
      digitalWrite(2, HIGH);   // set the LED on
      delay(300);
      digitalWrite(2, LOW);  
      increment(0);    
  }
  
  if(line2==1)
  {
      digitalWrite(3, HIGH);   // set the LED on
      delay(300);
      digitalWrite(3, LOW);  
      increment(1);    
  }
    
  if(line3==1)
  {
      digitalWrite(4, HIGH);   // set the LED on
      delay (200);
      digitalWrite(4, LOW); 
      increment(2);    
  }   
}*/

void increment(int line)
{
  if(line==0)
  {
   g_numberToDisplay = no1;
   no1++;
   show(g_numberToDisplay,line);
  }else if(line==1)
  {
   g_numberToDisplay = no2;
   no2++;
   show(g_numberToDisplay,line);
  }
  else if(line==2)
  {
  g_numberToDisplay = no3;
  no3++; 
  show(g_numberToDisplay,line);
  }
}
   
void show(int numberToDisplay,int fromLine)
{
  if (numberToDisplay < 10)
  {
  //  g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [numberToDisplay];
  }
  else if (numberToDisplay < 100)
  {
    //g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [numberToDisplay / 10];
    g_registerArray [0] = g_digits [numberToDisplay % 10];
  }
  else if (numberToDisplay < 1000)
  {
    //g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [numberToDisplay / 100];
    g_registerArray [1] = g_digits [(numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [numberToDisplay % 10];
  }
  else
  {
    //g_registerArray [4] = g_digits [0];
    g_registerArray [3] = g_digits [numberToDisplay / 1000];
    g_registerArray [2] = g_digits [(numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [numberToDisplay % 10];
  }
 // else
  //{
    //g_registerArray [4] = g_digits [numberToDisplay / 10000];
    //g_registerArray [3] = g_digits [(numberToDisplay % 10000) / 1000];
    //g_registerArray [2] = g_digits [(numberToDisplay % 1000) / 100];
    //g_registerArray [1] = g_digits [(numberToDisplay % 100) / 10];
    //g_registerArray [0] = g_digits [numberToDisplay % 10];    
  //}
  sendSerialData (g_registers, g_registerArray, fromLine);
}