I'm trying to create a code that has an LED (2 in this case), randomly blinking on/off between set values. I've got this part working amongst the code below.
But I also want to incorporate a LDR sensor into it, so that the closer a hand gets (the less light it receives) the brighter and more stable (gradually stop blinking) the light gets.
//Work in progress Code
int LED1 = 9; // LED connected to digital pin 9
int LED2 = 11;
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time
int val = 0; // variable used to store the value coming from the senor
void setup() // run once, when the sketch starts
{
randomSeed (analogRead (0)); // randomize
pinMode( LED1, OUTPUT); // sets the digital pin as output
pinMode( LED2, OUTPUT);
}
void loop() // run over and over again
{
val = analogRead(0); // read the value from the sensor
randOn = random (10, 150); // generate ON time between 0.02 and 0.15 seconds
randOff = random (250, 2000); // generate OFF time between 0.25 and 2.5 seconds
digitalWrite(LED1, HIGH); // sets the LED on
digitalWrite(LED2, HIGH);
delay(randOn); // waits for a random time while ON
digitalWrite(LED1, LOW); // sets the LED off
digitalWrite(LED2, LOW);
delay(randOff); // waits for a random time while OFF
}
Any ideas what I'm doing wrong? At the moment, it's only blinking the LED's randomly, not reacting to the sensor at all...
analogWrite(LED1, val/4);
analogWrite(LED2, val/4);
delay(10);
randOn = random (10, 150); // generate ON time between 0.02 and 0.15 seconds
randOff = random (250, 2000); // generate OFF time between 0.25 and 2.5 seconds
digitalWrite(LED1, HIGH); // sets the LED on
digitalWrite(LED2, HIGH);
So, 1/100th of a second after you wrote a PWM value, you turn it full on anyway?
Probably not.
Look at how "loop" well, loops, so things repeat.
A digitalWrite will turn the LED either fully on or fully off (which is which depends on how you've wired the LED).
I've got the LDR connected to a resistor, into the ground and 5.v of my arduino uno board?
It's still not exactly clear, but how about you write a small program just to test your LDR. Take the analogRead() readings and print them to the serial terminal.
RuggedCircuits - my LDR works, I've used it with other codes, I've got it so that it gets brighter as you get near (as the LDR receives less light), I'm just trying to combine that with the random blinking, i.e when there is a set level of light it randomly blinks. Then when this light is reduced, it will become stable and begin getting brighter...