Pages: [1]   Go Down
Author Topic: Combining an LDR sensor with random blinking  (Read 1136 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

Hi Guys,

The bigger picture - I want a few LED's to randomly blink on/off as a general state (without using delay), untill someone gets close to them. (SEE CODE BELOW)
Then set a limit on an LDR sensor, so that when people approach the light, the LED's stop flickering, and stay on - dimmed, gradually getting brighter, the closer (less light) someone gets.

I've got the stages in separate codes, but I'm having trouble combining them together.

 This is the blink randomly without delay code:
Code:
//* Blink without Delay
 
// Turns on and off a light emitting diode(LED) connected to a digital 
// pin, without using the delay() function.  This means that other code
// can run at the same time without being interrupted by the LED code.
 
//The circuit:
// * LED attached from pin 13 to ground.
// * Note: on most Arduinos, there is already an LED on the board
// that's attached to pin 13, so no hardware is needed for this example.
 

// http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

// constants won't change. Used here to
// set pin numbers:
#define LED =  9;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated



// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.

long randNumber;

void setup() {
  // set the digital pin as output:
  pinMode(9, OUTPUT);   
randomSeed(analogRead(2));

}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  randNumber = random(0, 1000000);
  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(9, ledState);
  }
}

And this is the LDR sensor code I've been using

Code:
//Example 06B: Set the brightness of the LED at a rate specified by the value of the analogue input


#define LED 9       // pin that the LED is attached to


int val = 0;   // variable used to store the value coming from the sensor


void setup() {
 
  pinMode(LED, OUTPUT);   //LED is as an OUTPUT
 
                     // Note: Analogue pins are automatically set as inputs
}

void loop() {
 
  val = analogRead(0);   // read the value from the sensor
 
  analogWrite(LED, val/4);     // turn the LED on at the brightness set by the sensor
 
  delay(10);             // stop the program for some time
 
}

Do you know if this is possible? If so, any tips or tricks would be greatly appreciated? smiley

Cheers

Logged

UK
Offline Offline
God Member
*****
Karma: 13
Posts: 903
Twitter: @simonmonk2
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can try doing something like looking for the LDR signal to suddenly reduce by a lot.

So, keep the old reading and each time round the loop, you see how much the reading has changed.

Or, you could consider using a PIR sensor like this:

http://www.sparkfun.com/products/8630
Logged

--
My New Arduino Book: http://www.arduinobook.com

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

thanks, it's going to be used in a live installation, so a motion detector wouldn't be suitable...

I've managed to use the if and else functions to put a threshold on the ldr sensor, so it randomly blinks, then when a set low amount of light is recieved, the lights stays on, however it doesn't fade in like it used to..

any ideas?

Code:
// Randomly blink - 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
                        // Analogue pins are autmatically set as inputs
  randomSeed(analogRead(2));

}

void loop() {
     
 
 if (Val = analogRead(0) < 800)    // read the value from the sensor
 {
 analogWrite(LED1, 255-Val/4);  // turn LED on at brightness set by sensor value
  analogWrite(LED2, 255-Val/4);  // turn LED on at brightness set by sensor value
 delay(10);    // stop the program for some time
 }
 
 else if (Val = analogRead(0) > 800)
{
  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);
  }
  }
 
}
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This construct is your problem:
Code:
if (Val = analogRead(0) < 800)

It should have more brackets:
Code:
if ((Val = analogRead(0)) < 800)

This is because the assignment operator has low precedence, which means that your original version is equivalent to this:
Code:
if (Val = (analogRead(0) < 800))

Which means that you're assigning the value of a boolean to val, which is clearly not what you intend.
Logged

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Another "answer" would be to use an ultrasonic proximity detector...

http://sheepdogguides.com/arduino/aht1a.htm
Logged

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

The extra brackets seem to have worked. It does now fade in - but only very slightly...
Is there a way to start the LED's much dimmer, for a more overt fade in rather than subtle??

This is the code as it stands..

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
                        // Analogue pins are autmatically set as inputs
  randomSeed(analogRead(2));

}

void loop() {
     
 
 if ((Val = analogRead(0)) < 500)    // read the value from the sensor
 {
 analogWrite(LED1, 255-Val/4);  // turn LED on at brightness set by sensor value
  analogWrite(LED2, 255-Val/4);  // turn LED on at brightness set by sensor value
 delay(10);    // stop the program for some time
 }
 
 else if ((Val = analogRead(0)) > 500)
{
  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);
  }
  }
 
}

cheers for the help!
Logged

Pages: [1]   Go Up
Jump to: