Hi,
I have recently purchased Arduino Nano every. I am trying to code this board to slowly turn on the led light (fade in) when the 12V power is supplied (no switch). When the power is disconnected, LED must turn off slowly (fade out). Can someone please supply the code?
I am not trying to loop this unconditionally but only when the power is supplied and disconnected.
Addressable LED strips are entirely different from a "regular" LED strip where all of the LEDs are powered & controlled together.
Probably nobody is going to hand you the completed code, but I'll give you some hints. And you can start with just the fade-up or fade-down... Don't try to write the whole thing at once.
If it's a non-addressable strip you'll need a Driver circuit to "boost the voltage & current". The driver will pass PWM so the fade example should get you started.
You'll have to figure-out the logic which can be If-statements and loops...
Besides the main loop() there are 3 kinds of loops in C++. For-loops, while() loops, and do-while() loops. (these go inside your main loop. A for-loop is good if you want to count-up (or count down) the brightness.
A "do nothing" while-loop would be handy when you are waiting to start the fade-down. i.e. After the fade-up and while the trigger voltage (derived from 12V) is present , just sit there looping. When the trigger goes-away it will stop looping and go-in to the next step, which is probably a for-loop counting-down the brightness level.
After you're done with the fade-down, a "normal" Arduino would start the main loop over again. But you might want another do-nothing loop that just runs forever until the Arduino is reset.
That can be done as while(1){}; Since "1" is aways "true" it will loop forever, running a loop that doesn't do anything.
Here is my code.
I am using regular led strip that I purchased from AliExpress.
I thought Nano can take upto 12V without any additional changes.
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
While(brightness <=255 ) {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// wait for 30 milliseconds to see the glowing effect
delay(30);
}
Need something here to detect power is off before triggering the second while loop.
While (brightness >0)
{
// set the brightness of pin 9:
analogWrite(led, brightness);
// Reduce the brightness for next time through the loop:
brightness = brightness - fadeAmount;
// wait for 30 milliseconds to see the dimming effect
You will not find any microcontroller that accepts 12V on its digital inputs. Any microcontroller would be burned.
You need to use components to adapt the 12V signal so that it is safe for the Nano's pins. A pair of resistors may be all that is required. For extra safety, an opto-isolator could be used.