Can you control 2 or more led's to do 2 differnet things at the same time.?

Alright so I'm new to this and have a newbie question. I have two led's wired up and have a for loop that makes one go from off state to 255 and back down to off again. can I make the other led do something completely different like just change brightnesses every second to a random value.?? I cant seem to figure out how you would do this. So far all the tutorials I have read are based around making one thing do something or making a bunch of things do the exact same thing. I basically want two different programs running in the main loop(). is that even possible ??


How can you expect one LED to do two different things? An LED can do one thing. It can emit light. It has no other tricks up its sleeve.

can I make the other led do something completely different like just change brightnesses every second to a random value.?


Does the extra punctuation help?

I'm guessing that the reason you are asking the question is because you are using delay() in the for loop, and nothing else happens until the for loop ends.

But, I shouldn't have to guess. You should have posted your code.

You need to read, understand, and embrace the blink without delay example.

I don’t expect one led to do two different things. There are two led’s wired up as stated.


Yes you can.
At the top of loop, capture the current time

void loop(){
currentTime = millis();

//and if its time to make one do something, do it and store the next time to check for an action:

if ( (currentTime - nextTime1) >= duration1){
nextTime1 = nextTime1 + duration1;
// do LED1 stuff

if ( (currentTime - nextTime2) >= duration2){
nextTime2 = nextTime2 + duration2;
// do LED2 stuff
} // end loop

Well that's cool, and helpful, thanks. Oh and I knew I remembered LED's being able to do more than just emit light.....

Pretty cool...

Yes, there's been a couple projects where folks used that ability to dim an LED display as a hand was waved over it.

A pattern that works well when you need to do a number of unrelated things is to create a function for each thing and use static variables in the function to hold the associated state information. This encapsulates the functionality and data without the mental overhead of an OO approach and avoids using global data for things that don't actually need to be global.

void loop()
  // etc

void blinkLed()
  static unsigned long lastTime = 0;
  const unsigned long Interval = 500; // ms

  if(millis() - lastTime >= Interval)
    lastTime += Interval;
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));

// etc

That's great info. thanks.!


Here is a library with an example to facilitate such things, but not to take away from previous comments, it is well worth understanding the techniques.

Multiblink sketch in my code repository below can do what you need as well.