Skip to content

Instantly share code, notes, and snippets.

@alexleekt
Last active August 29, 2015 14:17
Show Gist options
  • Save alexleekt/d8c7361bbfa9ebe3ea53 to your computer and use it in GitHub Desktop.
Save alexleekt/d8c7361bbfa9ebe3ea53 to your computer and use it in GitHub Desktop.
Arduino breathing light - with variable brightness and breathing rate.
/*
Arduino breathing light - with variable brightness and breathing rate.
*/
#include <math.h>
const int PIN_POT = 0;
const int PIN_LED = 13;
const int BRIGHTNESS_MIN = 0;
const int BRIGHTNESS_MAX = 255;
const float DURATION_FACTOR_MIN = 0.5;
const float DURATION_FACTOR_MAX = 2.0;
void setup() {
Serial.begin(9600);
pinMode(PIN_POT, INPUT);
pinMode(PIN_LED, OUTPUT);
}
int pot_value;
void loop() {
pot_value = analogRead(PIN_POT);
float brightness = map(pot_value, 0, 1023, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
float biased_duration_factor = map(pot_value, 0, 1023, DURATION_FACTOR_MIN, DURATION_FACTOR_MAX);
float brightnessFunction = (exp(sin(millis() / 4000.0 * PI * biased_duration_factor)) - 0.36787944) * 108.0;
brightness = map(brightnessFunction, BRIGHTNESS_MIN, BRIGHTNESS_MAX, 0, brightness);
analogWrite(11, brightness);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment