Help controlling a 595 shift register

Hi, i am trying to control a pair of 74hc595n's. I did this successfully using python and the RPi, and having set the hardware up the same way i cannot work out how to output how i desire.

int dataPin = 2;
int latchPin = 3;
int clockPin = 4;
int data_out[] = {0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1};

void setup() {
  // put your setup code here, to run once:

pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() {
  
  // put your main code here, to run repeatedly: 
  digitalWrite(latchPin, LOW);
  for (int thisLed = 0; thisLed < 16; thisLed++) 
  
    if (data_out[thisLed] != 0)
      digitalWrite(dataPin, HIGH);
    else 
      digitalWrite(dataPin, LOW);
    
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, HIGH);
  delay(500);
}

when this is uploaded to my nano all the outputs are high! can anyone see what i have done wrong?

Hi, I think you would be better off using the shiftOut() function. Also, pack your data bits into bytes with "B00010011" instead of "{0,0,0,1,0,0,1,1}".

I think the reason your code doesn't work is that there are some missing {}, so the compiler doesn't know which commands are inside the for loop. In Python, the line indentation tells the compiler what's in the loop, but C needs those brackets!

Paul

thanks for the reply

i knew there was a shift function, but i wanted to try and do it by hand to get used to arduino and C

i'll search out those curly brackets now :slight_smile:

EDIT:
curly brackets fixed it. thank you

zomzilla:
Hi, i am trying to control a pair of 74hc595n's. I did this successfully using python and the RPi, and having set the hardware up the same way i cannot work out how to output how i desire.

int dataPin = 2;

int latchPin = 3;
int clockPin = 4;
int data_out[] = {0,1,0,1, 0,1,0,1, 0,1,0,1, 0,1,0,1};

void setup() {
  // put your setup code here, to run once:

pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);

}

void loop() {
 
  // put your main code here, to run repeatedly:
  digitalWrite(latchPin, LOW);
  for (int thisLed = 0; thisLed < 16; thisLed++)
 
    if (data_out[thisLed] != 0)
      digitalWrite(dataPin, HIGH);
    else
      digitalWrite(dataPin, LOW);
   
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, HIGH);
  delay(500);
}




when this is uploaded to my nano all the outputs are high! can anyone see what i have done wrong?

This isn't Python, you need to put in some { and } to show which statements are inside the loop.

By the looks of it all you're doing is setting the data pin to high and then sending your data..

But the clock is not toggled back to low/high until your loop finishes

Yes, use shiftOut. It's easiest for most shift register needs. IF you want to really do it manually.... go find the shiftOut function in the "wiring_shift.c" library (somewhere in your Arduino folder) and dissect the code it uses.

Use SPI, way faster:

digitalWrite(ssPin, LOW);
SPI.transfer(your_data_byte);
digitalWrite (ssPin, HIGH);

and then replace the digitalWrite's with direct port manipulation, such as:

PORTB = PORTB & B11111011; // clear PB2 (SS pin, D10)
SPI.transfer(your_data_byte);
PORTB = PORTB | B00000100; // set PB2 (SS pin, D10)

Got fast hardware for shifting out data, learn to use it.