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);
}
/*
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);
}
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.
/*
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.
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.
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