Hello everyone, I'm very new to the arduino development scene and I could sure use some help. I just have a couple of basic questions.
I need code that will fade up an LED but keep it lit (not dim it back down). I tried modifying the fading sketch built into the program but I didn't have any luck.
Is it possible to fed an LED on pins that dont have PWM?
Is there a resource available that will teach me how to program in C for arduino?
I need code that will fade up an LED but keep it lit (not dim it back down). I tried modifying the fading sketch built into the program but I didn't have any luck.
If you delete the for loop that fades out, it won't fade out -- however, because the for loop that fades in is in the void loop routine, it'll start over again and again. Result: the led fades in, then goes out instantly and starts fading in again. So this loop needs to be moved to the void setup routine so it only runs once.
Untested, but try this:
int value = 0; // variable to keep the actual value
int ledpin = 9; // light connected to digital pin 9
void setup()
{
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
}
void loop()
{
// nothing to loop
}
Ok, so this code is probably super inefficient and terrible, but it semi works. I would really like to fade up a series of leds in succession which is what this code does but its very choppy. Is there any way I can smooth the transition? I tried lowering the value+ down to one and that helped a bit, I soon discovered you can't use numbers less than one (noOb) lol.
int value = 0; // variable to keep the actual value
int ledpin = 3; // light connected to digital pin 9
int ledpin1 = 4;
int ledpin2 = 12;
int ledpin3 = 13;
void setup()
{
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin, value); // sets the value (range from 0 to 255)
delay(2); // waits for 30 milli seconds to see the dimming effect
}
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin1, value); // sets the value (range from 0 to 255)
delay(2); // waits for 30 milli seconds to see the dimming effect
}
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin2, value); // sets the value (range from 0 to 255)
delay(2); // waits for 30 milli seconds to see the dimming effect
}
for(value = 0 ; value <= 255; value+=1) // fade in (from min to max)
{
analogWrite(ledpin3, value); // sets the value (range from 0 to 255)
delay(2); // waits for 30 milli seconds to see the dimming effect
}
the best way to do this is to use the system ticker function millis()
write a function for each led that does this:
remember when it was the last time the led brightness changed
if a change is due, do it. otherwise leave the brightness as is and exit
the in your loop function just call the functions fore each led.
void setup(void) {}
void loop(void) {
update_led1();
// update_led2();
/* and so on */
}
void update_led1(void) {
static long int last_run = 0; /* set it to 0 just once */
static long int interval = 10; /* set it to 10 just once */
long int now = millis();
if ( now > last_run + interval ) {
/* blabla */
}
last_run = millis();
}
Seriously, all you need is there. There's also an example coming with the arduino software called "blink without delay" or something like that. That is basically the same as what I've written down here. That is 99% of what you need.
Madworm is quite correct (if mildly sacrastic), and the "loop" construct is very apt.
Very few of us get it all right first time, so the iteration "think, type, check" is how software is developed.
Start thinking about a single LED but without using the "delay" function, and work up from there.
"delay" simply wastes time that you could use for other things like controlling at other LEDs.
I usually add the advice of thinking about how you yourself would do the problem -
Look at the clock,
Is it time to do something yet? - yes, do it, otherwise go look at the clock again. etc.
Spend your time either looking at the clock or doing useful stuff, instead of just waiting for the egg-timer to go off.
Above all, look around this forum - you don't imagine that you're the only person to have had this problem, do you?