LED fading

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.

About the only thing your code is accomplishing that matches your description is the fading of one of your LEDs.

You may just want to first go back and expand your description, making it as explicit as possible. Ideally turning it into a flowchart or state diagram if you are familiar with those. Break your description down to a series of states that will perform certain actions, and a series of events that will transition from one state to another. Google 'state diagrams'. Writing code from a state diagram is a lot easier than from a rather ambiguous three line description of what the program should do.

Thanks for the suggestion. I know the code I posted only completes that action - it's where I'm stuck. Should probably have included some pseudo code for the other sections.

Here's my attempt at a state diagram. It did help my thinking, more than writing it out like before.

Not sure if that's explicit enough...

I've been trying to figure out how to make the LEDs stay dark.

int sensorValue; // value of LDR for LED fade mapping
int ledPin11 = 11;  // LED connected to digital pin 11
int ledPin10 = 10;
int ledPin9 = 9;
int sensorPin = 0; // pin LDR is connected to
int fadeValue = 255;


void setup()  { 
  Serial.begin(9600);
 pinMode(sensorPin, INPUT);
 pinMode(ledPin11, OUTPUT);
 pinMode(ledPin10, OUTPUT);
 pinMode(ledPin9, OUTPUT); 

} 

void loop()  { 
  
  while(sensorValue <= 15) 
  {
     sensorValue = analogRead(sensorPin);
     sensorValue = map(sensorValue,0,1024,0,50);
      
     analogWrite(ledPin11, fadeValue);
     analogWrite(ledPin10, fadeValue); 
     analogWrite(ledPin9, fadeValue); 
     delay(20);
     fadeValue = 255;
     fadeValue = fadeValue-5;
     if(fadeValue = 0){ //If the fade reaches 0 (dark),
       analogWrite(ledPin11, 0); //Then stay dark?
       analogWrite(ledPin10, 0); //This doesn't work at all, the LEDs just switch off when 
       analogWrite(ledPin9, 0); //the LDR reads dark, even though fadeValue starts as 255?
     }
   }
       
  analogWrite(ledPin11, 255);
  analogWrite(ledPin10, 255);
  analogWrite(ledPin9, 255);  
  
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue,0,1024,0,50);//Map for LED.
       
}

I just don't know how to get them to only fade once and stay dark. Feeling pretty stupid right now - whatever I try doesn't seem to work. I was thinking a loop counter, but not sure how it could fit within the loop structure.

Put some flags in your code, and check their state - once an LED goes dark, set its flag.
Next pass thru if the flag is set then don't do anymore with that LED.

This part here makes them go full on:

analogWrite(ledPin11, 255);
analogWrite(ledPin10, 255);
analogWrite(ledPin9, 255);

check the flag for each LED, if set to dark then don't do this write.

Are the LEDs to come back on at a later time?

This code is simple and will fade the led's in order when your sensor falls below 16, and keep them off until the sensor rises above 15.

int fadeValuePin11 = 255;
int fadeValuePin9 = 255;

void loop()  { 

  while(sensorValue <= 15) 
  { 
     analogWrite(ledPin11, fadeValuePin11);
     analogWrite(ledPin9, fadeValuePin9);
     delay(20);
     if (fadeValuePin11 != 0){
     fadeValuePin11 = fadeValuePin11-5;
     } else {
     if (fadeValuePin9 != 0){
     fadeValuePin9 = fadeValuePin9-5;
     }
     }
     
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue,0,1024,0,50); //Map for LED.
}
  fadeValuePin11 = 255;
  fadeValuePin9 = 255;
  analogWrite(ledPin11, 255);
  analogWrite(ledPin9, 255);
  sensorValue = analogRead(sensorPin);
  sensorValue = map(sensorValue,0,1024,0,50); //Map for LED.
}

if (fadeValue != 0){fadeValue = fadeValue-5;}

will keep them dark in your code until the loop breaks

while(sensorValue <= 15) 
  {
     sensorValue = analogRead(sensorPin);
     sensorValue = map(sensorValue,0,1024,0,50);
      
     analogWrite(ledPin11, fadeValue);
     analogWrite(ledPin10, fadeValue); 
     analogWrite(ledPin9, fadeValue); 
     delay(20);
     fadeValue = 255;   /////////!! Delete this, you are setting fadeValue back to 255 every loop?? Then subtracting 5 in the next line??
                            /////////!!!! you will need to put it in the main loop so it resets when the loop breaks
     fadeValue = fadeValue-5;  //////////////!!!!! This is the line you want to add the if statement to.
     if(fadeValue = 0){ //If the fade reaches 0 (dark),                                       //////////!!!!!  Delete
       analogWrite(ledPin11, 0); //Then stay dark?                                            //////////!!!!!  Delete
       analogWrite(ledPin10, 0); //This doesn't work at all, the LEDs just switch off when    //////////!!!!!  Delete
       analogWrite(ledPin9, 0); //the LDR reads dark, even though fadeValue starts as 255? //////////!!!!!  Delete
     }                                                                            //////////!!!!!  Delete
   }

Adding the if statement makes it so once the fadeValue hits 0, it does not get changed until the loop break

My only guess, why it helps, is that if you -5 when the fadeValue is 0, it starts over at 255.

CrossRoads:
Are the LEDs to come back on at a later time?

Yes. As the state diagram shows, if the sensor is no longer disturbed, the LEDs should come back on.

Thank you both of you for your help. I never thought about using ! in the if structures. I should go back and read my Processing textbook again to refresh my memory. Thanks also for taking the time to explain the issue, I understand exactly what I was doing wrong now.

I merged the code, uploaded it and it works perfectly, solving both problems I had.

Thanks again!

One "Problem" with Arduino is it's Too Darn Easy to just start coding, and then get lost.

I am always harping on "Stepwise Refinement" , but I don't make it clear that the first step needs to be some clear design and diagramming. The State Machine drawing above is a good example of how this helps keep the brain seeing the pattern of the problem.

Makes me realize I've been hacking code way too much...

Nice community help on this one, guys!

int ledPin11 = 11;  // LED connected to digital pin 11

Catching up after getting back from vacation, and I spotted this in your code.

What happens if you need to change the pin that the LED is connected to? You probably think you will never need to, but suppose you add an ethernet shield that is hardwired to using pin 11.

So, you just assign a different value to ledPin11, right?

int ledPin11 = 8;  // LED connected to digital pin 8

Now, what looked like a clever name looks kind of stupid, doesn't it? Using the value in the name is not generally a bright idea, and now is the time to get out of that habit.