Created
November 19, 2014 20:07
-
-
Save prog/79f14b6e321c6014ee2c to your computer and use it in GitHub Desktop.
Arduino LED sine wave pulsing without PWM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://youtu.be/EUwtL09cJ6s