I love a good bargain, but it has to be said that they don't always love me.
In this case I have a couple of non-working cheap AliExpress remotes for our driveway solar powered farm gate.
A LOT of faffing about and testing and looking in obscure parts of the internet finally paid off though and I was able to bring back up to speed both the copy/learning remotes (HFY528DK V2.5) and as well the permanently programmed HFY528-8KEY Ver.2.2 EV1527 remotes.
It's a bit of journey and includes a lot of farm animals that had some strong (and loud) opinions - notwithstanding the million Aussie Flies making their own unique contribution!
Enjoy the video and feel free to use the timestamps provided if you'd like to skip the farm nonsense.
Another small break in uploading (what is it with 2026?), but after our house turned into a building site for a few weeks, filming/editing etc became a bit difficult!
But now I'm back with a look at the ArtCraft suite of apps (specifically FilmCraft) and as well using some leftover LED strip and diffuser housing becomes maybe a WOPR computer or a Cylon (or both).
Rather than a traditional ESP32 and WLED code for the WS2812, I've gone with a CH32V003.
/* ============================== CH32V003J4M6 WOPR SOLAR LIGHT ============================== Target Device : CH32V003J4M6 (SOP-8) Framework : ch32v003fun (https://github.com/cnlohr/ch32fun) Datasheet : https://www.wch-ic.com/downloads/CH32V003DS0_PDF.html Ref Manual : https://www.wch-ic.com/downloads/CH32V003RM_PDF.html What started as a simple solar light with leftover building items has transformed into some mad WOPR implementation. A WS2812 strip blinks and fades randomly to simulate the famous computer from the 1983 movie WarGames, periodically pausing for a classic Battlestar Galactica Cylon red eye scan. Pin Assignments (SOP-8): - Pin 3 (PA2) : Floating ADC entropy seed (later: solar voltage divider) - Pin 5 (PC4) : LED strip Data Out (shared with PC1/PC2 on the SOP-8!) =============================================================================*/// cnlohr's single-header WS2812B driver pulls its implementation here// Source: https://github.com/cnlohr/ch32fun/blob/master/extralibs/ws2812b_simple.h#define WS2812BSIMPLE_IMPLEMENTATION#include"ch32fun.h" // Core CH32V003 register definitions & startup glue#include"ws2812b_simple.h" // Cycle-accurate bit-banged NeoPixel driver from ch32fun#include<string.h> #define NUM_LEDS 17 // weird number but it matches the diffuser length#define LED_PORT GPIOC // Port C holds our data pin#define LED_PIN 4 // PC4 -> physical Pin 5 on the 8-pin CH32V003J4M6#define TICK_MS 25 // Heartbeat of the whole show: 25 ms per tick (40 ticks/sec)#define FADE_STEPS 5 // e.g. 5 steps x 25 ms = 125 ms smooth cross-fade// WOPR panel density and timing knobs#define DARK_CHANCE_PCT 75 // 75% chance an LED goes dark (keeps ~5-6 lit so it breathes)#define LIT_MIN_TICKS 10 // shortest lit blip#define LIT_MAX_TICKS 100 // longest lit hold#define DARK_MIN_TICKS 10 // shortest dark pause#define DARK_MAX_TICKS 100 // longest dark pause// Periodic full-strip WOPR blackout tuning (40 ticks = 1.0 s)// Simulates those ominous moments when "Joshua" stops to think...#define BLACKOUT_MIN_WAIT 480 // 480 x 25 ms = 12 s (~5 blackouts/min at most)#define BLACKOUT_MAX_WAIT 2400 // (~1 blackout/min at least)#define BLACKOUT_MIN_DUR 20 // 20 x 25 ms = 0.5 s quick power dip#define BLACKOUT_MAX_DUR 100 // 100 x 25 ms = 2.5 s dramatic pause// Periodic Cylon eye sequence tuning (40 ticks = 1.0 s, 2400 ticks = 1 min)// "By your command..." - https://en.wikipedia.org/wiki/Cylons#define CYLON_MIN_WAIT 4800 // 4800 x 25 ms = 120 s (2 minutes minimum between scans)#define CYLON_MAX_WAIT 12000 // 12000 x 25 ms = 300 s (5 minutes maximum between scans)#define CYLON_BLACKOUT_TICKS 120 // 120 x 25 ms = 3.0 s pitch-black before the "eye" wakes#define CYLON_STEP_TICKS 2 // 2 x 25 ms = 50 ms per pixel (~1.6 s per round-trip sweep)#define CYLON_PASSES 5 // 5 full back-and-forth sweeps before returning to WOPR// Brightness ceiling (0-255). 64 is fine inside a diffuser, keeping peak current under ~150 mA#define MAX_BRIGHT 64// Top-level state machine modestypedefenum{
MODE_WOPR=0,// Normal asynchronous WarGames console blinkenlightsMODE_BLACKOUT,// Brief random full-panel dark pauseMODE_CYLON_PAUSE,// 3-second dramatic blackout building up to Cylon modeMODE_CYLON_SCAN// Red eye sweeping back and forth with a fading tail
}display_mode_t;
// Every single one of the 17 LEDs gets its own private state struct (11 bytes each).// Total RAM for all 17 LEDs = 187 bytes out of the CH32V003's 2,048 bytes of SRAM!typedefstruct{
uint8_tcur_r,cur_g,cur_b;// What is actually glowing on the LED right nowuint8_tstart_r,start_g,start_b;// Where the color started when a fade beganuint8_ttarget_r,target_g,target_b;// Where the color is headinguint8_tfade_rem;// 25 ms steps left in current fade (0 = steady/snapped)uint8_thold_ticks;// 25 ms ticks left to hold steady before picking again
}led_state_t;
led_state_tleds[NUM_LEDS];// Our 17 independent pixel state machinesuint8_tled_buffer[NUM_LEDS*3];// Raw GRB byte array clocked out to the WS2812B strip// 1960s Mainframe / WOPR console color palette (scaled by MAX_BRIGHT)// Pure random RGB looks like a party store; but warm tungsten, amber, red, and// occasional phosphor green looks straight off the movie set.constuint8_tpalette[][3]={
{MAX_BRIGHT,MAX_BRIGHT/3,0},// Warm Tungsten Amber{MAX_BRIGHT,MAX_BRIGHT/2,MAX_BRIGHT/12},// Incandescent Panel Glow (hint of blue warms the yellow){MAX_BRIGHT,MAX_BRIGHT/5,0},// Deep Neon Orange{MAX_BRIGHT,0,0},// Alert / DEFCON Red{MAX_BRIGHT*3/4,0,0},// Dim Status Red{0,MAX_BRIGHT*3/4,MAX_BRIGHT/8}// Phosphor Indicator Green
};
#define PALETTE_SIZE (sizeof(palette) / sizeof(palette[0])) // Handy macro so we can add/remove colors freely// -----------------------------------------------------------------------------// Xoshiro128** PRNG Engine// Fast, rock-solid 32-bit pseudo-random generator by Blackman & Vigna.// Reference: https://prng.di.unimi.it/xoshiro128starstar.c// -----------------------------------------------------------------------------staticuint32_txos_s[4]={0x29012026,0x10141400,0xDEADBEEF,0xCAFEBABE};// Default seed (stirred at boot!)// Classic 32-bit bitwise rotate-left; GCC compiles this down to a neat RISC-V shift/or pairstaticinlineuint32_trotl(constuint32_tx,intk){
return(x<<k)|(x>>(32-k));
}
// Cranks the 128-bit state forward and spits out a fresh 32-bit random worduint32_txos_next(void){
constuint32_tresult=rotl(xos_s[1]*5,7)*9;// Scrambler function (the "**" in Xoshiro128**)constuint32_tt=xos_s[1]<<9;// Save shifted word 1 before mixingxos_s[2]^=xos_s[0];// Linear engine state transitions...xos_s[3]^=xos_s[1];
xos_s[1]^=xos_s[2];
xos_s[0]^=xos_s[3];
xos_s[2]^=t;// Fold saved shift back inxos_s[3]=rotl(xos_s[3],11);// Final bit rotationreturnresult;
}
// The old function, it returns a random integer from `small` up to `big`uint16_tgimmerand(uint16_tsmall,uint16_tbig){
if(small>=big)returnsmall;// Guard against swapped or equal boundsuint32_tresult=xos_next();// Grab 32 fresh random bitsuint16_trange=big-small+1;// +1 makes the upper bound inclusivereturnsmall+(uint16_t)(result%range);// Map into [small .. big]
}
// -----------------------------------------------------------------------------// Hardware Entropy Seeding via Floating ADC Pin// Unlike the Padauk PFS154, the CH32V003 has a 10-bit ADC Reading an// unconnected pin picks up thermal and ambient EMI noise so every power-up// starts a genuinely different lightshow instead of repeating the same pattern.// -----------------------------------------------------------------------------voidseed_rng_from_adc(void){
RCC->APB2PCENR|=RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1;// Wake up GPIOA and ADC1 peripheral clocksRCC->CFGR0&=~(0x1F<<11);// Clear existing ADC clock prescaler bitsRCC->CFGR0|=RCC_ADCPRE_DIV8;// Run ADC clock at 48 MHz / 8 = 6 MHz (well within 14 MHz max)// Set PA2 (ADC Channel A0, Pin 3 on SOP-8) to floating analog input (CNF=00, MODE=00)GPIOA->CFGLR&=~(0xF<<(2*4));
ADC1->CTLR2=ADC_ADON|ADC_EXTSEL|ADC_EXTTRIG;// Power on ADC1 and arm software-triggered conversionADC1->RSQR1=0;// Only 1 channel in our conversion sequenceADC1->RSQR3=0;// Select Channel 0 (PA2)ADC1->SAMPTR2=0x07;// Max sample window (241.5 cycles) so the floating pin soaks up plenty of noiseADC1->CTLR2|=ADC_RSTCAL;// Reset internal ADC calibration registers...while(ADC1->CTLR2&ADC_RSTCAL);// ...and wait until reset finishesADC1->CTLR2|=ADC_CAL;// Run self-calibration...while(ADC1->CTLR2&ADC_CAL);// ...and wait until calibration completes// Take 16 readings and XOR the noisy lowest bits across all four Xoshiro state wordsfor(uint8_ti=0;i<16;i++){
ADC1->CTLR2|=ADC_SWSTART;// Kick off a single conversionwhile(!(ADC1->STATR&ADC_EOC));// Spin until End-Of-Conversion flag pops upuint16_tsample=ADC1->RDATAR;// Grab the 10-bit result (clears EOC automatically)xos_s[i&0x03]^=((uint32_t)(sample&0x03)<<(i*2))^(sample<<7);// Stir noise into xos_s[0..3]}
// Shut down ADC1 completely so it doesn't run down batteryADC1->CTLR2&=~ADC_ADON;
RCC->APB2PCENR&=~RCC_APB2Periph_ADC1;
// Xoshiro128** must never have an all-zero state; restore fallback seed if that ever happenedif((xos_s[0]|xos_s[1]|xos_s[2]|xos_s[3])==0){
xos_s[0]=0x29012026;
xos_s[1]=0x10141400;
xos_s[2]=0xDEADBEEF;
xos_s[3]=0xCAFEBABE;
}
// Repeat the generator 8 times to thoroughly diffuse the newly mixed seed bitsfor(uint8_ti=0;i<8;i++){
xos_next();
}
}
// -----------------------------------------------------------------------------// Picks the next state for a single LED:// 1. Decides whether it goes dark (68%) or lights up in a console color (32%)// 2. Picks a random hold duration// 3. Flips a 50/50 coin between a crisp relay-style snap or a 125 ms fade// Returns 1 if it snapped immediately (meaning the strip needs an update now),// or 0 if it's starting a multi-step fade.// -----------------------------------------------------------------------------uint8_tstart_led_transition(uint8_tidx){
uint8_troll=(uint8_t)gimmerand(0,99);// Roll 0..99 percentile diceif(roll<DARK_CHANCE_PCT){
// Heading to darkleds[idx].target_r=0;
leds[idx].target_g=0;
leds[idx].target_b=0;
leds[idx].hold_ticks=(uint8_t)gimmerand(DARK_MIN_TICKS,DARK_MAX_TICKS);
}else{
// Heading to a random color from our 1960s mainframe paletteuint8_tc=(uint8_t)gimmerand(0,PALETTE_SIZE-1);
leds[idx].target_r=palette[c][0];
leds[idx].target_g=palette[c][1];
leds[idx].target_b=palette[c][2];
leds[idx].hold_ticks=(uint8_t)gimmerand(LIT_MIN_TICKS,LIT_MAX_TICKS);
}
// 50/50 coin flip: 1 = smooth 125 ms filament fade, 0 = instant mechanical clickif(gimmerand(0,1)){
leds[idx].start_r=leds[idx].cur_r;// Snapshot where we're fading FROMleds[idx].start_g=leds[idx].cur_g;
leds[idx].start_b=leds[idx].cur_b;
leds[idx].fade_rem=FADE_STEPS;// Arm the 5-step countdownreturn0;// First fade step happens on the next 25 ms tick}else{
leds[idx].cur_r=leds[idx].target_r;// Jump straight to the destination colorleds[idx].cur_g=leds[idx].target_g;
leds[idx].cur_b=leds[idx].target_b;
leds[idx].fade_rem=0;// No fade neededreturn1;// Tell main loop the strip buffer changed}
}
// Packs our RGB structs into WS2812B's GRB byte order and sends it out to PC4voidpush_leds(void){
for(inti=0;i<NUM_LEDS;i++){
led_buffer[i*3+0]=leds[i].cur_g;// Green goes first on WS2812B!led_buffer[i*3+1]=leds[i].cur_r;// Red secondled_buffer[i*3+2]=leds[i].cur_b;// Blue third}
WS2812BSimpleSend(LED_PORT,LED_PIN,led_buffer,sizeof(led_buffer));
}
// Blanks the entire strip immediately (used for blackouts and Cylon build-up)voidclear_strip_now(void){
memset(led_buffer,0,sizeof(led_buffer));
WS2812BSimpleSend(LED_PORT,LED_PIN,led_buffer,sizeof(led_buffer));
}
// Resets all 17 pixels from dark into staggered random WOPR statesvoidinit_wopr_leds(void){
for(uint8_ti=0;i<NUM_LEDS;i++){
leds[i].cur_r=0;
leds[i].cur_g=0;
leds[i].cur_b=0;
start_led_transition(i);// Some will snap on right away, others will fade up}
push_leds();
}
// =============================================================================// Main Loop// =============================================================================intmain(void){
SystemInit();// Bring the CH32V003 up to 48 MHz (required for FUNCONF_SYSTICK_USE_HCLK 1 in funconfig.h)seed_rng_from_adc();// Harvest boot-time analog noise before enabling Port CRCC->APB2PCENR|=RCC_APB2Periph_GPIOC;// Turn on the clock for Port C (our data pin)init_wopr_leds();// Light up the initial WOPR panel statedisplay_mode_tmode=MODE_WOPR;// Start in normal WarGames computer modeuint16_tblackout_timer=gimmerand(BLACKOUT_MIN_WAIT,BLACKOUT_MAX_WAIT);// Schedule first brief blackoutuint16_tcylon_timer=gimmerand(CYLON_MIN_WAIT,CYLON_MAX_WAIT);// Schedule first Cylon scan// State variables for the Cylon red-eye sweepuint8_tcylon_step_tick=0;// Sub-timer pacing the eye movement (every 50 ms)int8_tcylon_pos=0;// Signed index (0 .. NUM_LEDS-1) so bouncing math is safeint8_tcylon_dir=1;// +1 sweeping right, -1 sweeping leftuint8_tcylon_passes=0;// Counts completed round tripswhile(1){
Delay_Ms(TICK_MS);// Pace the whole engine to an exact 25 ms heartbeat// ---------------------------------------------------------------------// State 1: Brief WOPR Blackout// ---------------------------------------------------------------------if(mode==MODE_BLACKOUT){
if(--blackout_timer==0){
mode=MODE_WOPR;// Back to work, Joshuablackout_timer=gimmerand(BLACKOUT_MIN_WAIT,BLACKOUT_MAX_WAIT);
init_wopr_leds();// Wake up into a fresh random configuration}
continue;
}
// ---------------------------------------------------------------------// State 2: 3-Second Suspense Pause Before Cylon Eye Wakes Up// ---------------------------------------------------------------------if(mode==MODE_CYLON_PAUSE){
if(--cylon_timer==0){
mode=MODE_CYLON_SCAN;
cylon_step_tick=1;// Trigger the very first LED on the next loop passcylon_pos=0;// Start at the left end of the diffusercylon_dir=1;// Sweep rightwardcylon_passes=0;
memset(led_buffer,0,sizeof(led_buffer));// Ensure a clean black canvas}
continue;
}
// ---------------------------------------------------------------------// State 3: Cylon Red Eye Scan with Phosphor Decay Trail// ---------------------------------------------------------------------if(mode==MODE_CYLON_SCAN){
if(--cylon_step_tick==0){
cylon_step_tick=CYLON_STEP_TICKS;// Reload 50 ms step timer// Decay every pixel's red channel by half (>> 1) to leave a glowing tailfor(uint8_ti=0;i<NUM_LEDS;i++){
led_buffer[i*3+0]=0;// No greenled_buffer[i*3+1]=led_buffer[i*3+1]>>1;// Halve previous red brightnessled_buffer[i*3+2]=0;// No blue}
// Stamp the bright red Cylon eye at its current spot and update stripled_buffer[cylon_pos*3+1]=MAX_BRIGHT;
WS2812BSimpleSend(LED_PORT,LED_PIN,led_buffer,sizeof(led_buffer));
// Move the eye one pixel and bounce cleanly off both endscylon_pos+=cylon_dir;
if(cylon_pos>=(NUM_LEDS-1)){
cylon_pos=NUM_LEDS-1;
cylon_dir=-1;// Hit right end -> head back left}elseif(cylon_pos<=0){
cylon_pos=0;
cylon_dir=1;// Returned to left end -> one full round trip complete!cylon_passes++;
if(cylon_passes>=CYLON_PASSES){
// Done scanning: return to normal WOPR blinkenlightsmode=MODE_WOPR;
cylon_timer=gimmerand(CYLON_MIN_WAIT,CYLON_MAX_WAIT);
blackout_timer=gimmerand(BLACKOUT_MIN_WAIT,BLACKOUT_MAX_WAIT);
init_wopr_leds();
}
}
}
continue;
}
// ---------------------------------------------------------------------// State 4: Normal Asynchronous WOPR Console Operation// ---------------------------------------------------------------------// Check Cylon trigger first (every 2 to 5 minutes)if(--cylon_timer==0){
mode=MODE_CYLON_PAUSE;
cylon_timer=CYLON_BLACKOUT_TICKS;// 3.0 s blackout before the red eye appearsclear_strip_now();
continue;
}
// Check regular random WOPR blackout triggerif(--blackout_timer==0){
mode=MODE_BLACKOUT;
blackout_timer=gimmerand(BLACKOUT_MIN_DUR,BLACKOUT_MAX_DUR);
clear_strip_now();
continue;
}
// Tick all 17 independent LEDs; only talk to the WS2812B if something moveduint8_tmoved=0;
for(uint8_ti=0;i<NUM_LEDS;i++){
if(leds[i].fade_rem>0){
// Mid-fade: step linearly from start_r/g/b toward target_r/g/bleds[i].fade_rem--;
uint8_tstep=FADE_STEPS-leds[i].fade_rem;// Counts 1, 2, 3, 4, 5leds[i].cur_r=leds[i].start_r+((int16_t)(leds[i].target_r-leds[i].start_r)*step)/FADE_STEPS;
leds[i].cur_g=leds[i].start_g+((int16_t)(leds[i].target_g-leds[i].start_g)*step)/FADE_STEPS;
leds[i].cur_b=leds[i].start_b+((int16_t)(leds[i].target_b-leds[i].start_b)*step)/FADE_STEPS;
moved=1;// Strip needs refreshing on this tick}else{
// Holding steady: count down until it's time to pick a new stateif(leds[i].hold_ticks>0){
leds[i].hold_ticks--;
}
if(leds[i].hold_ticks==0){
moved|=start_led_transition(i);// Returns 1 if snapped, 0 if starting a fade}
}
}
// Send out the 51-byte GRB frame when at least one pixel actually changedif(moved){
push_leds();
}
}
}
This was a (ch32)fun project and hopefully a fun video for you.
In a previous video we looked at retiring the QX5252 workhorse of the candle project in favour of a boost converter (a modified BL8530)
The problem with measuring the light (and hence determining if it is day or night) using a comparator on the PFS154 is that the solar panel voltage dips alarmingly when the BL8530 is drawing current.
Net result? The PFS154 keeps making the wrong decision re nightfall - it mistakes a drop in voltage due to the hungry supercap/boost converter duo for a drop in voltage due to the sun disappearing for the day.
What to do?
Well I went up to the ideas factory with my furry partners, and drew the following possible solution...
...then we went to the lab and tested some switching code...
/* MOSFET Bench Test / Blink Example for PFS154 Drives PA7 HIGH/LOW to switch a low-side N-Channel MOSFET (BS170 or AO3400). Connections: - PFS154 Pin 2 (PA7) -> MOSFET Gate (Pin 2) - MOSFET Drain -> LED Cathode (-) (LED Anode + goes through 330R to VCC) - MOSFET Source -> System GND - 100k Resistor -> Between PA7 (Gate) and System GND*/#include<pdk/device.h>#include"auto_sysclock.h"#include"startup.h"#include"delay.h"// Hardware bitmask for PA7 (Bit 7 / Pin 2)#define MOSFET_GATE_BIT 0b10000000// Helper macros for N-Channel MOSFET control#define turnMosfetOn() PA |= MOSFET_GATE_BIT // Drive PA7 HIGH -> MOSFET ON#define turnMosfetOff() PA &= ~MOSFET_GATE_BIT // Drive PA7 LOW -> MOSFET OFFvoidmydelay(uint16_tms){
while(ms>0){
_delay_ms(1);
ms--;
}
}
voidmain(void){
// Set PA7 as Output ModePAC|=MOSFET_GATE_BIT;PAPH&=~MOSFET_GATE_BIT;// Disable internal pull-upturnMosfetOff();// Ensure MOSFET starts OFFwhile(1){
turnMosfetOn();// PA7 HIGH -> MOSFET ON -> LED ONmydelay(4500);
turnMosfetOff();// PA7 LOW -> MOSFET OFF -> LED OFFmydelay(4500);
}
}
unsignedcharSTARTUP_FUNCTION(void){
AUTO_INIT_SYSCLOCK();
AUTO_CALIBRATE_SYSCLOCK(TARGET_VDD_MV);
return0;
}
...and finally we made the following video to explain the possible update to the candle project.
Also, please stay to the end of the video for an announcement of what might be the next iteration in this project.
For years I have been working on the candle project - the idea of an environmentally friendly perpetual "candle", solar powered, that does justice to the gentle flickering of a real candle, including sputtering, has driven a lot of content on this channel!
The only unease I have felt is in using NiMH batteries for solar power storage. Heavy and having a fairly low power cycle count, the main issue was environmental - some nasty chemicals and a not particularly long life meant too many would end up in landfill.
Then in the super capacitor realm we had 120F and 250F LIC capacitors that charged to 3.8V, has a cycle count between 100000 and 1000000 charge/discharge cycles, was light weight and seemed to contain a number of problematic barriers:
3. Boost/control the output of the solar panel to charge a 3.8V super capacitor safely
This third hurdle bothered me A LOT because the QX5252 was 80% efficient on a good day and I was using a 3.9V zener clamp on the output which further wasted power.
I needed a good DC-DC boost converter that could step into it's shoes (after all there must be tens of videos on this channel that feature this little soldier).
And so the search began - with some parameters to help me weed out contenders. AI was not much help (a bit too "chicken Little" for my taste)
I am really happy with the TPS6102X family (featured before on this channel), but it is too expensive at the moment, and so with cost in mind I compromised to the BL8530/1N5817 combo.
Please take a look at the video and let me know what you think - especially if you have another contender!