Pages: [1]   Go Down
Author Topic: Combining an LDR sensor with random blinking  (Read 1131 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26622
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26622
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

--
Beat707: MIDI drum machine / sequencer / groove-box for Arduino
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

cool thanks,

what would you have instead of digitalWrite though?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26622
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what would you have instead of digitalWrite though
Personally?
Nothing.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Pages: [1]   Go Up
Jump to: