Okay, so here is what I'm trying to accomplish:
I have four LEDs, under my fish tank, that I want to fade in and out. I want each LED to fade at a different rate, start at a different "brightness" with PWM, and get a "new" fade-rate every once in a while. Ultimately, it'll make the bottom of the fish tank "pulse" in a "natural" way.
The code I'm using to accomplish this can be found in my GIT repo. There is also a GIF of it "working," there.
Now, here is my problem:
Every once in a while, one of the LEDs (not a specific one) will stop fading and start blinking like a strobe light. I'm not sure what's causing this blinking, since it seems to happen randomly.
The other three LEDs seem to continue to fade in/out while the "bad" LED is spaz-ing out, and it doesn't seem to be isolated to a specific LED. It also doesn't seem to affect only one LED at a time, although it's usually only one LED "blinking" and any given time... but I have seen two going at the same time.
It also doesn't seem to be specific to the "high" or "low" PWM levels, either... it can happen at "bright" levels, "dim" levels, or even in the middle of the fading transition.
Here's what I've tried:
I've tried different Arduino boards, and I've taken the LED count down to a single LED. I've changed variable types from bytes to ints, and back and forth with that tactic. I've been combing through this sketch all day, and I don't see anything that stands out to me as causing the problem.
So, now I'm turning to you all, hoping that someone will be able to see where I've screwed up my code.
Thank you, in advance!
EDIT: Since I've been asked for direct posting of my code, instead of just a link to the source code:
Here is the main .ino file:
#include "led_controller.h"
// Set pin numbers for each LED
byte led1 = 3,
led2 = 6,
led3 = 9,
led4 = 11;
void setup(){
pinMode( led1, OUTPUT );
pinMode( led2, OUTPUT );
pinMode( led3, OUTPUT );
pinMode( led4, OUTPUT );
}
// Initialize our four classes for our four LEDs
LED a( led1 ),
b( led2 ),
c( led3 ),
d( led4 );
void loop(){
a.ledChange();
b.ledChange();
c.ledChange();
d.ledChange();
delay( 60 );
}
And the header file with the class:
// Create our custom LED class
class LED{
private:
// Set higest and lowest brightnesses for LEDs
byte highest = 250,
lowest = 5;
byte pin, brightness, isNeg; // Random necessary variables
int change; // Set the inc/decrement rate for the LED
int checkNeg(); // See if our change rate is negative, or not
void changeChange(); // Get a new change rate
public:
LED( byte led ); // Define our constructor
void ledChange ();
};
// Define our constructor
LED::LED( byte led ){
pin = led;
changeChange();
// Set the initial LED brightness by random
brightness = random( lowest, highest );
// Randomly determine if the LED will start brightening or darkening
if( random( 1, 2 ) == 1 )
change *= -1;
}
// Get a new change rate
void LED::changeChange(){
change = random( 1, 4 );
if( isNeg )
change *= -1;
}
int LED::checkNeg(){
if( change < 0 )
return 1; // If `change` is negative, return true
return 0;
}
void LED::ledChange(){
// Flip LED change value between brightening and darkening
if( brightness <= lowest || brightness >= highest ){
change *= -1;
isNeg = checkNeg(); // Determine if we're brightening/dimming the LEDs
// A 1-in-25 chance to change the fade-rate
if( random( 25 ) == 13 )
changeChange();
}
brightness += change; // Adjust the LEDs brightness
analogWrite( pin, brightness ); // Brighten/darken the LED
}
analogWrite()
function for PWM.