Skip to content

Instantly share code, notes, and snippets.

@dmccreary
Created November 12, 2013 04:28
Show Gist options
  • Save dmccreary/7425484 to your computer and use it in GitHub Desktop.
Save dmccreary/7425484 to your computer and use it in GitHub Desktop.
Sample Arduino C program to fade and LED slowly on and off and control the delay time of ramp up, on, ramp down and off.
/* Based on the Arduino fader example
Dan McCreary
Nov. 2013 */
int ledPin = 11; // LED connected from digital pin 11 to ground
int maxb = 155; // max brightness 0 to 255
int inc = 1; // step increment value: typical values are from 1 to 10
int slope = 20; // delay at each step in milliseconds - combine with inc to change speed transition
int delayOn = 1000; // how long to stay at max brightness in milliseconds
int delayOff = 2000; // how long to stay off in milliseconds
void setup() {
// nothing happens in setup
}
void loop() {
// fade in loop
for(int fadeValue = 0 ; fadeValue <= maxb; fadeValue +=inc) {
// sets the new value as we make the LED brighter (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait see the dimming effect
delay(slope);
} ;
// how long to stay at full brightness
delay(delayOn);
// fade out loop
for(int fadeValue = maxb ; fadeValue >= 0; fadeValue -=inc) {
// sets the new value as we dim down
analogWrite(ledPin, fadeValue);
// wait a bit
delay(slope);
};
// how long to stay at the off position
delay(delayOff);
}
@Eddiewards
Copy link

Hi there, great program but please could you tell me how to keep the (delay on) for 7 hours?
My dad is using this program for marine fish tank led's but having trouble with timings.please help.
Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment