Go Down

Topic: Combining an LDR sensor with random blinking (Read 1 time) previous topic - next topic

SJK70

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
 
  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);
   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...

Thanks!

RuggedCircuits

First question, how do you have the LDR hooked up?

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

AWOL

Code: [Select]
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?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

SJK70

RuggedCircuits - I've got the LDR connected to a resistor, into the ground and 5.v of my arduino uno board?


AWOL - Should I not have the digital write function in the code?

AWOL

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).
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up