Last active
May 27, 2024 22:54
-
-
Save jaythomas/4e1c2e71ac708f6263b3ec3324602426 to your computer and use it in GitHub Desktop.
Wemos ESP8266 D1 Mini RGB led swell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *********************************************************** | |
* Global Constants * | |
* Hardware Definitions * | |
* ********************************************************* */ | |
// Wemos Mini D1 Pro pinout. This should have been provided by selecting the correct board but this | |
// board wasn't available when I looked for it so whatever this requires less dependency on the | |
// Arduino IDE configuration anyway. | |
// https://arduino-projekte.info/wp-content/uploads/2017/03/wemos_d1_mini_pro_pinout.png | |
byte PIN_D0 = 16; | |
byte PIN_D1 = 5; | |
byte PIN_D2 = 4; | |
byte PIN_D3 = 0; | |
byte PIN_D4 = 2; | |
byte PIN_D5 = 14; | |
byte PIN_D6 = 12; | |
byte PIN_D7 = 13; | |
byte PIN_D8 = 15; | |
/* The Arduino Web site recommends that one uses const | |
* rather than #define since using #define could have | |
* unwanted side effects | |
*/ | |
const int redPin = PIN_D3; // Red LED pin | |
const int greenPin = PIN_D2; // Green LED pin | |
const int bluePin = PIN_D1; // Blue LED pin | |
const int maxBright = 255; // Maximum LED brightness | |
/* *********************************************************** | |
* setColor Functions * | |
* ********************************************************* */ | |
void setColor(int redValue, int greenValue, int blueValue){ | |
analogWrite(redPin, redValue); | |
analogWrite(greenPin, greenValue); | |
analogWrite(bluePin, blueValue); | |
Serial.print("Red: "); | |
Serial.print(redValue); | |
Serial.print(" Green: "); | |
Serial.print(greenValue); | |
Serial.print(" Blue: "); | |
Serial.println(blueValue); | |
} | |
/* *********************************************************** | |
* setup Function * | |
* ********************************************************* */ | |
void setup(){ | |
// Turn on the Serial Port | |
Serial.begin(115200); | |
pinMode(redPin, OUTPUT); | |
pinMode(greenPin, OUTPUT); | |
pinMode(bluePin, OUTPUT); | |
} | |
/* *********************************************************** | |
* loop Function * | |
* ********************************************************* */ | |
void loop(){ | |
//Local Variables | |
int longDelay = 1000; // fading time between colors | |
int shortDelay = 10; // short time delay between colors | |
int redValue; | |
int greenValue; | |
int blueValue; | |
int ledGear; // For switching LED fading | |
// Various ways to turn on the RGB LED; | |
// Here we manually set the color | |
Serial.println("Manual Colors"); | |
setColor(maxBright, 0, 0); // Red Color | |
delay(longDelay); | |
setColor(0, maxBright, 0); // Green Color | |
delay(longDelay); | |
setColor(maxBright, maxBright, maxBright); // White Color | |
delay(longDelay); | |
setColor(170, 0, maxBright); // Purple Color | |
delay(longDelay); | |
Serial.println("Fade from Red to Green"); | |
//Fade from Red to Green | |
delay(longDelay); | |
redValue = maxBright; | |
greenValue = 0; | |
blueValue = 0; | |
for(int i = 0; i < maxBright+1; i++){ | |
setColor(redValue, greenValue, blueValue); | |
redValue -= 1; | |
greenValue += 1; | |
delay(shortDelay); | |
} | |
Serial.println("Fade from Green to Blue"); | |
//Fade from Green to Blue | |
delay(longDelay); | |
redValue = 0; | |
greenValue = maxBright; | |
blueValue = 0; | |
for(int i = 0; i < maxBright+1; i++){ | |
setColor(redValue, greenValue, blueValue); | |
greenValue -= 1; | |
blueValue += 1; | |
delay(shortDelay); | |
} | |
Serial.println("Fade from Blue to Red"); | |
//Fade from Blue to Red | |
delay(longDelay); | |
redValue = 0; | |
greenValue = 0; | |
blueValue = maxBright; | |
for(int i = 0; i < maxBright+1; i++){ | |
setColor(redValue, greenValue, blueValue); | |
blueValue -= 1; | |
redValue += 1; | |
delay(shortDelay); | |
} | |
Serial.println("Fade All three colors Red to Green, Green to Blue, Blue to Red"); | |
//Fade All three colors Red to Green, Green to Blue, Blue to Red | |
delay(longDelay); | |
redValue = maxBright; | |
greenValue = 0; | |
blueValue = 0; | |
ledGear = 1; | |
do { | |
setColor(redValue, greenValue, blueValue); | |
//First Gear - Fade from Red to Green | |
if (ledGear == 1) { | |
redValue -= 1; | |
greenValue += 1; | |
} | |
//Second Gear - Fade from Green to Blue | |
if (ledGear == 2){ | |
greenValue -= 1; | |
blueValue += 1; | |
} | |
//Third Gear - Fade from Blue to Red | |
if (ledGear == 3){ | |
blueValue -= 1; | |
redValue +=1; | |
} | |
//Gear Switching | |
if (greenValue == maxBright) ledGear = 2; | |
if (blueValue == maxBright) ledGear = 3; | |
if (redValue == maxBright+1) ledGear = 4; | |
delay(shortDelay); | |
} while (ledGear < 4); | |
delay(longDelay); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment