Last active
October 6, 2025 16:27
-
-
Save AchimPieters/d8c33246e19c448d20e8ca1e1266d180 to your computer and use it in GitHub Desktop.
Halloween V2.1.0
This file contains hidden or 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
| /** | |
| Copyright 2025 Achim Pieters | StudioPieters® | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| For more information visit https://www.studiopieters.nl | |
| ATtiny85 – Blue Wizard Fire (HP Cinematic) | |
| 5x neon-blue LEDs on PB0..PB4 (D0..D4), NOT on RESET/PB5. | |
| Core: ATTinyCore (Spence Konde). No external libraries. | |
| What you get: | |
| - Deep blue flame as the base (organic) | |
| - Crackle (sparking crackles) | |
| - Whoosh (fast flare-up with afterglow) | |
| - SpellBurst (staccato pulses marching across the LEDs) | |
| - PatronusWave (center-out wave, clearly visible) | |
| - Global swell (musical swell) | |
| Everything is non-blocking and layers nicely. | |
| **/ | |
| const bool ACTIVE_HIGH = true; // LED to GND -> true, to VCC -> false | |
| const uint8_t LED_PINS[] = {0,1,2,3,4}; // D0..D4 = PB0..PB4 | |
| const uint8_t NUM_LEDS = sizeof(LED_PINS); | |
| // ====== “Cinematic” settings (tweak here) ====== | |
| uint8_t MAX_BRIGHT = 230; // go big! blue is bright — set 255 if desired | |
| uint8_t MIN_BRIGHT = 6; // never completely off | |
| uint8_t INTENSITY = 100; // 60..140 – global multiplier in % | |
| const uint8_t LOOP_MS = 10; // faster cadence | |
| const uint8_t GAMMA2 = 1; // 1 = gamma~2 enabled, 0 = off | |
| // ===== Flame “personality” ===== | |
| struct Flame { | |
| uint16_t t; | |
| uint8_t speed; | |
| uint8_t base; | |
| uint8_t range; | |
| uint8_t seed; | |
| } flames[NUM_LEDS]; | |
| // ===== RNG ===== | |
| static uint16_t xr = 0xA1C3; | |
| uint16_t r16(){ | |
| uint16_t x=xr; x^=x<<7; x^=x>>9; x^=x<<8; return xr=x; | |
| } | |
| uint8_t r8(uint8_t a,uint8_t b){ | |
| return a + (uint8_t)(r16() % (uint16_t)(b-a+1)); | |
| } | |
| // ===== Smooth value-noise 0..255 ===== | |
| uint8_t noise(uint16_t t, uint8_t s){ | |
| uint8_t x0 = (uint8_t)(t>>8), x1 = x0+1; | |
| auto h=[](uint8_t x,uint8_t s) -> uint8_t { | |
| uint8_t v=x; v^=s*37U+17U; v=(v^61U)^(v>>3); v*=9U; v^=(v<<8); return v; | |
| }; | |
| uint16_t a=h(x0,s), b=h(x1,s); | |
| uint8_t f8 = (uint8_t)(t&0xFF); | |
| uint16_t f=f8, f2=(f*f)>>8, f3=(f2*f)>>8; | |
| uint16_t fade=((3*f2)>255 ? 255 : (3*f2)) - (2*f3); | |
| if((int16_t)fade<0) fade=0; if(fade>255) fade=255; | |
| return (uint8_t)(((256-fade)*a + fade*b)>>8); | |
| } | |
| inline uint8_t g2(uint8_t x){ | |
| uint16_t v=x; v=(v*v)>>8; return (uint8_t)v; | |
| } | |
| inline void ledWrite(uint8_t pin, uint8_t val){ | |
| if(!ACTIVE_HIGH) val=255-val; analogWrite(pin,val); | |
| } | |
| inline uint8_t clamp8(int16_t v){ | |
| if(v<0) return 0; if(v>255) return 255; return (uint8_t)v; | |
| } | |
| // ===== Overlays ===== | |
| uint8_t crackle[NUM_LEDS]; // fast crackle | |
| const uint8_t CRACKLE_DECAY = 24; | |
| const uint8_t CRACKLE_CHANCE = 35; // ~1/35 per tick per LED (quite often) | |
| uint8_t whoosh = 0; // global boost with slower decay | |
| const uint8_t WHOOSH_DECAY = 3; | |
| uint32_t nextWhooshAt=0; | |
| bool burstActive=false; | |
| uint8_t burstStep=0, burstRepeats=0; // staccato train | |
| uint32_t nextBurstAt=0; | |
| bool waveActive=false; | |
| int16_t wavePos=0; // 0..(NUM_LEDS-1)*256 | |
| int16_t waveSpeed=240; // fast | |
| uint8_t waveStrength=160; // highly visible | |
| bool swellActive=false; | |
| uint16_t swellPh=0, swellSpd=900; | |
| uint8_t swellAmt=80; | |
| uint32_t nextSwellAt=0; | |
| // ===== helpers for triggers ===== | |
| void scheduleWhoosh(uint32_t now){ | |
| nextWhooshAt = now + (400 + (r16()%1800)); | |
| } | |
| void scheduleBurst (uint32_t now){ | |
| nextBurstAt = now + (900 + (r16()%2500)); | |
| } | |
| void scheduleSwell (uint32_t now){ | |
| nextSwellAt = now + (700 + (r16()%2200)); | |
| } | |
| void startWhoosh(){ | |
| whoosh = r8(120, 200); | |
| } // strong flare-up | |
| void startBurst(){ | |
| burstActive=true; burstStep=0; burstRepeats=r8(2,4); | |
| } | |
| void startWave (){ | |
| waveActive=true; wavePos = (NUM_LEDS/2)*256; // start in the center | |
| waveSpeed = 200 + (r16()%200); | |
| waveStrength = r8(130, 200); | |
| } | |
| void startSwell(){ | |
| swellActive=true; swellPh=0; swellSpd = 700 + (r16()%1400); swellAmt=r8(60,120); | |
| } | |
| // ===== setup ===== | |
| void setup(){ | |
| for(uint8_t i=0; i<NUM_LEDS; i++) { pinMode(LED_PINS[i],OUTPUT); ledWrite(LED_PINS[i],0); } | |
| for(uint8_t i=0; i<NUM_LEDS; i++) { | |
| flames[i].t = r16(); | |
| flames[i].speed = r8(3,8); | |
| flames[i].base = r8(18,34); | |
| flames[i].range = r8(120,170); // greater dynamic range | |
| flames[i].seed = r8(1,250); | |
| crackle[i]=0; | |
| } | |
| uint32_t now=millis(); | |
| scheduleWhoosh(now); scheduleBurst(now); scheduleSwell(now); | |
| // start immediately with a Patronus wave so you get a “wow” right away | |
| startWave(); | |
| } | |
| // ===== loop ===== | |
| void loop(){ | |
| uint32_t now = millis(); | |
| // triggers | |
| if((int32_t)(now-nextWhooshAt)>=0) { startWhoosh(); scheduleWhoosh(now); } | |
| if((int32_t)(now-nextBurstAt )>=0) { startBurst(); scheduleBurst(now); } | |
| if((int32_t)(now-nextSwellAt)>=0) { startSwell(); scheduleSwell(now); } | |
| // a visible “Patronus” wave every now and then | |
| if(!waveActive && (r16() & 0x03FF)==0) startWave(); | |
| for(uint8_t i=0; i<NUM_LEDS; i++) { | |
| Flame &f = flames[i]; | |
| // — Base flame (two layers of noise, more contrast) — | |
| uint8_t n1 = noise(f.t, f.seed); | |
| uint8_t n2 = noise(f.t + 17000U, f.seed ^ 0x9B); | |
| uint16_t mix = ((uint16_t)n1*180 + (uint16_t)n2*76) >> 8; // 70/30 | |
| int16_t raw = f.base + ((uint16_t)f.range * mix >> 8); | |
| // — Crackle: frequent, short, and bright — | |
| if((r16() % CRACKLE_CHANCE) == 0) { | |
| uint8_t add = r8(120, 220); // really bright | |
| uint16_t c = crackle[i] + add; crackle[i] = (c>240 ? 240 : (uint8_t)c); | |
| } | |
| if(crackle[i]) { raw += crackle[i]; crackle[i] = (crackle[i]>CRACKLE_DECAY) ? (crackle[i]-CRACKLE_DECAY) : 0; } | |
| // — Whoosh (global flare-up) — | |
| if(whoosh) { raw += whoosh; whoosh = (whoosh>WHOOSH_DECAY) ? (whoosh-WHOOSH_DECAY) : 0; } | |
| // — SpellBurst: staccato pulse train that marches — | |
| if(burstActive) { | |
| // burstStep determines which LED gets the “hit”, then echoes | |
| uint8_t lead = (burstStep % NUM_LEDS); | |
| int8_t d = (int8_t)i - (int8_t)lead; | |
| int16_t boost = 0; | |
| if(d==0) boost = 200; // main hit | |
| else if(d==1 || d==-1) boost = 120; // direct echo | |
| else if(d==2 || d==-2) boost = 60; // trailing tail | |
| raw += boost; | |
| } | |
| // — PatronusWave: center-out wave with a wide lobe — | |
| if(waveActive) { | |
| int16_t here = (int16_t)i*256; | |
| int16_t d = here - wavePos; if(d<0) d=-d; | |
| int16_t width = 380; // width of the lobe | |
| if(d < width) { | |
| // bell-ish (triangle-ish) shape -> clearly visible | |
| raw += (int16_t)((uint32_t)(width - d) * waveStrength / width); | |
| } | |
| } | |
| // — Swell (slow musical swell) — | |
| if(swellActive) { | |
| uint8_t t8 = (uint8_t)(swellPh>>8); | |
| uint16_t t=t8, t2=(t*t)>>8, t3=(t2*t)>>8; | |
| uint16_t ease=((3*t2)>255 ? 255 : (3*t2)) - (2*t3); // 0..255 | |
| raw += (uint16_t)(swellAmt * ease) >> 8; | |
| } | |
| // global intensity + clamping | |
| raw = (raw * INTENSITY) / 100; | |
| if(raw > MAX_BRIGHT) raw = MAX_BRIGHT; | |
| if(raw < MIN_BRIGHT) raw = MIN_BRIGHT; | |
| uint8_t v = (GAMMA2 ? g2((uint8_t)raw) : (uint8_t)raw); | |
| ledWrite(LED_PINS[i], v); | |
| // advance time (slightly faster than before) | |
| f.t += (uint16_t)(f.speed * 7U); | |
| } | |
| // wave progress | |
| if(waveActive) { | |
| wavePos += waveSpeed; | |
| if(wavePos > (int16_t)((NUM_LEDS-1)*256 + 420)) waveActive=false; | |
| } | |
| // burst progress (staccato, fast) | |
| static uint32_t lastBurstStep=0; | |
| if(burstActive && (now - lastBurstStep) >= 55) { // 55 ms per step | |
| lastBurstStep++; // exact value isn’t important | |
| burstStep++; | |
| if(burstStep >= NUM_LEDS) { | |
| burstStep = 0; | |
| if(burstRepeats) burstRepeats--; | |
| else burstActive=false; | |
| } | |
| } | |
| // swell progress | |
| if(swellActive) { | |
| uint16_t prev = swellPh; swellPh += swellSpd; | |
| if(swellPh < prev) swellActive=false; // done after overflow | |
| } | |
| delay(LOOP_MS); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment