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:
//* 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
//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?
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?
// 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);
}
}
}
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..
// 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);
}
}
}