Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Gamma correction curtesy of https://learn.adafruit.com/led-tricks-gamma-correction?view=all
- */
- extern const uint8_t gamma8[];
- // Comment out the line below for production
- // (serial monitoring affects timings)
- //#define DEBUG_MODE
- bool isCommonAnode = true; // Set to false for common cathode
- int pinsRGB[3] = { 9, 10, 11 };
- int delayBeforeStartupMS = 3000;
- int powerUpFadeTimeMS = 5000;
- int delayBeforeWarpMS = 6000;
- int goToWarpFadeTimeMS = 3000;
- bool waitForStartup = true;
- bool startupSeq = false;
- bool waitForWarp = false;
- bool gotoWarp = false;
- bool startLoop = false;
- // Colour arrays
- int black[3] = { 0, 0, 0 };
- int white[3] = { 255, 255, 255 };
- int dimWhite[3] = { 76, 76, 76 };
- int red[3] = { 255, 0, 0 };
- int green[3] = { 0, 255, 0 };
- int blue[3] = { 0, 0, 255 };
- // Amber/copper colour choices
- int orange[3] = { 255, 69, 0 };
- int orange2[3] = { 255, 102, 0 }; //#FF6600 rgb(255, 102, 0) rgb(100%, 40%, 0%)
- int orange3[3] = { 255, 128, 0 }; //#FF8000 rgb(255, 128, 0) rgb(100%, 50%, 0%)
- int orange4[3] = { 255, 153, 0 }; // 255, 153, 0;
- int orangeCrush[3] = { 248, 117, 49}; //#F87531 rgb(248, 117, 49) rgb(97%, 46%, 19%)
- int cadmiumOrange[3]= { 255, 97, 3 }; //#FF6103 rgb(255, 97, 3) rgb(100%, 38%, 1%)
- int darkOrange[3] = { 255, 140, 0 }; //#FF8C00 (255, 140, 0)
- int darkOrange5[3] = { 255, 134, 0 }; //#FF8600 rgb(255, 134, 0) rgb(100%, 53%, 0%)
- int cadmiumYellow[3]= { 255, 153, 18 }; //#FF9912 rgb(255, 153, 18) rgb(100%, 60%, 7%)
- int copper[3] = {84, 36, 10};
- int goldish[3] = {247, 216, 131};
- int cyan[3] = {44, 126, 166};
- // Blue colour choices
- int royalBlue[3] = { 65, 105, 225 }; //#4169E1 (65, 105, 225)
- int dodgerBlue[3] = { 30, 144, 255 }; //#1E90FF (30, 144, 255)
- int mediumBlue[3] = { 0, 0, 205 }; //#0000CD (0, 0, 205)
- int mediumBlue2[3] = { 0, 0, 195};
- int mediumBlue3[3] = { 0, 0, 165};
- // Set initial colour values
- int valsRGB[3] = { black[0], black[1], black[2] };
- // Initialize colour variables
- int prevVals[3] = { valsRGB[0], valsRGB[1], valsRGB[2] };
- int delayRGB[3] = { 0, 0, 0 };
- // R/G/B PWM step values
- int stepsRGB[3] = { 0, 0, 0 };
- bool fadingRGB[3] = { false, false, false }; // fade state
- int fadeDirRGB[3] = { 1, 1, 1 }; // "1" for fade up; "-1" for fade down
- void setup() {
- #ifdef DEBUG_MODE
- Serial.begin(115200);
- Serial.println("DEBUG_MODE ON");
- Serial.println("=============");
- #endif
- for (int i = 0; i<3; i++) {
- pinMode(pinsRGB[i], OUTPUT); // sets the pins as output
- writeLED(pinsRGB[i], valsRGB[i]); // sets initial PWM value
- }
- }
- void loop() {
- static unsigned long startTimeRGBms[3]= { millis(), millis(), millis() };
- unsigned long currentTimeMS = millis();
- static unsigned long timeStampMS = millis();
- #ifdef DEBUG_MODE
- Serial.print("currentTimeMS = ");
- Serial.print(currentTimeMS);
- for (int i=0; i<3; i++) {
- Serial.print(", startTimeRGBms[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(startTimeRGBms[i]);
- }
- Serial.println("");
- #endif
- // 1. Initial power-up -> dish and impulse crystal fade up to an orange/copper colour
- // ==================================================================================
- if ( (currentTimeMS >= delayBeforeStartupMS) && (waitForStartup == true) ) {
- waitForStartup = false;
- startupSeq = true;
- startLoop = true; // set boolean for first time thru loop
- }
- if ( startupSeq == true ) {
- if ( startLoop == true ) {
- #ifdef DEBUG_MODE
- Serial.println("IN STARTUP SEQUENCE");
- Serial.println("===================");
- #endif
- startFadeToColourSequence(orange, powerUpFadeTimeMS); // call function to calculate fading variables
- debugPrintValues();
- startLoop = false;
- }
- doPWMStep(currentTimeMS, startTimeRGBms); // call function to do PWM stepping (if individual R/G/B delay has passed)
- if ( fadingRGB[0] == false && fadingRGB[1] == false && fadingRGB[2] == false ) {
- // Cross fade complete; update current values for next cross fade
- for (int i=0; i<3; i++) {
- prevVals[i] = valsRGB[i];
- }
- startupSeq = false;
- waitForWarp = true; // set state variable to begin check for next cross fade
- timeStampMS = currentTimeMS; // start timer for pause before going to warp
- #ifdef DEBUG_MODE
- for (int i=0; i<3; i++) {
- Serial.print("prevVals[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.println(prevVals[i]);
- }
- Serial.println("");
- Serial.print("startupSeq = ");
- Serial.print(startupSeq);
- Serial.print("waitForWarp = ");
- Serial.print(waitForWarp);
- Serial.print(", timeStampMS = ");
- Serial.println(timeStampMS);
- #endif
- }
- }
- // 2. Go to warp
- // =============
- if ( ((currentTimeMS - timeStampMS) >= delayBeforeWarpMS) && (waitForWarp == true) ) {
- waitForWarp = false;
- gotoWarp = true;
- startLoop = true; // set boolean for first time thru loop
- }
- if ( gotoWarp == true ) {
- if ( startLoop == true ) {
- #ifdef DEBUG_MODE
- Serial.println("IN GO TO WARP SEQUENCE");
- Serial.println("======================");
- #endif
- startFadeToColourSequence(mediumBlue3, goToWarpFadeTimeMS); // call function to calculate fading variables
- debugPrintValues();
- startLoop = false;
- }
- doPWMStep(currentTimeMS, startTimeRGBms); // call function to do PWM stepping (if individual R/G/B delay has passed)
- if ( fadingRGB[0] == false && fadingRGB[1] == false && fadingRGB[2] == false ) {
- gotoWarp = false;
- }
- }
- }
- // ------------------------------------------------------------------------------------------------------
- /*
- Purpose: function to calculate delay between PWM steps (specific to each colour)
- Parameters: prevValue [current value]; endValue [fade to value]; overallFadeTimeMS [desired fade time]
- Returns: delay in microseconds
- */
- int calculateDelay(int prevValue, int endValue, int overallFadeTimeMS) {
- // For delay between PWM steps, value needs to be positive so take absolute value of difference
- int delayMS = abs((endValue - prevValue)); // What's the overall gap?
- if (delayMS) { // If its non-zero,
- delayMS = overallFadeTimeMS/delayMS; // divide by overallFadeTimeMS
- }
- return delayMS;
- }
- /*
- Purpose: prepares variables for colour fading
- Parameters: fadeToColour [desired R/G/B colour]; fade time in microseconds
- Returns: nothing
- */
- void startFadeToColourSequence( int fadeToColour[], unsigned long fadeTimeMS ) {
- for (int i=0; i<3; i++) {
- valsRGB[i] = fadeToColour[i]; // set new values for R/G/B
- delayRGB[i] = calculateDelay( prevVals[i], valsRGB[i], fadeTimeMS ); // calculate time delay between PWM step changes for each colour
- stepsRGB[i] = prevVals[i]; // set starting value for PWM steps
- if ( valsRGB[i] > prevVals[i] ) { // calculate fade direction
- fadeDirRGB[i] = 1;
- } else {
- fadeDirRGB[i] = -1;
- }
- fadingRGB[i] = true;
- }
- }
- /*
- Purpose: performs PWM change
- Parameters: timestamp variale, array of delay timers
- Returns: nothing
- */
- void doPWMStep(unsigned long timeNowMS, unsigned long timerRGB[]) {
- for (int i=0; i<3; i++) {
- // Check to see if colour should be changed
- if ( ( (timeNowMS - timerRGB[i]) >= delayRGB[i] ) && ( fadingRGB[i] == true ) ) {
- #ifdef DEBUG_MODE
- Serial.print("timeNowMS = ");
- Serial.print(timeNowMS);
- for (int i=0; i<3; i++) {
- Serial.print(", timerRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(timerRGB[i]);
- Serial.print(", stepsRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(stepsRGB[i]);
- }
- Serial.println("");
- #endif
- if ( stepsRGB[i] != valsRGB[i] ) {
- writeLED( pinsRGB[i], stepsRGB[i] );
- stepsRGB[i] += fadeDirRGB[i];
- timerRGB[i] = timeNowMS; // set wait timer for this colour
- } else {
- // Done fading this colour
- fadingRGB[i] = false;
- }
- }
- }
- }
- /*
- Purpose: performs update on PWM pin
- Parameters: pin [one of R/G/B]; val [incremented or decremented PWM value]
- Returns: nothing
- */
- void writeLED(int pin, int val) {
- uint8_t gammaCorr; // Use Gamma correction table
- if ( isCommonAnode == true ) {
- gammaCorr = pgm_read_byte(&gamma8[255-val]);
- } else {
- gammaCorr = pgm_read_byte(&gamma8[val]);
- }
- #ifdef DEBUG_MODE
- Serial.print("pin = ");
- Serial.print(pin);
- Serial.print(", val = ");
- Serial.print(val);
- Serial.print(", gammaCorr = ");
- Serial.println(gammaCorr);
- #endif
- analogWrite(pin, gammaCorr);
- }
- void debugPrintValues() {
- #ifdef DEBUG_MODE
- for (int i=0; i<3; i++) {
- Serial.print("valsRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(valsRGB[i]);
- Serial.print(", delayRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(delayRGB[i]);
- Serial.print(", stepsRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(stepsRGB[i]);
- Serial.print(", fadeDirRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.print(fadeDirRGB[i]);
- Serial.print(", fadingRGB[");
- Serial.print(i);
- Serial.print("] = ");
- Serial.println(fadingRGB[i]);
- }
- #endif
- }
- /*
- 8-bit Gamma correction lookup table (uses gamma correction factor of 2.8)
- Table generated using Processing app (Processing_Gamma_Correction_Table_Generator.pde)
- */
- const uint8_t PROGMEM gamma8[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
- 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
- 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
- 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
- 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
- 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
- 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
- 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
- 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
- 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
- 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
- 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
- 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
- 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement