Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jwillmer
Last active April 28, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwillmer/18f8f700b48149c8a69f47db6b6bd4ed to your computer and use it in GitHub Desktop.
Save jwillmer/18f8f700b48149c8a69f47db6b6bd4ed to your computer and use it in GitHub Desktop.
Pulse a LED
// Source: https://www.sparkfun.com/tutorials/329
// To understand the code you need to look at the sinus curve in the toturial.
const int LED = 0;
void setup() {
}
void loop()
{
// prevent LED to light on max brightness by stoping before the curve reaches the top plane
float trimBrightnessMax = 0.854;
// optional: modify sinus curve.
// use the same options for rising and falling fade to prevent a missmatch of max/min brightness
float sinRange = 127.5;
float sinOffset = 127.5;
// modify step length to change the transition speed
float stepLength, start, end, out;
// rising (start from zero and go to max brightness)
// start value is at bottom of the curve
// end value is the top of the sinus curve
start = 4.712; //pi*1.5
end = 7.854; //pi*2.5
end -= trimBrightnessMax;
stepLength = .001;
fade(start, end, stepLength, sinRange, sinOffset);
// pause at peak brightness
delay(500);
// falling (start from max brightness and go to zero)
// start value is the top of the sinus curve
// end value is at bottom of the curve
start = 1.570; //pi*0.5
start += trimBrightnessMax;
end = 4.712; //pi*1.5
stepLength = .0008;
fade(start, end, stepLength, sinRange, sinOffset);
// pause when LED is off
delay(2000);
}
void fade(float start, float end, float stepLength, float sinRange, float sinOffset) {
float out;
while (start < end)
{
start = start + stepLength;
out = sin(start) * sinRange + sinOffset;
analogWrite(LED, out);
delay(1);
}
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment