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.
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];
}
}
}
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.
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.
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?
#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
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
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.