# Led breathing, help needed sinewave SOLVED (post 14)

**URL:** <https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890>\
**Category:** LEDs and Multiplexing\
**Created:** [May 17, 2021, 2:12pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890 "2021-05-17T14:12:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 2:12pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/1 "2021-05-17T14:12:27Z")

</div>

hi guys,

so i was looking around for an effect and found almost exactly what i needed, i was able to utilise the code from this topic 3 years back

> [@Breathing leds with random fading times](https://forum.arduino.cc/t/breathing-leds-with-random-fading-times/498800/8):
>
> Probably a good idea to find some numbers to get your fade all the way from 0-255. I found ((exp(sin(remainingIntervals / fadingInterval \* PI)) - 0.36787944) \* 148.2)-93 to work.

the code being this

```arduino
#include <math.h> // mathematical functions
#define ledNumber 4 // number of leds used
int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins
float fadingInterval[ledNumber]; // fading time variable
int remainingIntervals[ledNumber];

void setup() {
  randomSeed(analogRead(0));
  for (int i = 0; i < ledNumber; i++) {
    fadingInterval[i] = random(1000, 4000); // assign random fading time
    remainingIntervals[i] = fadingInterval[i];
  }
}

void loop() {
  for ( int i = 0; i < ledNumber; i++) {
    uint8_t b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.36787944) * 108.0;
    analogWrite(ledPins[i], b);
    if (!--remainingIntervals[i]) {
      fadingInterval[i] = random(1000, 4000);
      remainingIntervals[i] = fadingInterval[i];
    }
  }
}

```

now i managed to expand it easily enough to encompass a fourth led. however upon further reading it begins to talk about sine waves and so on to bring the leds down to full off.

i dont understand this and i was kind of hoping someone might be able to help me out in the sense im asking if someone can make the required code changes to add in the sine wave or other method mentioned but not take them down completely off, maybe a value of 20 out of 255 so they are just heavily dimmed?

i know thats alot to ask, but hey, if you dont ask, you get right? or if someone can explain (in stupid step my step terms cause i really dont understand much) how to add this in myself that would be amazing also because then i'd learn a little.

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 2:22pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/3 "2021-05-17T14:22:10Z")

</div>

[Mod edit]  
Comment removed

anyway i got it fixed and going to request this be closed by a moderator

for anyone else, this is the final compiled and working sketch

#include \<math.h\> // mathematical functions  
#define ledNumber 4 // number of leds used  
int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins  
float fadingInterval[ledNumber]; // fading time variable  
int remainingIntervals[ledNumber];

void setup() {  
randomSeed(analogRead(0));  
for (int i = 0; i \< ledNumber; i++) {  
fadingInterval[i] = random(1000, 4000); // assign random fading time  
remainingIntervals[i] = fadingInterval[i];  
}  
}

void loop() {  
for ( int i = 0; i \< ledNumber; i++) {  
uint8\_t b = (exp(sin(remainingIntervals[i] / fadingInterval[i] \* PI)) - 0.975) \* 145.0;  
analogWrite(ledPins[i], b);  
if (!--remainingIntervals[i]) {  
fadingInterval[i] = random(1000, 3000);  
remainingIntervals[i] = fadingInterval[i];  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 17, 2021, 2:24pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/4 "2021-05-17T14:24:14Z")

</div>

Hi,

> [@secretreeve](#):
>
> anyway i got it fixed and going to request this be closed by a moderator

If you look in your forum edit window, you have a "solved" button or similar, this will flag the thread as having a solution, to make it easy for anyone with a similar problem.

Thanks.. Tom.. 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 2:28pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/6 "2021-05-17T14:28:14Z")

</div>

Hi,  
this line: (exp (sin (remainingIntervals [i] / fadingInterval [i] \* PI)) - 0.36787944) \* 108.0;  
calculates the sine, so the value is a point of a sinusoid.  
In this other line: remainingIntervals [i] = fadingInterval [i];  
The value of fadingInterval [i]; indexed by i is saved in  
remainingIntervals [i].

Do you need help to modify this sketch?  
RV mineirin

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 2:30pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/8 "2021-05-17T14:30:30Z")

</div>

hi there ruilviana, thank you for the reply, i really appreciate the explanation.

i was able to make changes to it by playing around with the figures and got it to actually do what i wanted it to do, still dont fully understand it but its working so im happy.

[Mod edit]  
Comment removed

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 2:38pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/10 "2021-05-17T14:38:47Z")

</div>

do you think there is a way to set the brightness in this code of 2 specific leds? (i,e those on pins 10 and 9) to a lower max brightness than the other two or would that be too much for this sketch?

[Mod edit]  
Comment removed.

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 3:06pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/12 "2021-05-17T15:06:18Z")

</div>

Hi,  
try this sketch:

```cpp
#include <math.h> // mathematical functions
#define ledNumber 4 // number of leds used
int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins
float fadingInterval[ledNumber]; // fading time variable
int remainingIntervals[ledNumber];
//------------------------------------------------------------------------------
void setup() {
  //Serial.begin(115200);
  randomSeed(analogRead(0));
  for (int i = 0; i < ledNumber; i++)
  {
    fadingInterval[i] = random(1000, 4000); // assign random fading time
    remainingIntervals[i] = fadingInterval[i];
  }
}
//------------------------------------------------------------------------------
void loop() {
  for ( int i = 0; i < ledNumber; i++)
  {
    uint8_t b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.975) * 145.0;
    //Serial.print("b "); Serial.println(b);
    if (i == 1 || i == 2) // if led on pin 10 or 9
      b = b + 50; // Add 50 to bright
    //Serial.print("b2 "); Serial.println(b);
    analogWrite(ledPins[i], b);

    if (!--remainingIntervals[i]) {
      fadingInterval[i] = random(1000, 3000);
      remainingIntervals[i] = fadingInterval[i];
    }
  }
}

```

RV mineirin

PS: correct line b = b + 50; // Add 50 to bright

Hi,  
Oops, if the brightness is lower, the correct line is: b = b - 50; // Add 50 to bright  
RV mineirin

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 3:10pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/13 "2021-05-17T15:10:02Z")

</div>

> [@ruilviana](#):
>
> ```arduino
> #include <math.h> // mathematical functions
> #define ledNumber 4 // number of leds used
> int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins
> float fadingInterval[ledNumber]; // fading time variable
> int remainingIntervals[ledNumber];
> //------------------------------------------------------------------------------
> void setup() {
> //Serial.begin(115200);
> randomSeed(analogRead(0));
> for (int i = 0; i < ledNumber; i++)
> {
> fadingInterval[i] = random(1000, 4000); // assign random fading time
> remainingIntervals[i] = fadingInterval[i];
> }
> }
> //------------------------------------------------------------------------------
> void loop() {
> for ( int i = 0; i < ledNumber; i++)
> {
> uint8_t b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.975) * 145.0;
> //Serial.print("b "); Serial.println(b);
> if (i == 1 || i == 2) // if led on pin 10 or 9
> b = b + 50; // Add 50 to bright
> //Serial.print("b2 "); Serial.println(b);
> analogWrite(ledPins[i], b);
> 
> if (!--remainingIntervals[i]) {
> fadingInterval[i] = random(1000, 3000);
> remainingIntervals[i] = fadingInterval[i];
> }
> }
> }
> 
> ```

thanks i'll load it up now and play with the figures and see how it reacts.

its actually for a plague doctor syringe where the fluid chamber has a semi transparent "glass" and the liquid is made to look moving/alive

however 2 of the leds are behind the housing so they appear to be dimmer and 2 are more central so im having to dim those 2 middle ones to compensate

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 3:10pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/14 "2021-05-17T15:10:54Z")

</div>

Hi,  
Oops, if the brightness is lower, the correct line is: b = b - 50; // subtrac 50 to bright  
RV mineirin

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 3:13pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/15 "2021-05-17T15:13:38Z")

</div>

ok i loaded it up, but the leds kind of "jolt" between max and min brightness with partial fades and its only the 2 with the modified brightness,

1 and 4 are fine fading in and out but 2 and 3 are jolting around

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 3:30pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/16 "2021-05-17T15:30:48Z")

</div>

Hi,  
try this new sketch.

```cpp
#include <math.h> // mathematical functions
#define ledNumber 4 // number of leds used
int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins
float fadingInterval[ledNumber]; // fading time variable
int remainingIntervals[ledNumber];
//------------------------------------------------------------------------------
void setup() {
  //Serial.begin(115200);
  randomSeed(analogRead(0));
  for (int i = 0; i < ledNumber; i++)
  {
    fadingInterval[i] = random(1000, 4000); // assign random fading time
    remainingIntervals[i] = fadingInterval[i];
   // Serial.print("remainingIntervals[i] "); Serial.println(remainingIntervals[i]);
  }
}
//------------------------------------------------------------------------------
void loop() {
  for ( int i = 0; i < ledNumber; i++)
  {
    float b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.975) * 145.0;
    //Serial.print("b "); Serial.println(b);
    if (i == 1 || i == 2) // if led on pin 10 or 9
      b = b * 0.8; // bright 0.2x less
    //Serial.print("b2 "); Serial.println(b);
    analogWrite(ledPins[i], b);

    if (!--remainingIntervals[i]) {
      fadingInterval[i] = random(1000, 3000);
      remainingIntervals[i] = fadingInterval[i];
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 3:33pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/17 "2021-05-17T15:33:48Z")

</div>

> [@ruilviana](#):
>
> ```arduino
> #include <math.h> // mathematical functions
> #define ledNumber 4 // number of leds used
> int ledPins[ledNumber] = { 11, 10, 9, 6 }; // assign the PWM pins
> float fadingInterval[ledNumber]; // fading time variable
> int remainingIntervals[ledNumber];
> //------------------------------------------------------------------------------
> void setup() {
> //Serial.begin(115200);
> randomSeed(analogRead(0));
> for (int i = 0; i < ledNumber; i++)
> {
> fadingInterval[i] = random(1000, 4000); // assign random fading time
> remainingIntervals[i] = fadingInterval[i];
> // Serial.print("remainingIntervals[i] "); Serial.println(remainingIntervals[i]);
> }
> }
> //------------------------------------------------------------------------------
> void loop() {
> for ( int i = 0; i < ledNumber; i++)
> {
> float b = (exp(sin(remainingIntervals[i] / fadingInterval[i] * PI)) - 0.975) * 145.0;
> //Serial.print("b "); Serial.println(b);
> if (i == 1 || i == 2) // if led on pin 10 or 9
> b = b * 0.8; // bright 0.2x less
> //Serial.print("b2 "); Serial.println(b);
> analogWrite(ledPins[i], b);
> 
> if (!--remainingIntervals[i]) {
> fadingInterval[i] = random(1000, 3000);
> remainingIntervals[i] = fadingInterval[i];
> }
> }
> }
> 
> ```

thanks i will give it a go now, i really appreciate your help, your an absolute star!

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 3:40pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/18 "2021-05-17T15:40:05Z")

</div>

its now working awesomely!!!!

so if i want to brighten it i would use 0.6, 0.4, 0.2

and to dim would be 1.0, 1.2, 1.4?

think its the other way around playing with the multiplier lol

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 3:42pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/19 "2021-05-17T15:42:03Z")

</div>

Hi,  
Yes, lighten to increase the value and darken to decrease the value, but the maximum is 1.0.  
RV mineirin

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 3:43pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/20 "2021-05-17T15:43:47Z")

</div>

thank you, and yes i had it backwards or poorly explained what i was asking. anyway i thank you so much, this things almost ready to go off to paint now the electronics are sorted.

all i gotta do is make a stand for it and load the code to a digispark board to go into its stand (after changing pin assignments of course

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 17, 2021, 3:45pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/21 "2021-05-17T15:45:16Z")

</div>

Hi,

You marked the topic as resolved by yourself in post # 13.  
Please correct.

RV mineirin

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [May 17, 2021, 5:38pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/22 "2021-05-17T17:38:15Z")

</div>

My sketch [millis\_soft\_pulsating\_led.ino](https://github.com/Koepel/Fun_with_millis/blob/master/millis_soft_pulsating_led.ino) might be easier to understand.

---

<div class="post-metadata">

**Author:** ![secretreeve](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/secretreeve/32/716131_2.png) [@secretreeve](https://forum.arduino.cc/u/secretreeve)\
**Post date:** [May 17, 2021, 5:40pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/23 "2021-05-17T17:40:41Z")

</div>

i did stumble onto that during my searches however the code used in the thread was easier for me to wrap my limited knowledge around. however, others may actually find your code alot easier and is good to have on here for those searching for similar.

---

<div class="post-metadata">

**Author:** ![Datman](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/datman/32/219519_2.png) [@Datman](https://forum.arduino.cc/u/Datman)\
**Post date:** [May 18, 2021, 1:20pm UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/24 "2021-05-18T13:20:14Z")

</div>

For a LED heartbeat effect, the best and simplest solution I found is a resistor, a capacitor and a transistor, driven by a high/low level output for fading up/down. Using PWM, at low levels steps are annoying.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 19, 2021, 2:14am UTC](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890/25 "2021-05-19T02:14:21Z")

</div>

Hi,  
Nice, especially if you don't have a PWM pin available and would like the fading.  
Tom.. 😀 👍 ☕ 🇦🇺

[Next page](https://forum.arduino.cc/t/led-breathing-help-needed-sinewave-solved-post-14/862890.md?page=2)
