hello guys, i'am new to arduino UNO family as i 'am still in beginners level i tried to blink 2 LED and fade another LED at single programming. i succeeded in blinking the LED's but same as usual fade doesn't appear to be perfect. It doesn't fade in given delay() seconds please help me.
programme:
int pin1 = 7;
int pin2 = 8;
int pin3 = 5;
int brightness = 0;
int fadeAmount = 5;
At the moment you are fading LED3 by one "fadeAmount" then waiting for 300ms plus the time taken for the other LEDs to blink on then off. Only then does it loop back again and fade by one more "fadeAmount". So fading up and down takes a long time.
Have a look at the Blink without Delay example sketch and apply that method to your blinking and fading.
Please read How to use the forum and please edit your post accordingly (especially the part about code-tags ;))
And it's the same problem as seen a million times here and it's the delay(). A delay() does exactly that, it stops EVERYTHING dead in it's tracks and just waits. So the fading part will have to wait for the delay()'s in the blinking part. So as I said many times before, chapter two of every Arduino howto should be called "NEVER use delay() again!". See Blink without delay for that.
And if you want easy fading and even have a nicer fading because of gamma correction, see the FadeLed-library in my footer. But even then, you need to call update() as often as possible so again, NO delay()'s in the rest of the code.
Welcome to this forum, It’s a little tricky at first to use, but you will soon work it out.
As for LED’s. normally to control the brightness of LED’s, you turn it on and off rapidly, changing the duration of the off period to reduce the brightness. If the on and off periods are equal, then the LED will be ½ as bright, cycling though the loop quickly! Examples below.
// LED 1 On @ ½ max brightness.
digitalWrite(pin1,HIGH);
delay(8);
digitalWrite(pin1,LOW);
delay(8);
// LED 1 On @ 3/4 max brightness.
digitalWrite(pin1,HIGH);
delay(6);
digitalWrite(pin1,LOW);
delay(8);
// LED 1 On @ 1/4 max brightness.
digitalWrite(pin1,HIGH);
delay(2);
digitalWrite(pin1,LOW);
delay(8);
// LED 1 On @ 100% max brightness.
digitalWrite(pin1,HIGH);
delay(8);
digitalWrite(pin1,LOW);
delay(0);
Yeahhhhhh, just don't do that until you really need software PWM. : And even then delay() is terrible. A standard Arduino Uno has 6 PWM pins that just do that but in hardware.
So here is an example, not quite want you want but on the way. Move LED 3 to Output #9. (digital)
This code should reduce the brightness to below 1/2 way than back up again over 500 ms.. try playing with the values of "Brightness and "FadeAmont".
int pin1 = 7;
int pin2 = 8;
int pin3 = 9;//Move this LED from analog.
int brightness = 25;
int fadeAmount = 1;
void setup()
{
pinMode (pin1, OUTPUT);
pinMode (pin2, OUTPUT);
pinMode (pin3, OUTPUT);
}
void loop(){
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
while (brightness > 0) {// Fade Out
digitalWrite(pin3, HIGH);
delay(brightness);
digitalWrite(pin3, LOW);
delay(fadeAmount);
brightness -= fadeAmount;
}
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
brightness = 0;
while (brightness < 26) {// Fade In
digitalWrite(pin3, HIGH);
delay(brightness);
digitalWrite(pin3, LOW);
delay(fadeAmount);
brightness += fadeAmount;
}
}
And he's the subborn type. I added the code-tags myself
ragul1230:
As u said i tried mills() function bu again and again it doesn't work on my UNO BOARD.
int pin1 = 9;
int pin2 = 10;
int pin3 = 11;
int pin4 = 7;
int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int fadeAmount1 = 5;
int fadeAmount2 = 5;
int fadeAmount3 = 5;
@ragul1230, that's simple not how millis() works. Millis() doesn't take an argument, it returns the time (in ms) since the Arduino turned on. You should have known if you read the Blink without delay as I suggested you. But then again, you should know about code-tags as well if you read How to use the forum as I told you... :