Skip to content

Instantly share code, notes, and snippets.

@bakercp
Created April 3, 2013 21:01
Show Gist options
  • Save bakercp/5305264 to your computer and use it in GitHub Desktop.
Save bakercp/5305264 to your computer and use it in GitHub Desktop.
"LED that I'm soldering to a protoshield to start off, fade up to full brightness for a period of about 45 seconds, begin fading to dark again and as it dims, to strobe."
const byte ledPin = 11; // pwm pin
boolean bIsGoingUp = true;
long fadeUpTime = 15000; // 45 * 1000 milliseconds
long fadeDownTime = 15000; //
long nextTransition = -1;
long strobeOnTime = 200; // the time the strobe effect will be on
long strobeOffTime = 1000; // the time the normal fade out will be on
boolean bIsStrobeOn = false;
long nextStrobeTransition = -1;
void setup() {
// nothing b/c we are using pwm out
Serial.begin(9600);
}
void loop() {
long now = millis();
if(now > nextTransition) {
Serial.println("Transitioning.");
if(bIsGoingUp) {
nextTransition = now + fadeDownTime;
bIsGoingUp = false;
Serial.println("Going down!");
} else {
nextTransition = now + fadeUpTime;
bIsGoingUp = true;
Serial.println("Going up!");
}
}
long remainingTime = nextTransition - now;
Serial.println(remainingTime);
byte pwmValue = 0;
if(bIsGoingUp) {
pwmValue = map(remainingTime,fadeUpTime,0,0,255);
} else {
if(bIsStrobeOn) {
pwmValue = 255; // max
} else {
pwmValue = map(remainingTime,fadeUpTime,0,255,0);
}
if(now > nextStrobeTransition) {
if(bIsStrobeOn) {
nextStrobeTransition = now + strobeOffTime;
bIsStrobeOn = false;
} else {
nextStrobeTransition = now + strobeOnTime;
bIsStrobeOn = true;
}
}
}
analogWrite(ledPin,pwmValue);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment