I've had a play around with your code. It looks correct and should work. I've rewritten it a little to see if that helps give it properly random behaviour.
Please note the following changes
- power mode (don't set to 1 if using 5V if your resistors are designed for 3.3v!) - sets up the pin mode for the LEDs
- random seed initialisation (making each turn on of each device different)
- took out the cos function - straight PWM should look identical for the periods you are using (put back later if desired)
- added a brightness gold of up to 1/4 second when max brightness reached (take out if not desired)
- Code: Select all
#define POWER 0 // 0 for LOW power mode, 1 for HIGH power mode
int pwmPin = 11; // light connected to digital pin 11-- I just chose an initial value
// int period = 500;
long blink = 3;
const byte pwmPins [] = {3, 5, 6, 9, 10, 11}; //5, 11, 12, 15, 16, 17
void setup()
{
// setup input or output depending on power mode
for (int i = 0; i < 6; i++)
{
if(POWER == 1)
{
pinMode(pwmPins[i], OUTPUT);
}
else
{
pinMode(pwmPins[i], INPUT);
}
}
randomSeed( analogRead(0) ); // set the seed so every turn on on every device is different!
}
void loop()
{
setup_next_firefly();
fade();
delay( random(500, 2001) );
}
void fade()
{
int value;
for(int i = 0; i < 255; i++) //fade in
{
// the next line is way too clever! based on your short period, should look the same with simpler code
// value = abs(-127+127*cos(4*PI/period*i));
analogWrite(pwmPin, i);
delay(blink);
}
delay( random(1,250) ); // hold at max brightness for up to 1/4 second
for(int i = 255; i > 0; i--) //fade out
{
// the next line is way too clever! based on your short period, should look the same with simpler code
// value = abs(-127+127*cos(4*PI/period*i));
analogWrite(pwmPin, i);
delay(blink);
}
}
void setup_next_firefly()
{
pwmPin = pwmPins [random (0, 6)];
blink = random(1, 3);
}