Absolute Beginner's Question

Hello,
I’ll just dive in. I’m trying to make two LEDs fade in and out alternately. That is to say that I would like for the loop cycle to constitute:
redPin to fade in/out
yellowPin to remain off
yellowPin to fade in/out
redPin to remain off

My question is how do I assign the fade function to a particular pin?

I’ve tried writing a couple of things, my last attempt being

for(int redPin fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)

Which does not pass verification.

Here is the entire code I am currently working on

int redPin = 9; // red line connected to digital pin 9
int yellowPin = 8; // yellow line connected to digital pin 8
void setup() {
// nothing happens in setup
}

void loop() {
// fade in from min to max in increments of 5 points:
for(int redPin fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(5);
}

// fade out from max to min in increments of 5 points:
for(int redPin fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(5);
}

digitalWrite(redPin, LOW); // set the LED off
delay(1000); // wait for a second

}

The end goal is to make two lines of about twenty LEDs each fade in and out alternately. I found a schematic on this forum to power larger amounts of LEDs using a transistor that I plan to use. I have little experience and the book I just bought pointed me to this forum.

Thank you in advance!
Yann

The 1st argument to analogWrite defines which pin is affected. To fade the red led up:

for(int fadeValue =0; fadeValue<256; fadeValue++)
{
   analogWrite(redPin, fadeValue);
   delay(5);
}

To fade back down:

for(int fadeValue = 255; fadeValue > 0; fadeValue--)
{
   analogWrite(redPin, fadeValue);
   delay(5);
}

If you want a step size of other than 1, change fadeValue++ to fadeValue+=5 (or whatever size you like).

Repeat this code, changing redPin to yellowPin, to fade the other LED.

By the way, in setup, you should declare that redPin and yellowPin are output pins:

pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);

Here is a quick little program to show the basics.
I changed the pin asignment to redpin 9 and yellowpin 10. They both need to be on a PWM pin.

/* alternateledfade.pde
   by Daniel Wright
   2010.FEB.04
   
   A simple little program to show how to alternately fade two leds.
   
*/
int redpin = 9;  // define the pin which will control the red led
int yellowpin = 10;  // define the pin which will control the yellow led
int redval = 0; // initialize the red led value to 0
int yellowval = 0; // initialize the yellow led value to 0
int maxval = 255; //Maximum brightness value

// Setup the program

void setup()

{

 //General setup

 Serial.begin(115200);



 //led pin setup

pinMode(redpin, OUTPUT); // sets the red pin as an output
 pinMode(yellowpin, OUTPUT); // sets the yellow pin as an output
}



// Run the program loop to alternately fade the leds

void loop()

{
for (int i=0; i <= maxval; i++){
redval = i;
 analogWrite(redpin, redval);  // writes the fade up value to the red led
delay(10); //these delays determine how fast the fade goes
}

for (int i=maxval; i >= 0; i--){
redval = i;
 analogWrite(redpin, redval);  // writes the fade down value to the red led
delay(10); //these delays determine how fast the fade goes
}
delay(500); //pause between led fades

for (int i=0; i <= maxval; i++){
yellowval = i;
 analogWrite(yellowpin, yellowval);  // writes the fade up value to the red led
delay(10); //these delays determine how fast the fade goes
}

for (int i=maxval; i >= 0; i--){
yellowval = i;
 analogWrite(yellowpin, yellowval);  // writes the fade down value to the red led
delay(10); //these delays determine how fast the fade goes
}
delay(500); //pause between led fades
}

Have fun and let us know how it goes.

Dan

:wink: