SJK70

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.

// 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 LEDlong 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.

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

SJK70

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

int Val = 0; // variable used to store the value coming from the sensorint ledState = LOW; // ledState used to set the LEDlong 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 }

int Val = 0; // variable used to store the value coming from the sensorint ledState = LOW; // ledState used to set the LEDlong 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 }