Led flow control

Dear all,

hope my question fits into this forum section, in case not please give me an advise for the right section.

I searched in the forum but I haven’t found what I’m looking for.

Can someone help me with a code to do the following thing:

4 Led’s on output pins 5,6,9,10

The should work along this sequence. Start with fade in Led 1 within a specific time(for example 10 seconds), than Led1 is a specific time on (for example 60 seconds) within a specific brightness. After the on period Led1 is fade out within the same timeframe as the fade in. If 50%(5s) of the fade out of Led1 is reached a fade in of Led2 should start. So there is a crossover between fade out of Led1 and fade in of Led2. Then Led2 is 60 seconds on and fades out and Led3 start with fade in with the same crossover as Led1/Led2. This sequence goes from Led1 to Led2 to Led3 to Led4 and back to Led 3 to Led2 and start again with Led1.

All values of time of fade in/ fade out, on duration, brightness and crossing point are valid for all 4 Led’s, but adjustable with 4 potentiometer. In case my description isn’t understandable, I can draw a flowchart as well.

The difficult job for me is the fade in/fade out section with the adjustable time and crossing point, because I can’t use the delay function.

I appreciate your feedback and as well an example code.

Angela

Sorry this is not a code writing forum.

Sure we will help you with code but if you want some one to write it for you then post in the "Products and Services" section and say what sort of fee you are willing to pay.

If you want help with your code then post what you have written and ask about why it is not doing what you want it to do and people will be willing to help you get it going.

The difficult job for me is the fade in/fade out section with the adjustable time and crossing point, because I can’t use the delay function.

No, you can't. The blink without delay example shows how to do things at specific times without using delay. The only challenge in your application is knowing what state each pin is in, and when to change to the next state. Not terribly difficult, if you sketch the logic of the state changes for each pin independently, and code them that way, too.

My apologies that I'm use the forum in the wrong way.

Here is my current code:

int dimvalue = 0;                            

int ledpin1 = 5;                            
int ledpin2 = 6;
int ledpin3 = 9;
int ledpin4 = 10;

int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;

int time_dim_read;
int on_duration_read;
int off_duration_read;
int time_dim;
int on_duration;
int off_duration;






void setup() 
{
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);  
pinMode(ledpin3, OUTPUT);  
pinMode(ledpin4, OUTPUT);  
Serial.begin(115200);
} 
 
void loop() 
{
time_dim_read = analogRead(analogPin0);
on_duration_read = analogRead(analogPin1);
off_duration_read = analogRead(analogPin2);
 
time_dim = map(time_dim_read, 0, 1023 , 0,100);
on_duration = map(on_duration_read, 0, 1023, 0, 10000);
off_duration =map(off_duration_read, 0, 1023, 0, 10000);

Serial.print("fade in/out duration in ms:  ");
Serial.println(time_dim);
Serial.print("on duration in ms:           ");
Serial.println(on_duration);
Serial.print("off duration in ms:          ");
Serial.println(off_duration);

function1();
function2();
function3();
function2();

} 

int function1(){ 
 for(dimvalue = 0 ; dimvalue <= 255; dimvalue+=5) // fade in (from min to max) 
  { 
    analogWrite(ledpin1, dimvalue);           // sets the value (range from 0 to 255) 
    delay(time_dim);                            // waits for 30 milli seconds to see the dimming effect 
  }
  analogWrite(ledpin1, 255);
delay (on_duration);

 for(dimvalue = 255; dimvalue >=0; dimvalue-=5)   // fade out (from max to min) 
{ 
    analogWrite(ledpin1, dimvalue); 
    delay(time_dim); 
  }  
  
analogWrite(ledpin1, 0);
delay (off_duration); 
}

int function2(){ 
 for(dimvalue = 0 ; dimvalue <= 255; dimvalue+=5) // fade in (from min to max) 
  { 
    analogWrite(ledpin2, dimvalue);           // sets the value (range from 0 to 255) 
    delay(time_dim);                            // waits for 30 milli seconds to see the dimming effect 
  }
  analogWrite(ledpin2, 255);
delay (on_duration);

 for(dimvalue = 255; dimvalue >=0; dimvalue-=5)   // fade out (from max to min) 
{ 
    analogWrite(ledpin2, dimvalue); 
    delay(time_dim); 
  }  
  
analogWrite(ledpin2, 0);
delay (off_duration); 
}

