Hey guys,
I'm working on a code to randomly blink without (delay), and uses if and else functions to incorporate a sensor too..
I just want to expand the code to include more than 1 LED, however at the moment with 2 LED's they just blink randomly together as a pair.
Is there a way so they individually blink randomly?
here's the code:
// LED gets brighter the less light the LDR recieves
#define LED1 9 // pin that LED is attached to
#define LED2 10
int Val = 0; // variable used to store the value coming from the sensor
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long randNumber;
void setup() {
pinMode(LED1, OUTPUT); // LED is an OUTPUT
pinMode(LED2, OUTPUT); // LED is an OUTPUT
randomSeed(analogRead(2));
// Analogue pins are autmatically set as inputs
}
void loop() {
if ((Val = analogRead(0)) < 600) // read the value from the sensor
{
analogWrite(LED1, 255-Val); // turn LED on at brightness set by sensor value
analogWrite(LED2, 255-Val); // turn LED on at brightness set by sensor value
delay(10); // stop the program for some time
}
else if ((Val = analogRead(0)) > 600)
{
randNumber = random(0, 150000);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > randNumber) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(LED1, ledState);
digitalWrite(LED2, ledState);
}
}
}