Fade with a flash

I need to fade a LED up with a flash then fade down and loop.

I know this will do the fade
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9; // the pin that 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);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

I need to fade a LED up with a flash

What does this mean?

Need the LED to be off, fade up to almost full power then flash, fade be to off. This will loop

fade up to almost full power then flash, fade be to off.

What the hell does "then flash" mean?

Try to repeat the red light in the middle

Try fading 0 to 127 then pulsing at 255 then fading back down again from 127 to 0.

Sounds easy....... but I'm new to the code. How would I add that to the above fade.

Give this a try.

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 9;           // the pin that 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);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

 // check to see if we are ready for flash
  if (brightness > 127 || brightness < 0) {
   // flash the led
    analogWrite(led, 255);
   // start the dimming effect 
    fadeAmount = -fadeAmount ;
  }   
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

it starts out at about 127 and fades up to 255 then hits 127 with no fade. I changed teh analog to digital as i'm using a digital pin

Try the code again and leave the analogWrite you need it to control the brightness. I had a typo originally (corrected) I will check with an arduino and post the results and/or tweaks in a bit.

That works, playing with some numbers but only need to flash on the fade up and not at 0

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 9;           // the pin that 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);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

 // check to see if we are ready for flash
  if (brightness > 127) {
   // flash the led
    analogWrite(led, 255);
   // start the dimming effect 
    fadeAmount = -fadeAmount ;
    Serial.println(brightness);
  }
  if (brightness <= 0) {
   fadeAmount = -fadeAmount;
  }  

  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

better but it's funny on my 328 board i have to do analogWrite(led, 0); for the light to be on....So I cahange the line in teh code to get it on. so it fades out and flashes then fads back to full.

I tested the code I just posted on an arduino uno and it fades up to half brightness flashes then fades down. Then repeats the process over and over.

So is it working for you in this same manner or is there still a bug?

On my board it is switched. If I give a light a 0 it is on. If I give it a 255 it is off. Guess a code for flash when the led day out would work on my board. It was a cheap $3 one from China.

Ahh yes, another reason to use a genuine arduino.

Well just adjust the values accordingly hopefully you get the idea now thought.

Ok I took the delay out of the bottom It works it works...
/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/
int led = 9; // the pin that the LED is attached to
int brightness = 255; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const unsigned long beaconinterval = 30; // Time to fade.
unsigned long beacontimer; // time
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(9600);
beacontimer = millis ();
}
// the loop routine
void beaconfade () {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness - fadeAmount;
// check to see if we are ready for flash
if (brightness < 127) {
// flash the led
analogWrite(led, 0);
// start the dimming effect
fadeAmount = -fadeAmount ;
Serial.println(brightness);
}
if (brightness >= 255) {
fadeAmount = -fadeAmount;
}
// remember when we toggled it
beacontimer = millis ();
} // end of toggleRedLED
void loop ()
{
// Reset beacon
if ( (millis () - beacontimer) >= beaconinterval)
beaconfade ();
} // end of loop

Thanks for all the help!!!!!!!!!!!!!!

Any way to do the above code without serial in it.

Any way to do the above code without serial in it.

Highlight the lines containing Serial and press the delete key.