Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created December 25, 2010 10:09
Show Gist options
  • Save jimsynz/754809 to your computer and use it in GitHub Desktop.
Save jimsynz/754809 to your computer and use it in GitHub Desktop.
Pulse the LED with a sine wave.
/*
My second sketch: pulse the LED with a sine wave.
*/
const float pi = 3.14159;
int ledPin = 9;
void sinArc(int analogPin, int duration, float arcStart, float arcStop) {
int steps = 255;
int startStep = (int)(arcStart * (float)steps);
int stopStep = (int)(arcStop * (float)steps);
int sleep = (int)((float)duration / (float)(stopStep - startStep));
int maxAmplitude = 256;
int DCoffSet = 0;
for(int _step = 0; _step <= steps; _step += 1) {
int amplitude = DCoffSet + (int)((float)maxAmplitude * sin(pi * (float)_step / (float)steps));
analogWrite(analogPin, amplitude);
delay(sleep);
}
}
void sinOn(int analogPin, int duration) {
sinArc(analogPin, duration, 0.0, 0.5);
}
void sinOff(int analogPin, int duration) {
sinArc(analogPin, duration, 0.5, 1.0);
}
void sinWave(int analogPin, int duration) {
sinArc(analogPin, duration, 0.0, 1.0);
}
void setup() {
pinMode(13, OUTPUT);
analogWrite(ledPin, 0);
}
void loop() {
sinWave(ledPin, 2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment