Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Created March 7, 2018 00:37
Show Gist options
  • Save pepasflo/df6427eda9b7372f37a9202d85feda30 to your computer and use it in GitHub Desktop.
Save pepasflo/df6427eda9b7372f37a9202d85feda30 to your computer and use it in GitHub Desktop.
An Arduino sketch which fades an LED in and out.
int led_pin = 13;
void setup() {
pinMode(led_pin, OUTPUT);
}
void loop() {
int on_time;
int off_time;
int period = 1000;
for(int i = 1; i <= period; i = i+1) {
on_time = i;
digitalWrite(led_pin, HIGH);
delayMicroseconds(on_time);
off_time = period - on_time;
digitalWrite(led_pin, LOW);
delayMicroseconds(off_time);
}
for(int i = period; i > 0; i = i-1) {
on_time = i;
digitalWrite(led_pin, HIGH);
delayMicroseconds(on_time);
off_time = period - on_time;
digitalWrite(led_pin, LOW);
delayMicroseconds(off_time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment