Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Last active December 19, 2018 10:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/a2734bc0944d6f566fc22480447a0f7a to your computer and use it in GitHub Desktop.
Save sixtyfive/a2734bc0944d6f566fc22480447a0f7a to your computer and use it in GitHub Desktop.
#include <math.h>
unsigned const int led_count = 5;
int led_pin[led_count] = {10, 9, 6, 5, 3};
const unsigned int min_val = 25; // never go dark completely
const unsigned int max_val = 255; // don't kill the LEDs (atm there are no resistors)
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("Hello there!");
for (unsigned int i=0; i<led_count; i++)
pinMode(led_pin[i], OUTPUT);
}
unsigned int val[led_count] = {0, 0, 0, 0, 0};
int prefix[led_count] = {1, 1, 1, 1, 1};
void set_brightness(int pin_index)
{
val[pin_index] = val[pin_index] + (1 * prefix[pin_index]);
if (prefix[pin_index] == 1 && val[pin_index] >= max_val)
prefix[pin_index] = -1;
if (prefix[pin_index] == -1 && val[pin_index] <= min_val)
prefix[pin_index] = 1;
// modify curve to be more breath-like
float v = exp(sin(val[pin_index] / (10*PI)) - 0.36787944) * 108.0;
analogWrite(led_pin[pin_index], (int)v);
}
bool led_started[led_count];
unsigned int led_start_time[led_count] = {0, 1000, 2000, 3000, 4000};
unsigned long last_millis[led_count] = {0, 0, 0, 0, 0};
const unsigned int offset = 5*PI;
void loop()
{
for (unsigned int i=0; i<led_count; i++) {
if (!led_started[i] && millis() > led_start_time[i]) {
led_started[i] = true;
}
if (millis()-last_millis[i] >= offset && led_started[i]) {
set_brightness(i);
last_millis[i] = millis();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment