Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active December 20, 2021 09:44
Show Gist options
  • Save bboyho/d58b3514e21d08a503f692dd7cba14b0 to your computer and use it in GitHub Desktop.
Save bboyho/d58b3514e21d08a503f692dd7cba14b0 to your computer and use it in GitHub Desktop.
/*
Written by: Ho Yun "Bobby" Chan
Modified: Dec 20, 2021
@ SparkFun Electronics
"Breathing sleep LED, like on a Mac."
Originally Written By: Jeremy Saglimbeni 2011
thecustomgeek.com
Description: This sketch makes the LEDs breath like on a Mac.
The code is modified to adjust for different colors on an
RGB LED strip.
-----RGB Common Anode LED Strip Connections-----
RGB Common Anode LED Strip => BJT/MOSFET => Arduino
R pin => transistor => 5
G pin => transistor => 6
B pin => transistor => 9
- pin
License: ?
*/
// Define our LED pins:
#define redPin 5
#define greenPin 6
#define bluePin 9
// Create float variables for our LED color value:
int redValue = 1;
int greenValue = 1;
int blueValue = 1;
float tempRedValue = 1.0;
float tempGreenValue = 1.0;
float tempBlueValue = 1.0;
int brightness_LED = 0;
void setup() {
Serial.begin(115200);
Serial.println("Begin Breathing RGB LED");
pinMode(redPin, OUTPUT); // sets the red as output
pinMode(greenPin, OUTPUT); // sets the green as output
pinMode(bluePin, OUTPUT); // sets the blue as output
// bring the LED up nicely from being off
for (brightness_LED = 0 ; brightness_LED <= 15; brightness_LED += 1)
{
//CYAN
tempRedValue = 0.0;
tempGreenValue = 1.0;
tempBlueValue = 1.0;
redValue = int(tempRedValue * brightness_LED);
greenValue = int(tempGreenValue * brightness_LED);
blueValue = int(tempBlueValue * brightness_LED);
show_RGB();
delay(5);
}
}//end setup()
void loop()
{
for (brightness_LED = 15 ; brightness_LED <= 255; brightness_LED += 1)
{
//CYAN
tempRedValue = 0.0;
tempGreenValue = 1.0;
tempBlueValue = 1.0;
redValue = int(tempRedValue * brightness_LED);
greenValue = int(tempGreenValue * brightness_LED);
blueValue = int(tempBlueValue * brightness_LED);
show_RGB();
if (brightness_LED > 150) {
delay(4);
}
if ((brightness_LED > 125) && (brightness_LED < 151)) {
delay(5);
}
if (( brightness_LED > 100) && (brightness_LED < 126)) {
delay(7);
}
if (( brightness_LED > 75) && (brightness_LED < 101)) {
delay(10);
}
if (( brightness_LED > 50) && (brightness_LED < 76)) {
delay(14);
}
if (( brightness_LED > 25) && (brightness_LED < 51)) {
delay(18);
}
if (( brightness_LED > 1) && (brightness_LED < 26)) {
delay(19);
}
}
for (brightness_LED = 255; brightness_LED >= 15; brightness_LED -= 1)
{
//CYAN
tempRedValue = 0.0;
tempGreenValue = 1.0;
tempBlueValue = 1.0;
redValue = int(tempRedValue * brightness_LED);
greenValue = int(tempGreenValue * brightness_LED);
blueValue = int(tempBlueValue * brightness_LED);
show_RGB();
if (brightness_LED > 150) {
delay(4);
}
if ((brightness_LED > 125) && (brightness_LED < 151)) {
delay(5);
}
if (( brightness_LED > 100) && (brightness_LED < 126)) {
delay(7);
}
if (( brightness_LED > 75) && (brightness_LED < 101)) {
delay(10);
}
if (( brightness_LED > 50) && (brightness_LED < 76)) {
delay(14);
}
if (( brightness_LED > 25) && (brightness_LED < 51)) {
delay(18);
}
if (( brightness_LED > 1) && (brightness_LED < 26)) {
delay(19);
}
}
delay(970);
}//end loop()
void show_RGB() {
//once value is calculated, show the LED color
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment