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

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

the code being this

``````#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.

[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];
}
}
}

Hi,

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..    1 Like

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

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

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.

Hi,
try this 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() {
//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

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

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

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

Hi,
try this new 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() {
//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];
}
}
}
``````
1 Like

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

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

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

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

Hi,

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

RV mineirin

My sketch millis_soft_pulsating_led.ino might be easier to understand.

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.

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.

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