Pages: [1]   Go Down
Author Topic: delay() causing array data to increment !?!  (Read 302 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
    else
      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
 
  writeData();
  for( int i = 17; i >= 0; i-- ){
    Serial.print( data[i] );
    Serial.print( " " );
  }
  Serial.println();
 
  //delay(1000);
}

// 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????
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11672
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: