/ Logans_Run_Hand_Jewel_LED / Logans_Run_Hand_Jewel_LED.ino
Logans_Run_Hand_Jewel_LED.ino
1 // SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #ifdef __AVR__ 6 #include <avr/power.h> 7 #endif 8 9 #include <Adafruit_NeoPixel.h> 10 11 #define PIXEL_PIN 1 // Pin connected to neo pixels 12 #define PIXEL_COUNT 7 // Count of neo pixels 13 14 Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); 15 16 void setup() { 17 18 strip.begin(); 19 strip.show(); 20 21 } 22 23 // Linear interpolation of y value given min/max x, min/max y, and x position. 24 float lerp(float x, float x0, float x1, float y0, float y1) 25 { 26 // Clamp x within x0 and x1 bounds. 27 x = x > x1 ? x1 : x; 28 x = x < x0 ? x0 : x; 29 // Calculate linear interpolation of y value. 30 return y0 + (y1-y0)*((x-x0)/(x1-x0)); 31 } 32 33 // Set all pixels to the specified color. 34 void fill_pixels(Adafruit_NeoPixel& pixels, uint32_t color) 35 { 36 for (int i=0; i < pixels.numPixels(); ++i) { 37 pixels.setPixelColor(i, color); 38 } 39 strip.show(); 40 } 41 42 // Get the color of a pixel within a smooth gradient of two colors. 43 uint32_t color_gradient(uint8_t start_r, // Starting R,G,B color 44 uint8_t start_g, 45 uint8_t start_b, 46 uint8_t end_r, // Ending R,G,B color 47 uint8_t end_g, 48 uint8_t end_b, 49 float pos) // Position along gradient, should be a value 0 to 1.0 50 { 51 // Interpolate R,G,B values and return them as a color. 52 uint8_t red = (uint8_t) lerp(pos, 0.0, 1.0, start_r, end_r); 53 uint8_t green = (uint8_t) lerp(pos, 0.0, 1.0, start_g, end_g); 54 uint8_t blue = (uint8_t) lerp(pos, 0.0, 1.0, start_b, end_b); 55 return Adafruit_NeoPixel::Color(red, green, blue); 56 } 57 58 void animate_gradient_fill(Adafruit_NeoPixel& pixels, // NeoPixel strip/loop/etc. 59 uint8_t start_r, // Starting R,G,B color 60 uint8_t start_g, 61 uint8_t start_b, 62 uint8_t end_r, // Ending R,G,B color 63 uint8_t end_g, 64 uint8_t end_b, 65 int duration_ms) // Total duration of animation, in milliseconds 66 { 67 unsigned long start = millis(); 68 // Display start color. 69 fill_pixels(pixels, Adafruit_NeoPixel::Color(start_r, start_g, start_b)); 70 // Main animation loop. 71 unsigned long delta = millis() - start; 72 while (delta < duration_ms) { 73 // Calculate how far along we are in the duration as a position 0...1.0 74 float pos = (float)delta / (float)duration_ms; 75 // Get the gradient color and fill all the pixels with it. 76 uint32_t color = color_gradient(start_r, start_g, start_b, end_r, end_g, end_b, pos); 77 fill_pixels(pixels, color); 78 // Update delta and repeat. 79 delta = millis() - start; 80 } 81 // Display end color. 82 fill_pixels(pixels, Adafruit_NeoPixel::Color(end_r, end_g, end_b)); 83 } 84 void loop() { 85 86 // Run It: 87 88 // Nice fade from dim red to full red for 1/2 of a second: 89 animate_gradient_fill(strip, 90 10, 0, 0, 91 255, 0, 0, 92 500); 93 // Then fade from full red to dim red for 1/2 a second. 94 animate_gradient_fill(strip, 95 255, 0, 0, 96 10, 0, 0, 97 500); 98 //delay(1000); Use this delay if using multiple color fades 99 }