int function3(){ 
 for(dimvalue = 0 ; dimvalue <= 255; dimvalue+=5) // fade in (from min to max) 
  { 
    analogWrite(ledpin3, dimvalue);           // sets the value (range from 0 to 255) 
    delay(time_dim);                            // waits for 30 milli seconds to see the dimming effect 
  }
  analogWrite(ledpin3, 255);
delay (on_duration);

 for(dimvalue = 255; dimvalue >=0; dimvalue-=5)   // fade out (from max to min) 
{ 
    analogWrite(ledpin3, dimvalue); 
    delay(time_dim); 
  }  
  
analogWrite(ledpin3, 0);
delay (off_duration); 
}

Your not making a good start are you.

Why not do the sensible thing and read the "how to use this forum" sticky post and also the one that says "read this before you post in this section.
Then go back and modify that post (do not post again) to include code tags so we can read it.

If not then the next thing I will do is sing at you, and you wouldn't like that. :slight_smile:

Hope thats good right now.

Sorry but I'm a girl :frowning:

Angela_1983:
Sorry but I'm a girl :frowning:

We will have no talk like that round here. :slight_smile: you are just as capable as anyone else.

Now what you have to do is to remove all those delays and implement you program as a state machine.
In this way you can make it so it looks like several things are happening at the same time.
have you seen the blink without delay example code?
Try and google for finite state machine.
Basically it all runs on the millis time function. Start off by just having one fade, a sort of fade without delay. On each time out you increase what was your loop counter and write out a new value, then set the values for the next time out.
Get that going first and it will be a lot easier to add more functions.

Try and google for finite state machine.

Mike's being modest: read his FSM write-up here.

This video is quite good too. Doesn't do a fade, but it explains how to handle two LEDs.

Hi,

the "blink without delay" portion is understood, but it still the question how can I use the function to combine a fade on sequence, a on sequence, a fade out sequence and an off sequence. I see there is a dependency between the different sequences and conditions.

Cheers

I see there is a dependency between the different sequences and conditions.

A dependency on what? The current time? Well, what else would you use? There should not be a dependency on anything else.

How would YOU perform the action you want the Arduino to perform? You've got a pencil, a pad of paper, 4 switches with LEDs connected, and a watch. That's all the Arduino has, too.

Hmm, is there at least possibility to show me, how I can use Blink without delay in an asynchronous mode? LED 1 second on, 5 seconds off?

Angela_1983:
Hmm, is there at least possibility to show me, how I can use Blink without delay in an asynchronous mode? LED 1 second on, 5 seconds off?

Uneven intervals and asynchronous mode are not the same thing. Which is it you want?

For uneven intervals:

unsigned long lastTime;
unsigned long interval = 1000;
byte ledState;
byte ledPin = 4; // Use your pin # here

void loop()
{
   unsigned long currTime = millis();
   if(currTime - prevTime >= interval)
   {
      ledState = !ledState; // toggle the state
      if(interval == 1000)
         interval = 5000;
      else
         interval = 1000;
       prevTime = currTime;

       digitalWrite(ledPin, ledState);
   }
}

Hi,

got you!

Uneven intervals seems the right one.

Can you give me another advise? This was blinking with one LED and a uneven interval. If I would add a second LED with the same uneven interval as LED1 but start this interval 2 seconds later. Like an offset of the start time.

It would be a great thing!

Cheers

Can you give me another advise?

No. It's time to release the parking break, let out the clutch, and engage YOUR brain.

If you can't figure out how to write the code, at least list the steps YOU would follow to make two lights go on and off at uneven intervals starting the second one some time after the first one.

Here's a hint. Toggling the second one is EXACTLY like toggling the first one, and completely independently. Copy/paste/change the names...

Here's another hint. Starting the second light toggling at the same uneven interval some known time later means that you need to know when the first one started, and you need to determine if it's time to start the second one going. Hmmm, sounds exactly like the process for determining if it's time to toggle a light.

Final hint. There are three completely independent things to make happen. So, create some appropriately named functions, and call them as needed.

Angela_1983:
Can you give me another advise?

JimboZA:
Mike's being modest: read his FSM write-up here.
This video is quite good too. Doesn't do a fade, but it explains how to handle two LEDs.

Did you look at either of those.....

Yes I looked into both Videos, and hammer this into my head. Is fully unterstood except the question regarding the "start offset". All examples start always at 0 and not with a delay.

You do not use delay at all, that is the point.

Read what PaulS says. The time to begin the second LED is tied to the time to start the first. Just like you use two differant intervals to cause the uneven blinking you use another interval to kick off the second LED.

Hi,

Thank you for the instruction. Now I have 5 “Timers” or whatever you can call these. Three for the dedicated LED’S and 2 as an offset for the start points of LED2 and LED3.
Now my pattern looks like 1,2,3,1,2,3,1,2,3. What can be the proposal if I would change the pattern to 1,2,3,2,1,2,3,2,1…. A For loop, switch case or array.

Is the code the right way or had I create too much waste around? Hope I follow the way that you told me.

unsigned long prevTime_LED1;
unsigned long interval_LED1;
int state_LED1=LOW;
unsigned long currTime_LED1;

unsigned long prevTime_LED2;
unsigned long interval_LED2;
int state_LED2=LOW;
unsigned long currTime_LED2;

unsigned long prevTime_LED3;
unsigned long interval_LED3;
int state_LED3=LOW;
unsigned long currTime_LED3;



unsigned long prevTime_delay2;
unsigned long interval_delay2 = 4000;
int state_delay2;
unsigned long currTime_delay2;

unsigned long prevTime_delay3;
unsigned long interval_delay3 = 8000;
int state_delay3;
unsigned long currTime_delay3;

int ledPin1 = 9; // Use your pin # here
int ledPin2 = 10; // Use your pin # here
int ledPin3 = 11; // Use your pin # here



void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

Serial.begin(9600);
}



void loop()
{

//-----------------------------------------LED#1-------------------------------------------------------  
  
  currTime_LED1 = millis();
   if(currTime_LED1 - prevTime_LED1 >= interval_LED1)
   {
 
      if (state_LED1 == LOW)
      {state_LED1 = HIGH;
      interval_LED1=5000;
      }
      else
      {state_LED1 = LOW;
      interval_LED1=7000;
      }
       prevTime_LED1 = currTime_LED1;

       digitalWrite(ledPin1,state_LED1);
     }
//-----------------------------------------LED#2-------------------------------------------------------  
  
currTime_LED2 = millis();
   if(currTime_LED2 - prevTime_LED2 >= interval_LED2&&state_delay2==1)
   {
 
      if (state_LED2 == LOW)
      {state_LED2 = HIGH;
      interval_LED2=5000;
      }
      else
      {state_LED2 = LOW;
      interval_LED2=7000;
      }
       prevTime_LED2 = currTime_LED2;

       digitalWrite(ledPin2,state_LED2);
     }
//-----------------------------------------LED#3-------------------------------------------------------  
  
 currTime_LED3 = millis();
   if(currTime_LED3 - prevTime_LED3 >= interval_LED3&&state_delay3==1)
   {
 
      if (state_LED3 == LOW)
      {state_LED3 = HIGH;
      interval_LED3=5000;
      }
      else
      {state_LED3 = LOW;
      interval_LED3=7000;
      }
       prevTime_LED3 = currTime_LED3;

       digitalWrite(ledPin3,state_LED3);
     }
//-----------------------------------------Start_OffsetLED2-------------------------------------------------------  
currTime_delay2 = millis();
   if(currTime_delay2 - prevTime_delay2 >= interval_delay2)
   {
state_delay2=1;
   }



//-----------------------------------------Start_OffsetLED3-------------------------------------------------------    
 currTime_delay3 = millis();
   if(currTime_delay3 - prevTime_delay3 >= interval_delay3)
   {
state_delay3=1;
   }

}

Is the code the right way or had I create too much waste around?

How many variables do you need to hold now?

currTime_delay2 = millis();
   if(currTime_delay2 - prevTime_delay2 >= interval_delay2)
   {
state_delay2=1;
   }

And, if
not? What
should
happen?

The Tools menu has an Auto Format item for a reason. Learn what that reason is.

PaulS:

Is the code the right way or had I create too much waste around?

How many variables do you need to hold now?

