Programming a loop with unpredictable changes in 3 LEDs state sequence

Hello

I am trying to write a code which will read data from a sensor. If the value read is higher than a certain threshold, I want the following LED-state sequence to happen:

  • Turn LED1 ON;
  • 1-second delay;
  • Dim LED1; Turn LED2 ON;
  • 1-second delay;
  • Dim LED2; Turn LED3 ON;
  • 1-second delay;
  • Dim LED3;

Now, this is very easy to code.

However, I am trying to modify this program so that I read the value of the sensor after each delay.

If the value read is higher than that certain threshold, then I want to start this sequence WHILE AT THE SAME TIME the already on-going sequence continues without interruption.

This is a formidable task, and I hope someone could guide me in the right direction.

I tried (if statements, for loops, while loops, case statements), but it seems to be infinite. My loop(s) need to have some sort of memory.

I don't see any way to simultaneously dim LED1 (Step 3) and turn on LED1 (Step 1).

Perhaps you should describe the pattern of lights you hope to see for different patterns of input.

Perhaps you want the lights to dim up (or turn on) in sequence as long as the signal is high and dim out (or turn off) in sequence when the signal goes low again?

"I don't see any way to simultaneously dim LED1 (Step 3) and turn on LED1 (Step 1)."

You are absolutely right. When I read the sensor, and I find that it holds a value higher than that certain threshold, then I will leave out dimming LED1.

"Perhaps you should describe the pattern of lights you hope to see for different patterns of input."

Besides that pattern I've initially described, let's assume the following pattern of inputs:

Sensor reads values higher than threshold at time = 0 and at time = 1 s.

[u]The following pattern should occur:[/u]

time LED1 LED2 LED3 -1 DIM DIM DIM 0 ON DIM DIM 1 ON ON DIM 2 DIM ON ON 3 DIM DIM ON 4 DIM DIM DIM

Let's assume another pattern of inputs:

Sensor reads values higher than threshold at time = 0 and at time = 2s.

[u]The following pattern should occur:[/u]

time LED1 LED2 LED3 -1 DIM DIM DIM 0 ON DIM DIM 1 DIM ON DIM 2 ON DIM ON 3 DIM ON DIM 4 DIM DIM ON 5 DIM DIM DIM

"Perhaps you want the lights to dim up (or turn on) in sequence as long as the signal is high and dim out (or turn off) in sequence when the signal goes low again?"

The original state of all LEDs should be DIM. If there is an input to the sensor, then the described sequence(s) should occur.

Seems like you want to have an array that stores the current dimmed state of each light. Then have loop that shifts the dimmed state one light to the right each second. Then when it is done with that it checks the input and if it’s high, it lights the first light.

OK I think the attached is what you need to do in theory.
It’s often best to approach tasks this way then the code becomes easier.

Your biggest problem is the use of delay(). During a delay() nothing else can happen.

A lesser problem is to think of code as being one logical line of flow. The processor does one step after the other but your leds and sensors can all be coded as separate independent pieces. Getting rid of delays and long-running loops will let such pieces run together.

Think of an RTS with flow vs a chess board and timer. You can Real Time your leds and sensors.

There is an Example sketch, Blink Without Delay, that teaches how to use time in Arduino. A complete commonsense tutorial of that (explains the what, why and how of knowing WHEN) is at the first link at the bottom of this post.

Langy: OK I think the attached is what you need to do in theory. It's often best to approach tasks this way then the code becomes easier.

Super smart.. super simple

Thank you for helping. This is exactly what I needed.