Offset my pulsing LED's

Hello all! :slight_smile:

I am hoping on some guidance, I have an attiny85 that I plan to use in some resident evil vials I've made.

I have 4 LED's I want to have pulsing, and after finding a library on github (Breathe.h) I've found the right pulsing for me!

Here is all the code that i necessary;

#include <Breathe.h>
Breathe Breathe;




void setup(){ 
       
}

void loop(){
  Breathe.set(0, HIGH, 1, 20);
  Breathe.set(1, HIGH, 1, 20);
}

But I need to offset it.. Now they pulse the same, but I need it to be roughly opposite, that when 2 is low, the other 2 is on it's brightest...

I did try to put a delay(3000); between, this did not work.. Any other way I could try? :slight_smile:

Which library on GitHub? A link please.

I used this

As said, it is not important to have them be exactly offset from another, but just as they dont pulse at the same time..

Breathe.set(10, HIGH, 1, 20)

Third parameter controls speed of effect, so you’re going to have to time it and mess around.

Delay() stops code executing...so does it interrupt the breathing pattern too? If you set a long delay does the led “hold its breath”? If so use millis()...set a “start” variable when you trigger the first, then put in an if statement to check for;

millis()-start >= (value you need here)

And start the second one...you could even use a random value up between (for example) a quarter and three quarters of the “breath” length.

It's coming along here, it is not perfect, but I have made it this far ;

#include <Breathe.h>
Breathe Breathe;

void setup(){ 
       
}

void loop(){

long currentMillis = millis();

  if (currentMillis < 3000){
  Breathe.set(0, HIGH, 1, 20);
  }
  if (currentMillis > 3000){  
  Breathe.set(1, HIGH, 1, 20);
  }
  }

This offcourse do not work as the millis(); just climbs as I belive it's the time the arduino has been on. So what happens here is that the led connected to 1 works fine after 3sec, but the first just stays lit.

Is there an way to restart the timer back to zero?

Yes, as I said, you need to assign millis to a variable when you trigger the breathe routine...

But you have BOTH LEDS dependant on an “if” statement...do you need that? I thought you were just starting one and offsetting the second;

#include <Breathe.h>
Breathe Breathe;

void setup(){ 
       
}

void loop(){

long currentMillis = millis();

  Breathe.set(0, HIGH, 1, 20);
  currentMillis = millis();           // start the timer when you start the breathe, not when you start the sketch!
  
if (currentMillis > 3000){      // then this time is “since led 0 started breathing”
  Breathe.set(1, HIGH, 1, 20);
// currentMillis=millis();          if necessary, set again here then It’s “since led 1 started breathing”
  }
  }

That worked great! Thank you!

I guess I didnt need that, but that was the best way I could think of. I was trying to reset the timer for evry round of the loop.

You still could, but you might need a second variable so you have a start for each timer.

GreyArea:
You still could, but you might need a second variable so you have a start for each timer.

Agree!

What do you mean by that? The code works great with what you wrote.. Do you have an example so I can learn for future projects on why a second variable will be a good idea?

Well, if you have two timers, you need to delay one relevant to the other. I guess with just two LEDs you could reset the same variable each time a timer starts...but what if you build to three,or four...or ten...and you still want it that none of the breathing LEDs are synchronised?

You’re going to need a start time for each one...so probably easier to start using the same concept now, rather than reprogram later...