Go Down

Topic: delay() causing array data to increment !?! (Read 786 times) previous topic - next topic


Hi, I've started playing around with a few 74HC595's to light a few LEDs but when I use the delay function it begins incrementing the 17th element by the amount of ms I'm delaying by ( give or take a little bit ).

Code: [Select]

int latchPin = 3; // RCLK  (12)
int clockPin = 4; // SRCLK (11)
int dataPin  = 2; // SER   (14)
int data[16];

void setup() {

  pinMode( clockPin, OUTPUT );
  pinMode( latchPin, OUTPUT );
  pinMode( dataPin, OUTPUT );
  // Turn on all blue LEDs
  for( int i = 0; i < 18 ; i++ ){
    if( (i%3) - 2 == 0 )
      data[i] = 1;
      data[i] = 0;
  Serial.begin( 115200 );

void loop() {
  // On board !OE is LOW, !SRCLR is HIGH
  // By pulsing the clock pin ( SRCLK / Shift Register CLK ),
  // The low to high transition shifts the data from the data pin.
  // (To take input from the pin, and move data across)
  // By pulsing the latch pin ( RCLK / Register CLK ),
  // The low to high transition shifts the data to the outputs
  // (Turns on the LEDs based on data entered using SRCLK and SER)
  // Use once every 18 clocks for 6 rgb LEDs
  for( int i = 17; i >= 0; i-- ){
    Serial.print( data[i] );
    Serial.print( " " );

// Pass an array 16 bits long, then display based on the array
void writeData(){
  // set latch pin low
  digitalWrite( latchPin, LOW );

  for( int i = 17; i >= 0 ; i-- ){  // start loop
  // check value in data
  // if 1 then set data high
    if( data[i] == 1 )
      digitalWrite( dataPin, HIGH );
    else  // if 0 then set data low
      digitalWrite( dataPin, LOW );

  // clock
    digitalWrite( clockPin, HIGH );
    digitalWrite( clockPin, LOW );
    digitalWrite( dataPin, LOW );
  } // end loop
  // latch data (set latch pin high)
  digitalWrite( latchPin, HIGH );

note: Because i'm not using shiftOut(), when the array is printed and sent to the shift registers it is sent backwards (last element sent first and the first element last)

Does anybody know why this is happening????


Your data array has 16 elements but you're treating it as though it has 18


:smiley-eek: oops, well that's a bit of an embarrassing mistake.
thanks, works fine now. wish the IDE would pick up on those things sometimes.


Which means some random variable(s) (whichever ones the compiler decided to place after the array)
is being stomped on.  In C its entirely your responsibility to avoid such "buffer overruns" so you won't see
any useful compile-time warnings/errors about it.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131