2 right now. One for the second LED and one for the third LED

My autoformatted code:

unsigned long prevTime_LED1;
unsigned long interval_LED1;
int state_LED1=LOW;
unsigned long currTime_LED1;

unsigned long prevTime_LED2;
unsigned long interval_LED2;
int state_LED2=LOW;
unsigned long currTime_LED2;

unsigned long prevTime_LED3;
unsigned long interval_LED3;
int state_LED3=LOW;
unsigned long currTime_LED3;



unsigned long prevTime_delay2;
unsigned long interval_delay2 = 4000;
int state_delay2;
unsigned long currTime_delay2;

unsigned long prevTime_delay3;
unsigned long interval_delay3 = 8000;
int state_delay3;
unsigned long currTime_delay3;

int ledPin1 = 9; // Use your pin # here
int ledPin2 = 10; // Use your pin # here
int ledPin3 = 11; // Use your pin # here



const int fadeAmount1 = 5;                // Stufen im Fadingraster LED1
const int fadeAmount2 = 5;                // Stufen im Fadingraster LED2
const int fadeAmount3 = 5;                // Stufen im Fadingraster LED3

const long intervall1 = 40;               // Zeitraum zwischen Fadingstufen LED1
const long interval12 = 40;               // Zeitraum zwischen Fadingstufen LED2
const long interval13 = 40;               // Zeitraum zwischen Fadingstufen LED3

unsigned int brightness1 = 0;             // aktuelle Helligkeit der LED1
unsigned int brightness2 = 0;             // aktuelle Helligkeit der LED2
unsigned int brightness3 = 0;             // aktuelle Helligkeit der LED3

unsigned long Laufzeit;                   // Timer für Ablauf
unsigned long Trigger;                    // Timer für Ablauf
unsigned long previousMillis1 = 0;        // Timer für Fading LED 1
unsigned long currentMillis1;             // Timer für Fading LED 1
unsigned long previousMillis2 = 0;        // Timer für Fading LED 2
unsigned long currentMillis2;             // Timer für Fading LED 2
unsigned long previousMillis3 = 0;        // Timer für Fading LED 3
unsigned long currentMillis3;             // Timer für Fading LED 3


void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  Serial.begin(9600);
}



void loop()
{

  //-----------------------------------------LED#1-------------------------------------------------------  

  currTime_LED1 = millis();
  if(currTime_LED1 - prevTime_LED1 >= interval_LED1)
  {

    if (state_LED1 == LOW)
    {
      state_LED1 = HIGH;
      interval_LED1=5000;
    }
    else
    {
      state_LED1 = LOW;
      interval_LED1=7000;
    }
    prevTime_LED1 = currTime_LED1;

    digitalWrite(ledPin1,state_LED1);
  }
  //-----------------------------------------LED#2-------------------------------------------------------  

  currTime_LED2 = millis();
  if(currTime_LED2 - prevTime_LED2 >= interval_LED2&&state_delay2==1)
  {

    if (state_LED2 == LOW)
    {
      state_LED2 = HIGH;
      interval_LED2=5000;
    }
    else
    {
      state_LED2 = LOW;
      interval_LED2=7000;
    }
    prevTime_LED2 = currTime_LED2;

    digitalWrite(ledPin2,state_LED2);
  }
  //-----------------------------------------LED#3-------------------------------------------------------  

  currTime_LED3 = millis();
  if(currTime_LED3 - prevTime_LED3 >= interval_LED3&&state_delay3==1)
  {

    if (state_LED3 == LOW)
    {
      state_LED3 = HIGH;
      interval_LED3=5000;
    }
    else
    {
      state_LED3 = LOW;
      interval_LED3=7000;
    }
    prevTime_LED3 = currTime_LED3;

    digitalWrite(ledPin3,state_LED3);
  }
  //-----------------------------------------Start_OffsetLED2-------------------------------------------------------  
  currTime_delay2 = millis();
  if(currTime_delay2 - prevTime_delay2 >= interval_delay2)
  {
    state_delay2=1;
  }



  //-----------------------------------------Start_OffsetLED3-------------------------------------------------------    
  currTime_delay3 = millis();
  if(currTime_delay3 - prevTime_delay3 >= interval_delay3)
  {
    state_delay3=1;
  }

}