So I'm driving 6 rgb LEDs using three 74HC595, and have that working. And in my main loop I have a counter that simply increments so that it changes colour every time a delay time is reached, but my problem is the line of code that increments the counter doesn't work unless there is either a delay(x); before or after the increment, where x is any number (including 0). or a Serial.print("") before and after it.
example:
delay(0);
++counter;
if( counter > 2 ){
counter = 0;
}
or
Serial.print( " before " );
++counter;
Serial.print( " after " );
if( counter > 2 ){
counter = 0;
}
work, but
++counter;
if( counter > 2 ){
counter = 0;
}
doesn't
What is happening, because otherwise the counter stays at 2, and the rest of the loop seems to execute fine...
any help would be appreciated, even if it's some stupid error on my part.
Here's all of the code (minus the bounce method).
The code in question is about halfway through.
There are a few variable lying around (specifically the once about brightness and the TIME_DELAY_TYPE) which are unused.
#define NUM_RGB_LEDS 6
#define NUM_LEDS NUM_RGB_LEDS*3
const int latchPin = 3; // RCLK (12)
const int clockPin = 4; // SRCLK (11)
const int dataPin = 2; // SER (14)
int data[NUM_LEDS];
typedef struct {
int red;
int green;
int blue;
byte redBrightness;
byte greenBrightness;
byte blueBrightness;
} LED_TYPE;
typedef struct {
unsigned long dt, t;
float timePassed;
unsigned long delayTime;
unsigned long f;
} TIME_DELAY_TYPE;
LED_TYPE leds[NUM_RGB_LEDS];
TIME_DELAY_TYPE fadeDelay;
int counter = 0;
unsigned long t;
float dt;
float timePassed;
unsigned long delayTime;
float f;
void setup() {
pinMode( clockPin, OUTPUT );
pinMode( latchPin, OUTPUT );
pinMode( dataPin, OUTPUT );
// Turn on all blue LEDs
/* Use data array to set leds
for( int i = 17; i >= 0 ; i-- ){
if( (i+1)%3 == 0 )
data[i] = 1;
else
data[i] = 0;
}
*/
// Use struct array to control LEDs
leds[0].red = 1;
leds[1].green = 1;
leds[2].blue = 1;
leds[3].red = 1;
leds[4].green = 1;
leds[5].blue = 1;
for( int i = 0; i < NUM_RGB_LEDS; i++ ){
leds[i].redBrightness = 255;
leds[i].greenBrightness = 255;
leds[i].blueBrightness = 255;
}
// delayTime in millis
delayTime = 250;
Serial.begin( 115200 );
Serial.println( "Ready" );
// delay( 2000 );
bounce( 1, 0, 25 );
bounce( 1, 1, 25 );
bounce( 1, 2, 25 );
}
void loop() {
// Serial.print( delayTime );
// Serial.print( "\t" );
//Serial.print( timePassed );
// Serial.print( "\t" );
// 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
delay(0);
++counter;
if( counter > 2 ){
counter = 0;
}
// delay without stopping everything
// allows other code to run when this code isn't
if( timePassed > delayTime ){
// Serial.print( "\t" );
// Serial.print( "IN?!?!" );
// Serial.print( "\t" );
// If counter = 0 turn on red
// = 1 green
// = 2 blue
for( int i = 0; i < NUM_RGB_LEDS; i++ ){
if( counter == 0 ){
setLED( i, 1, 0, 0);
}
else if( counter == 1 ){
setLED( i, 0, 1, 0 );
}
else if( counter == 2 ){
setLED( i, 0, 0, 1 );
}
}
Serial.print( counter );
Serial.print( " " );
Serial.println( timePassed );
timePassed = 0;
}// end delay if
writeData();
printFreq();
}
/*
* The data array is sent in reverse order, as the data
* is pushed along as data is entered
* R G B R G B R G B R G B R G B R G B
* 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*/
// Pass an array NUM_LEDS bits long, then display based on the array
// The array is a global variable, along with leds, defined at the start
void writeData(){
// Get values from LED_TYPE and enter into array to send
for(int i = 0; i < NUM_RGB_LEDS; i++ ){
data[ i*3 ] = leds[i].red;
data[ i*3 + 1 ] = leds[i].green;
data[ i*3 + 2 ] = leds[i].blue;
}
// set latch pin low
digitalWrite( latchPin, LOW );
for( int i = NUM_LEDS - 1; 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 );
}
void setLED( int ledNum, int r, int g, int b ){
leds[ ledNum ].red = r;
leds[ ledNum ].green = g;
leds[ ledNum ].blue = b;
}
/*
NOTE: It seems that the Serial.print() functions takes a while to execute
causing the whole code to take longer, also leave Serial to last in code
as (especially when reading the time, micros() ) a discrepency made by waiting
until after Serial to check can cause timing errors.
This was found as "t = micros();" in the following code was previously after
the Serial calls, causing timing errors.
*/
void printFreq(){
dt = micros() - t;
t = micros();
f = 1/(dt/1000000);
// Serial.print( "freq: " );
// Serial.print( f );
// Serial.print( "Hz\tdt: " );
// Serial.print( dt );
// Serial.println( "us" );
timePassed += (dt/1000);
}