/* NanoNeoPixeLDRSelfCal.INO Todd Andersen 05-NOV-18 Updated for Pinball News 30-SEP-22 */ // Based on Random Flash Animation // Random Flash Animation for Neopixel Ring Bangle Bracelet // by Dano Wall and Becky Stern for Adafruit Industries // In turn based on the Sparkle Skirt, minus the accelerometer // https://learn.adafruit.com/neopixel-ring-bangle-bracelet/code #include #define PIXELS 4 // Count of Neopixels on board / strip / ring #define PIN 1 // Arduino OUT pin for Din pin on Neopixels const int LRDPin = A1; // Sensor pin = LRD, A1 for Digispark PB2 pin const int threshold = 53; // Calibrated and mapped sensor trip point for LED display (10 to 100) // Smaller = More sensative, Larger = Less sensative const int calTime = 3000; // Time allowed to calibrate trip point (2000 to 3000) int sensorValue = 0; // Variable to store the value coming from the sensor int sensorMin = 1023; // Variable to store sensor calibration mimimum int sensorMax= 0; // Variable to store sensor calibration maximum int stepMin = 5; // Smaller = Fewer and Larger = Greater int stepsMax = 20; // Smaller = Fewer and Larger = Greater int steps = (random(stepMin, stepsMax)); // Set pseudorandom step fade in / out range int twinkMin = 5; // Smaller = Shorter and Larger = Longer int twinkMax = 20; // Smaller = Shorter and Larger = Longer int twinkle = (random(twinkMin, twinkMax)); // Set pseudorandom twinkle time int pixelMin = 1; // Smaller = Less and Larger = More int pixelMax = 4; // Smaller = Less and Larger = More int numPixels = (random(pixelMin, pixelMax)); // Set pseudorandom twinkling number of pixels // Parameter 1 = number of pixels (PIXELS) // Parameter 2 = pin number (PIN, most are valid) // Parameter 3 = pixel type flags, add together as needed: // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_RGB); // Here is where you can put in your favorite colors that will appear! // just add new {nnn, nnn, nnn}, lines. They will be picked out randomly // R G B // Use NEO_RGB for NeoLEDs uint8_t myColors[][3] = { {232, 100, 255}, // purple {230, 100, 020}, // yellow-red {255, 000, 000}, // red {255, 010, 100}, // off-red }; // Don't edit the line below! #define FAVCOLORS sizeof (myColors) / 3 void setup() { strip.begin(); // Initiate Neopixel library strip.setBrightness(255); // Range = 000 through 255, 40 = Default, strip.show(); // Initialize all pixels OFF while (millis() < (calTime)){ // Calibrate during calTime sensorValue = analogRead (LRDPin); if (sensorValue > sensorMax){ // Record sensorMax sensorMax = sensorValue; } if (sensorValue < sensorMin){ // Record sensorMin sensorMin = sensorValue; } } } void loop() { sensorValue = analogRead (LRDPin); // Read LDR value sensorValue = map (sensorValue, sensorMin, sensorMax, 10, 100); // Map calibrated sensorValue sensorValue = constrain (sensorValue, 10, 100); // Constrain mapped sensorValue if ((sensorValue) > (threshold)){ // OFF if pinball machine OFF, GI lights = dark return; } flashRandom:(twinkle, numPixels); // First number = 'Wait' delay, Smaller = Shorter twinkle flashRandom (twinkle, numPixels); // Second number = Number of neopixels to light at one time flashRandom (twinkle, numPixels); } void flashRandom(int wait, uint8_t howmany) { for(uint16_t i=0; i= 0; x--) { int r = red * x; r /= steps; int g = green * x; g /= steps; int b = blue * x; b /= steps; // LEDs will be off when done (they are faded to 0) strip.setPixelColor(j, strip.Color(r, g, b)); strip.show(); delay(wait); } } }