2

I'm having trouble with using parallel shift registers, I've studied example codes and I've come up with this function to set the pins to on or off. The problem is the first time the function runs all the output pins are set to HIGH regardless of the values in the array.

    void updateRegister(int ClockPin,int DataPin,int LatchPin){
      int pinState;//Holds the state of the data pin
      pinMode(ClockPin, OUTPUT);//Sets the pinmode of the pins
      pinMode(DataPin, OUTPUT);//Sets the pinmode of the pins
      pinMode(LatchPin, OUTPUT);
      digitalWrite(LatchPin, 0);//Turns latch off to prevent bleedover while shifting

      digitalWrite(DataPin, 0);//clear everything
      digitalWrite(ClockPin, 0);//clear everything

      for (int i=1; i<=16; i++){//Repeats 16 times with i increment from 1 to 16
          if (Qout[i]){pinState= 1;}//Checks the array for the value and if it is true sets the pinstate to 1, HIGH
          else{pinState= 1;}//If it equals false set it to 0, LOW

          digitalWrite(DataPin, pinState);//Sets the data pin depening on the pinstate
          digitalWrite(ClockPin, 1);
          digitalWrite(DataPin, 0);
          digitalWrite(ClockPin, 0);
        }
      digitalWrite(LatchPin, 1);//Turns latch pin back on
    }

I'm storing the boolean values of the each pin in an array called Qout which is defined and all set to false like this

    boolean Qout [17];//Holds the state for each pin
    for (int i = 1; i < 17; i++ ) {Qout[i]=false;}//Sets all output to off

I know I should start at Qout[0] but it just makes it way more confusing.

Can someone help me fix my function so that it actually works.

This is what I based it on ShftOut23

migrated from stackoverflow.com May 14 '14 at 16:26

This question came from our site for professional and enthusiast programmers.

  • 1
    Try bringing ClockPin down before bringing DataPin down and also add some delay before clearing ClockPin(say 1us). – rmi May 9 '14 at 8:20
  • Why not directly use standard Arduino API shiftOut()? – jfpoilpret May 14 '14 at 17:37
4

First of all, fix the following part of your code:

if (Qout[i]){pinState= 1;}//Checks the array for the value and if it is true sets the pinstate to 1, HIGH
else{pinState= 1;}//If it equals false set it to 0, LOW

into:

if (Qout[i])
{
    //Checks the array for the value and if it is true sets the pinstate to 1, HIGH
    pinState = 1;
}
else
{
    //If it equals false set it to 0, LOW
    pinState = 0;
}

Of course, if you always set pinState to 0, it won't work as you expect :-)

Note that you should rather use HIGH and LOW constants (more readable) instead of 1 and 0.

Finally, you could make the code much simpler this way:

pinState = (Qout[i] ? HIGH : LOW);

Your Answer

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