I'm pretty new to arduino. I want to recreate the flashing ditch lights on a locomotive, but I would like them to fade to look the incandescent bulbs used on real trains. (here's a good example: Ex-BN MAC with Flashing Ditch Lights Leads BNSF Coal Train - YouTube) I have this basic code working for a single led on my arduino: https://docs.arduino.cc/built-in-examples/basics/Fade
but the effect doesn't quite resemble an incandescent bulb. I think it's too linear. How do I make it look more realistic?
Welcome to the forum
Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial
well considering that your end brightness value is an 8-bit integer, you can use sin() to create a sinus curve, it is all just in the math.
Instead of subtracting a constant, as is being done in the example, you could divide by a constant to get an exponential drop.
As you need to do “ two things at once”
Putting a slight fade as the lights switch, have a look at the example blink without delay .
You can use the pwm ( “analog”) pins to drive the leds , the analog values you write will control brightness ( as you’ve seen )
Have a play with those , see if you can think of a method of doing what you want ( I would use some sort of equation to vary brightness with passing millis()). Ask when (if) you get stuck .
Put the values to be written to the LED and/or the length of each period in an array and step trough them rather than having a fixed step value and step period
I see several problems here. When the power to an incandescent light is turned off, the reduction of light is linear because of heat dissipation. Your vision is NOT linear, as with other senses, it is logarithmic. Then there is the problem of the wavelength of light changing for the cooling light filament and your eye sensitivity to the different wavelengths.
You need to develop a stepped reduction of the LED light using, perhaps a table of values for the pulse values. Process the table in ascending order to turn off the light and in descending order to turn the LED back on. The values of the table will be based on your personal perceptions.
From:
We need to know exactly what hardware you have. We need to know what Arduino board (or other brand, such as ESP32) you are using and exactly what other hardware you are connecting it to. The more details we have, the easier it is to help.
If you are using something that is a non-Arduino product then it helps to have a link to its technical data sheet or a link to where you bought it.
Are you sure about that?
You could try my MobaTools library. it was developed with such things in mind. The lib can be installed be means of the library manager.
So give this a try and you can play with times and characteristics.
#include <MobaTools.h>
// Demo of flashing light
const byte ledPin = 13; // arbitrary digital pin
MoToSoftLed myLed;
void setup() {
myLed.attach(ledPin);
myLed.riseTime( 600 ); // risetime in ms
}
void loop() {
//myLed.riseTime( 800 ); // if you want different times for switching on and off
myLed.write(ON,BULB); // characteristic can be BULB or LINEAR
delay(800);
//myLed.riseTime( 600 ); // if you want different times for switching on and off
myLed.write(OFF,BULB);
delay(800);
}
By now i think an RGB LED would be the best option, they also come in THT and in milky cast.
The other option would be to just use really small incandescent bulbs. In some way there is no beating the original.
Yeah, that. You would have to correct for perceived brightness.
Have a look into 'gamma correction' and how the human eye perceives PWM LED brightness.
[EDIT]
[/EDIT]
Look at FadeLed in the IDE library.
A bulb light is more red/yellow when faded, and more blueish at very high temperature (think halogen lights).
This code will give reasonably similar results to the train lights shown in your example.
You can adjust the constants to give the speed and max min values.
/*
FadeLamp
https://skillbank.co.uk/arduino/index.htm
exaple written for arduino Uno
*/
int led = 9; // the PWM pin the LED is attached to
int state = 2; //fading up
int downfadeamount = 2; // how many to decrease the brighness
int upfadeamount = 5; // how many to increase the brighness
int rate = 12; //time between each new brightness
int max = 200; //maximum brightness
int min = 40; //minimum brightness
int upPause = 100; //delay betwen fade up & fade down
int downPause = 100; //delay betwen fade down & fade up
int brightness = 100; // how bright the LED is currently
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(57600); //for diagnostics
}
// the loop routine runs over and over again forever:
void loop() {
switch (state) {
case 0: { //fading down
brightness -= downfadeamount;
if (brightness <= min) state = 1; //finished fading down
analogWrite(led, brightness); // set the brightness of pin 9:
Serial.print("0: brightness is ");
Serial.println(brightness);
}
break;
case 1: {
delay(upPause);
Serial.println("1: delay");
state = 2;
}
break;
case 2: { //fading up
brightness += upfadeamount;
if (brightness >= max) state = 3; //finished fading up
analogWrite(led, brightness); // set the brightness of pin 9:
Serial.print("2: brightness is ");
Serial.println(brightness);
}
break;
default: {
delay(downPause);
Serial.println("3: delay");
Serial.println(" ");
state = 0;
}
break;
}
delay (rate);
}