Go Down

### Topic: Fading two LEDS  (Read 3481 times)previous topic - next topic

#### camillabrix

##### Apr 21, 2015, 12:48 pm
Hi

I am stuck trying to figure out the exercise seen below. I have managed to do the first part
but now I don't know how to add the other LED (the red part of the exercise)??

"EXERCISE 4

Make a LED fade up and down. The fade up should take 1 second
and the fade down should take 0.2 second.

When you have the above running, add another LED that does the opposite of the above,

#1
Hint: subtract.

#### beige22

#2
##### Apr 21, 2015, 02:58 pm
Hi! Can you upload the code you have so far so I can make a suggestion based on it?

#### Zerokj

#3
##### Apr 22, 2015, 12:32 am
I did this:

Code: [Select]
`int led = X;int brightness = 1;int fadeAmount = 7;void setup() { pinMode(led, OUTPUT);}void loop() { analogWrite(led, brightness); brightness = brightness + fadeAmount; if (brightness == 0 || brightness == 255) {   fadeAmount = -fadeAmount ; } delay(0.2);}`

Im begginer, I couldn't understand what you need.
If you need to use 2 LEDs at once you have to sting the other LED with the same cables as the first one.
To both LEDs do the oposite each other, I don't know how to do.

It's my first day using Arduino and I started with LEDs and my Relay Module

Hugs, Zerokj

#### siutoejai

#4
##### Apr 22, 2015, 06:25 am
Quote
delay 1000     = 1 second

the delay should be 200      200/1000 = 0.2 second

delay(0.2) would not work as I know

the min is delay(1)

#### UKHeliBob

#5
##### Apr 22, 2015, 09:47 am
Quote
I have managed to do the first part
Perhaps we could see your code so far.

Are the 2 LEDs supposed to be fading up and down at the same time or one after the other ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### mvmacd

#6
##### Apr 22, 2015, 09:11 pmLast Edit: Apr 22, 2015, 09:13 pm by mvmacd
Hi

I am stuck trying to figure out the exercise seen below. I have managed to do the first part
but now I don't know how to add the other LED (the red part of the exercise)??

"EXERCISE 4

Make a LED fade up and down. The fade up should take 1 second
and the fade down should take 0.2 second.

When you have the above running, add another LED that does the opposite of the above,

First take a look at http://www.arduino.cc/en/Reference/AnalogWrite
On most arduino boards, PWM outputs include 3, 5, 6, 9, 10, and 11.

Wire 2 LEDs, both cathodes to GND, and the anodes to pin 5, and pin 6 respectively.

Code: [Select]
`int pin1 = 5; //PWM pinint pin2 = 6; //PWM pinvoid setup() { pinMode(pin1, OUTPUT); pinMode(pin2, OUTPUT);}byte number = 0;bool goingUp = true; //This sets the directionvoid loop() {    if(goingUp) //this will be true the first time we run and true until we set it false  {      analogWrite(pin1, number);      analogWrite(pin2, 255 - number); //write the opposite      number++; //increase number      if(number == 255)      {          goingUp = false; //switch direction, we reached the top       }    }  else //we are going down here  {       analogWrite(pin1, number);       analogWrite(pin2, 255 - number);              number = number - 5; //decrease number by a factor of 5       //normally -1 means 1 second for 255 loops()       //but we want 0.2 seconds, so that's a 1/5th of a second       //so go 5 times faster than on the fadeUp              if(number <= 0)       {             goingUp = true; //switch direction again, going up           number = 0; //just in case it went in the negative, reset to 0       }  }    delayMicroseconds(3921); //IMPORTANT. for 3.9 milliseconds, we do nothing  //This means roughly, every 255 cycles that equals 1 second}`

#### UKHeliBob

#7
##### Apr 22, 2015, 10:47 pm
I just looked at the original requirement as follows :

Quote
Make a LED fade up and down. The fade up should take 1 second
and the fade down should take 0.2 second.

When you have the above running, add another LED that does the opposite of the above,