Skip to content

Instantly share code, notes, and snippets.

@caffo
Created November 16, 2023 19:47
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save caffo/b2cfb47c1326578fdfa612ff9b5f05ee to your computer and use it in GitHub Desktop.
a calming blinking light — https://caffo.link/led
const int ledPin = 13;
unsigned long startTime;
float currentDelay;
float initialBPM;
float finalBPM;
unsigned long interval; // Duration of BPM change
unsigned long holdInterval; // Duration to hold at final BPM
unsigned long fadeInterval; // Duration of fade to off
unsigned long totalTime; // Total time until LED turns off
// Function to convert minutes to milliseconds
unsigned long minutesToInterval(float minutes) {
return minutes * 60 * 1000;
}
void setup() {
pinMode(ledPin, OUTPUT);
startTime = millis(); // Record the start time
// Set your desired BPM and intervals here
initialBPM = 220.0; // Starting BPM
finalBPM = 60.0; // Final BPM
interval = minutesToInterval(20); // Duration of BPM change (20 minutes)
holdInterval = minutesToInterval(20); // Hold at final BPM (20 minutes)
fadeInterval = minutesToInterval(30); // Fade duration (30 minutes)
totalTime = interval + holdInterval + fadeInterval; // Total duration
// Calculate the initial delay in milliseconds
currentDelay = 60000.0 / initialBPM; // 60,000 milliseconds in a minute
}
void loop() {
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - startTime;
if (elapsedTime < interval) {
// Gradually change BPM
float intervalFraction = (float)elapsedTime / interval;
float bpm = initialBPM + (finalBPM - initialBPM) * intervalFraction;
currentDelay = 60000.0 / bpm;
} else if (elapsedTime < interval + holdInterval) {
// Hold at final BPM
currentDelay = 60000.0 / finalBPM;
} else if (elapsedTime < totalTime) {
// Fading logic will be added here later
} else {
// Turn off LED after total duration
digitalWrite(ledPin, LOW);
return; // Stop the loop
}
// Blink the LED
digitalWrite(ledPin, HIGH);
delay((int)currentDelay);
digitalWrite(ledPin, LOW);
delay((int)currentDelay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment