Basically I'm trying to get a series of LEDs to fade out in succession according to input from a sensor. I'm starting out with just two LEDs, which I eventually want to evolve into a larger scale project. Unfortunately I seem to have really gotten a block with the coding. I'm pretty new to Arduino to boot.
The program should run like this:
If the LDR sensor is disturbed, fade the first LED out, and keep it dark.
If/while the first LED is dark, wait for an amount of time, then fade the second LED out, and keep it dark.
Otherwise, If the sensor is NOT triggered, keep all LEDs lit.
Here's the current iteration:
int sensorValue; // using an LDR for the sensor - value for fade mapping
int ledPin11 = 11; // LED connected to digital pin 11
int ledPin9 = 9; //LED 2 connected to pin 9
int sensorPin = 0; // pin LDR is connected to
int fadeValue = 0;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin9, OUTPUT);
}
void loop() {
while((sensorValue <= 15)&&(fadeValue <= 255))
{
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue,0,1024,0,50);
analogWrite(ledPin11, fadeValue);
delay(20);
fadeValue = fadeValue-5;
}
fadeValue = 255;
analogWrite(ledPin11, 255);
analogWrite(ledPin9, 255);
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue,0,1024,0,50);//Map for LED.
}
In this iteration, the LEDs will stay lit until the LDR value reads as dark. The LED on pin 11 fades out, but blinks back to life and fades out repeatedly since it's stuck in that fading loop. I'm not sure how to make it stay dark - this is my first problem.
Once I can get the first LED to stay dark, I need to get the second LED to wait, then also fade out after the first. It should only fade if the LED before it is dark. This is my second issue.
Any assistance would be appreciated... please forgive my n00b status.