Go Down

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

SJK70

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: [Select]
// 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!

Go Up