What I cannot figure out is how to make the two blinking LED's blink randomly in the code instead of at a specified time. I can blink one LED random but I'm not sure how to do it without using code with a random delay time, which would block the other things going on.
As always thanks for any help.
// LED on pins 8 and 13 blink, LED on 9 fade in and out
const byte greenLED = 8;
const byte redLED = 13;
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int analogInPin = A0;
int sensorValue = 0;
// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 200;
const unsigned long redLEDinterval = 500;
// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;
void setup ()
{
pinMode (greenLED, OUTPUT);
pinMode (redLED, OUTPUT);
greenLEDtimer = millis ();
redLEDtimer = millis ();
pinMode(led, OUTPUT);
} // end of setup
void toggleGreenLED ()
{
if (digitalRead (greenLED) == LOW)
digitalWrite (greenLED, HIGH);
else
digitalWrite (greenLED, LOW);
// remember when we toggled it
greenLEDtimer = millis ();
} // end of toggleGreenLED
void toggleRedLED ()
{
if (digitalRead (redLED) == LOW)
digitalWrite (redLED, HIGH);
else
digitalWrite (redLED, LOW);
// remember when we toggled it
redLEDtimer = millis ();
} // end of toggleRedLED
void loop ()
{
// Handling the blink of one LED.
if ( (millis () - greenLEDtimer) >= greenLEDinterval)
toggleGreenLED ();
// The other LED is controlled the same way. Repeat for more LEDs
if ( (millis () - redLEDtimer) >= redLEDinterval)
toggleRedLED ();
analogWrite(led, brightness);
int sensorValue = analogRead(A0);
sensorValue = map(sensorValue, 0, 1023, 0, 30);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(sensorValue);
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop/code]
This is from the project I’m working on. It uses shift registers and shifter library. Basically everything is in arrays. The code is still very much in development so it could use a bit of cleaning up and optimization, but it works.
void neonFlicker(int firstPin, int lastPin) // Receives first and last pin number for the block to be assigned neonFlicker effect
{
for (int i = firstPin - 1; i <= lastPin -1 ; i++) // Iterates between the pins set in loop, sets addresses of pins to 1-8 per shift register, and not 0-7
{
currentMillis = millis (); // Updates current time
if (ledState [i] == 0 &&
currentMillis - previousMillisArray [i] > neonFlickerArray [i] && // Checks if it is time to flicker
flickerDelay [i] <= currentMillis) // Checks if it is time to turn on one of the outputs on the shift register.
{
shifter.setPin(i, HIGH); // If it is time for the pin in question, it is turned on.
previousMillisArray [i] = currentMillis; // Updates corresponding field in the time array.
ledState [i] = 1; // Changes the state ledState
neonFlickerArray [i] = (random (2, 200)); // Sets new flickering delay (time the light is turned on)
}
if (ledState [i] == 1 && currentMillis - previousMillisArray [i] > neonFlickerArray [i]) // Checks if it is time to turn off one of the outputs on the shift register.
{
shifter.setPin(i, LOW); // If it is time for the pin in question, it is turned off.
previousMillisArray [i] = currentMillis; // Updates corresponding field in the time array.
ledState [i] = 0; // Changes the state ledState
neonFlickerArray [i] = (random (2, 4000)); // Sets new flickering delay (time the light is turned off)
}
}
shifter.write();
}