Problems controlling 74HC595 from a Library, Logic Error Somewhere? Please Help

I’m trying to take this code that works as expected and turn it into a library I can easily reuse throughout my projects. I have taken the code and created a library that compiles just fine, but gives unexpected results with the LEDS. Can anyone see what I’m doing wrong? Please help if you can. Thanks!

The code that works as expected:

int SER_Pin = 11; //pin 14 on the 75HC595 // DATA PIN
int RCLK_Pin = 8; //pin 12 on the 75HC595 // LATCH PIN
int SRCLK_Pin = 12; //pin 11 on the 75HC595 // CLOCK PIN

//Number of shift registers p resent.
#define number_of_74hc595s 1

//Define number of output pins based on the number of shift registers present.
#define numOfRegisterPins number_of_74hc595s * 8

// Accelerometer connected to analog pins 0, 1, 2 (x, y, z)
#define xPin 0
#define yPin 1
#define zPin 2

boolean registers[numOfRegisterPins];

void setup(){

Serial.begin(9600);
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);

pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(zPin, INPUT);

//reset all register pins
clearRegisters();
writeRegisters();
}

//set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i >= 0; i–){
registers = LOW;

  • }*
    }
    //Set and display registers
    //Only call AFTER all values are set how you would like (slow otherwise)
    void writeRegisters(){
  • digitalWrite(RCLK_Pin, LOW); // unlatch so we can write changes*
  • for(int i = numOfRegisterPins - 1; i >= 0; i–){*
  • digitalWrite(SRCLK_Pin, LOW);*
    _ int val = registers*;_
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
    _
    }_
    digitalWrite(RCLK_Pin, HIGH);
    _
    }_
    _
    //set an individual pin HIGH or LOW*_
    void setRegisterPin(int index, boolean value){
    * registers[index] = value;*
    }
    void loop(){
    * setRegisterPin(0, HIGH);*
    * setRegisterPin(1, LOW);*
    * setRegisterPin(2, HIGH);*
    * setRegisterPin(3, LOW);*
    * setRegisterPin(4, HIGH);*
    * setRegisterPin(5, LOW);*
    * setRegisterPin(6, HIGH);*
    * setRegisterPin(7, LOW);*
    * writeRegisters(); //MUST BE CALLED TO DISPLAY CHANGES*
    * //Only call after the values are set how you need.*

_ Serial.print(analogRead(xPin));
Serial.print(",");
Serial.print(analogRead(yPin));
Serial.print(",");
Serial.println(analogRead(zPin));_

* delay(500);*
* clearRegisters();*

* setRegisterPin(0, LOW);*
* setRegisterPin(1, HIGH);*
* setRegisterPin(2, LOW);*
* setRegisterPin(3, HIGH);*
* setRegisterPin(4, LOW);*
* setRegisterPin(5, HIGH);*
* setRegisterPin(6, LOW);*
* setRegisterPin(7, HIGH);*
* writeRegisters();*
* delay(500);*
}
[/quote]
Here is the code for the library and sketch :
```
/
 SR74HC595.cpp - Library for working with the 74HC595 Shift Register
*/

#include <Arduino.h>
#include <SR74HC595.h>

SR74HC595::SR74HC595(int dataPin, int latchPin, int clockPin, int numOfRegisterPins)
{
_DATA_Pin = dataPin;
_LATCH_Pin = latchPin;
_CLOCK_Pin = clockPin;
numberOfRegisterPins = numOfRegisterPins;
registers[numberOfRegisterPins];

pinMode(_DATA_Pin, OUTPUT);
pinMode(_LATCH_Pin, OUTPUT);
pinMode(_CLOCK_Pin, OUTPUT);

}

void SR74HC595::ClearRegisters()
{
   for(int i = numberOfRegisterPins-1; i >= 0; i–)
{
      registers[i] = LOW;
   }
}

void SR74HC595::WriteRegisters()
{
   digitalWrite(_LATCH_Pin, LOW);  // unlatch so we can write changes

for(int i = numberOfRegisterPins-1; i >= 0; i–){
     digitalWrite(_CLOCK_Pin, LOW);

int val = registers[i];

digitalWrite(_DATA_Pin, val);
     digitalWrite(_CLOCK_Pin, HIGH);

}
   digitalWrite(_LATCH_Pin, HIGH);

}

void SR74HC595::SetRegisterPin(int index, boolean value)
{
registers[index] = value;
}*

*_ _*
/
 SR74HC595.h - Library for working with the 74HC595 Shift Register
*/

#ifndef SR74HC595_h
#define SR74HC595_h

#include <Arduino.h>

class SR74HC595
{
 private:
 int _DATA_Pin;   // Pin 14 on the 75HC595
 int _LATCH_Pin;  // Pin 12 on the 75HC595
 int _CLOCK_Pin;  // Pin 11 on the 75HC595
 
 int numberOfRegisterPins;

public:

boolean registers;
   
 SR74HC595(int dataPin, int latchPin, int clockPin, int numOfRegisterPins);
 void ClearRegisters();
 void WriteRegisters();
 void SetRegisterPin(int index, boolean value);
};

#endif*

```
> #include <SR74HC595.h>
>
> #define DATA_Pin 11
> #define LATCH_Pin 8
> #define CLOCK_Pin 12
>
> #define NumberOfRegisterPins 8
>
> SR74HC595 ShiftRegister(DATA_Pin, LATCH_Pin, CLOCK_Pin, NumberOfRegisterPins);
>
> void setup()
> {
> Serial.begin(9600);
> ShiftRegister.ClearRegisters();
> }
>
> void loop()
> {
>
> ShiftRegister.SetRegisterPin(0, HIGH);
> ShiftRegister.SetRegisterPin(1, LOW);
> ShiftRegister.SetRegisterPin(2, HIGH);
> ShiftRegister.SetRegisterPin(3, LOW);
> ShiftRegister.SetRegisterPin(4, HIGH);
> ShiftRegister.SetRegisterPin(5, LOW);
> ShiftRegister.SetRegisterPin(6, HIGH);
> ShiftRegister.SetRegisterPin(7, LOW);
>
> ShiftRegister.WriteRegisters(); //MUST BE CALLED TO DISPLAY CHANGES
> //Only call after the values are set how you need.
*> *
*> *
> delay(500);
*> *
> ShiftRegister.ClearRegisters();
>
> ShiftRegister.SetRegisterPin(0, LOW);
> ShiftRegister.SetRegisterPin(1, HIGH);
> ShiftRegister.SetRegisterPin(2, LOW);
> ShiftRegister.SetRegisterPin(3, HIGH);
> ShiftRegister.SetRegisterPin(4, LOW);
> ShiftRegister.SetRegisterPin(5, HIGH);
> ShiftRegister.SetRegisterPin(6, LOW);
> ShiftRegister.SetRegisterPin(7, HIGH);
>
> ShiftRegister.WriteRegisters();
*> *
> delay(500);
> }

Whoops, solved the problem. Looks like I was overflowing my array. Thanks anyways guys/gals!