JackOLantern_CircuitPlayground.ino
 1  // SPDX-FileCopyrightText: 2018 Phillip Burgess for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  // Jack-o-Lantern sketch for Adafruit Circuit Playground (Classic or Express)
 6  
 7  #include "Adafruit_CircuitPlayground.h"
 8  
 9  void setup() {
10    CircuitPlayground.begin();
11    CircuitPlayground.setBrightness(255); // LEDs full blast!
12  }
13  
14  uint8_t prev = 128;              // Start brightness in middle
15  
16  void loop() {
17    uint8_t lvl = random(64, 192); // End brightness at 128±64
18    split(prev, lvl, 32);          // Start subdividing, ±32 at midpoint
19    prev = lvl;                    // Assign end brightness to next start
20  }
21  
22  void split(uint8_t y1, uint8_t y2, uint8_t offset) {
23    if(offset) { // Split further into sub-segments w/midpoint at ±offset
24      uint8_t mid = (y1 + y2 + 1) / 2 + random(-offset, offset);
25      split(y1 , mid, offset / 2); // First segment (offset is halved)
26      split(mid, y2 , offset / 2); // Second segment (ditto)
27    } else { // No further subdivision - y1 determines LED brightness
28      uint32_t c = (((int)(pow((float)y1 / 255.0, 2.7) * 255.0 + 0.5) // Gamma
29                   * 0x1004004) >> 8) & 0xFF3F03; // Expand to 32-bit RGB color
30      for(uint8_t i=0; i<10; i++) CircuitPlayground.strip.setPixelColor(i, c);
31      CircuitPlayground.strip.show();
32      delay(4);
33    }
34  }