Skip to content

Instantly share code, notes, and snippets.

@prog
Created November 19, 2014 20:07
Show Gist options
  • Save prog/79f14b6e321c6014ee2c to your computer and use it in GitHub Desktop.
Save prog/79f14b6e321c6014ee2c to your computer and use it in GitHub Desktop.
Arduino LED sine wave pulsing without PWM
#define LED 13
#define FRAME 20000ul // 20000us => 50Hz
#define PERIOD 1000000ul // 1s
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
unsigned long t;
float p, f, v;
t = micros();
f = (t % FRAME) / (float) FRAME;
p = (t % PERIOD) / (float) PERIOD;
v = (1.0 + sin(p * 2.0 * PI)) / 2.0;
digitalWrite(LED, v > f ? HIGH : LOW);
}
@prog
Copy link
Author

prog commented Nov 19, 2014

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