ATtiny84 Motion-Activated Acrylic LED Plate

by PerL13 in Design > Software

397 Views, 2 Favorites, 0 Comments

ATtiny84 Motion-Activated Acrylic LED Plate

I built this as a small battery-powered motion indicator for my dog's food/water station. The original idea was to create an underglow beneath a wooden plate, but during development I changed to a frosted acrylic plate with four WS2812 LEDs illuminating the acrylic from the edge.

This worked much better than expected. Instead of simply illuminating the area underneath, the light spreads through the acrylic and makes the plate itself glow.

There is also a practical purpose. I wanted a simple indication of whether my dog had visited the water/food bowl during the day, without using Wi-Fi, Home Assistant or any external logging system.

Update 5 October 2026

I added a OLED display to make it easier to count the number of activations and also to tell the battery level. See Step 2.

Update 14 October 2026

Found a bug that prevented the Attiny84 to go to sleep which caused the power consumption to be higher than expected. Now fixed and code is updated in step 2 .

How to Do It

Hardware

The controller is an ATtiny84. For this type of single-purpose battery device, an ESP32 would be unnecessary. The ATtiny can spend most of its time in power-down sleep and wake directly from the PIR sensor.

I selected the ATtiny84 rather than the ATtiny85 mainly because of the additional I/O capability. It also allowed the application connections and ISP programming interface to coexist without having to disconnect the application hardware during programming.

Motion is detected by an HC-SR505 PIR sensor, and four WS2812 RGB LEDs provide the illumination. The LEDs are mounted along the edge of the frosted acrylic, which distributes and mixes the light.

A BC327 PNP transistor switches the 5 V supply to the WS2812s. This is important because WS2812 LEDs still consume power when logically switched off. Before removing their power, the software sets the LEDs to black and pulls the data line LOW to prevent back-powering through the data input.

Power comes from a 2700 mAh 18650 Li-ion battery through an LX-LCBST charging/boost module. It provides the 5 V system supply and allows the battery to be charged through USB-C. The board indicates red while charging and blue when charging is complete.

The ATtiny84 is programmed using an Arduino Uno as ISP. The hardware and ISP connections can remain connected, with one important exception: there is a jumper in the 5 V supply from the LX-LCBST. This jumper must be removed during programming so that the Arduino Uno and LX-LCBST do not both drive the 5 V rail.

The detailed connections and pin assignments are shown in the schematic.

Software and activity counter

Normally, the ATtiny84 is in power-down sleep with the WS2812 supply completely switched off.

When the PIR detects movement, it generates a pin-change interrupt that wakes the ATtiny. The LED supply is switched on and the LEDs are used to display an activation counter from 00 to 99.

Each digit 0–9 has its own clearly distinguishable RGB color. The two digits of the counter are displayed sequentially for five seconds each, separated by a short dark period. This means the four LEDs can act as both illumination and a very simple two-digit display without adding a screen.

After displaying the counter, the LEDs fade to a low brightness. They remain dimly illuminated while the PIR is active and are switched completely off when the PIR returns LOW. The ATtiny then goes back to sleep.

The counter is deliberately stored only in RAM and therefore resets to 00 when the device is power-cycled. The intention isn't accurate logging but simply to check the counter in the morning and later in the day to see how much activity has occurred around the bowl.

I use Arduino IDE with ATTinyCore by Spece Konde. Pinning for using Arduino Uno as ISP is:

  1. Arduino Uno 5 V → ATtiny84 pin 1 (VCC)
  2. Arduino Uno GND → ATtiny84 pin 14 (GND)
  3. Arduino Uno D10 → ATtiny84 pin 4 (RESET)
  4. Arduino Uno D11 → ATtiny84 pin 7 (MOSI / PA6)
  5. Arduino Uno D12 → ATtiny84 pin 8 (MISO / PA5)
  6. Arduino Uno D13 → ATtiny84 pin 9 (SCK / PA4)

See code below:

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <Adafruit_NeoPixel.h>

// ------------------------------------------------------------
// Hardware pins
// ------------------------------------------------------------

#define NEO PIN_PA2 // ATtiny84 physical pin 11 - WS2812 data
#define WAKE_PIN PIN_PA3 // ATtiny84 physical pin 10 - PIR output
#define LED_POWER PIN_PA1 // ATtiny84 physical pin 12 - BC327 control

// ------------------------------------------------------------
// LED settings
// ------------------------------------------------------------

#define NUMPIXELS 4

#define BRIGHTNESS 125 // Normal LED brightness
#define LED_ON_TIME 5000UL // Display each digit for 5 seconds
#define LED_OFF_TIME 200 // Blank period between digits

#define FADE_MIN 10 // Lowest stable WS2812 brightness
#define FADE_STEP 5
#define FADE_DELAY 40 // ms between fade steps

Adafruit_NeoPixel pixels(NUMPIXELS, NEO, NEO_GRB + NEO_KHZ400);


// ------------------------------------------------------------
// Digit colors
//
// Each color represents one numerical digit 0-9.
// ------------------------------------------------------------

struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
};

const Color arrayColors[10] =
{
{255, 0, 0}, // 0 - Red
{ 0, 255, 0}, // 1 - Green
{ 0, 0, 255}, // 2 - Blue
{255, 255, 0}, // 3 - Yellow
{ 0, 255, 255}, // 4 - Cyan
{255, 0, 255}, // 5 - Magenta
{255, 255, 255}, // 6 - White
{255, 128, 0}, // 7 - Orange
{128, 0, 255}, // 8 - Violet
{255, 128, 192} // 9 - Pink
};


// Counts PIR activations from 00 to 99.
// This value is stored only in RAM and therefore resets after power loss.
uint8_t activationCounter = 0;


// ------------------------------------------------------------
// Pin-change interrupt
//
// The interrupt does not need to perform any operation.
// Its only purpose is to wake the ATtiny from power-down sleep.
// ------------------------------------------------------------

ISR(PCINT0_vect)
{
}


// ------------------------------------------------------------
// Wake interrupt control
// ------------------------------------------------------------

void enableWakeInterrupt()
{
PCMSK0 |= _BV(PCINT3);
}


void disableWakeInterrupt()
{
PCMSK0 &= ~_BV(PCINT3);
}


void clearWakeInterrupt()
{
// AVR interrupt flags are cleared by writing a logic 1.
GIFR |= _BV(PCIF0);
}


// ------------------------------------------------------------
// LED functions
// ------------------------------------------------------------

void ledsPowerOn()
{
// BC327 high-side transistor:
// LOW = LEDs powered
digitalWrite(LED_POWER, LOW);

// Allow WS2812 supply to stabilize.
delay(10);
}


void setColor(uint8_t colorNumber)
{
pixels.setBrightness(BRIGHTNESS);

Color c = arrayColors[colorNumber];

for (uint8_t i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(c.r, c.g, c.b));
}

pixels.show();
}


void clearLeds()
{
// LEDs remain powered but display black.
pixels.clear();
pixels.show();
}


void fadeOut()
{
// Reduce brightness gradually from normal brightness
// to FADE_MIN.
//
// After this function returns, the LEDs remain ON
// at FADE_MIN until ledsOff() is called.

for (int b = BRIGHTNESS; b >= FADE_MIN; b -= FADE_STEP)
{
pixels.setBrightness(b);
pixels.show();
delay(FADE_DELAY);
}
}


void ledsOff()
{
// First send black while the WS2812s still have power.
pixels.clear();
pixels.show();

// Hold the WS2812 data line LOW before removing power.
// This prevents possible back-powering through DIN.
digitalWrite(NEO, LOW);
delay(1);

// BC327 OFF.
digitalWrite(LED_POWER, HIGH);

// Prepare normal brightness for next activation.
pixels.setBrightness(BRIGHTNESS);
}


// ------------------------------------------------------------
// Sleep
// ------------------------------------------------------------

void system_sleep()
{
// Make sure LEDs are unpowered before sleeping.
digitalWrite(LED_POWER, HIGH);
digitalWrite(NEO, LOW);

// Do not sleep if the PIR is already active.
if (digitalRead(WAKE_PIN) == HIGH)
{
return;
}

// Temporarily disable global interrupts while configuring sleep.
cli();

clearWakeInterrupt();
enableWakeInterrupt();

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();

// Check PIR again immediately before sleeping.
// This prevents missing a transition that occurred while
// sleep was being prepared.
if (digitalRead(WAKE_PIN) == LOW)
{
sei();
sleep_cpu();
}
else
{
sleep_disable();
sei();
return;
}

// Execution continues here after PIR wakes the ATtiny.
sleep_disable();
disableWakeInterrupt();

sei();
}


// ------------------------------------------------------------
// Setup
// ------------------------------------------------------------

void setup()
{
pixels.begin();

pinMode(LED_POWER, OUTPUT);
digitalWrite(LED_POWER, HIGH); // LEDs initially unpowered

pinMode(NEO, OUTPUT);
digitalWrite(NEO, LOW);

pinMode(WAKE_PIN, INPUT);

// Enable pin-change interrupt group for PORT A.
GIMSK |= _BV(PCIE0);

// Individual PIR interrupt is enabled only when entering sleep.
disableWakeInterrupt();

ledsOff();

sei();
}


// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------

void loop()
{
if (digitalRead(WAKE_PIN) == HIGH)
{
// We do not need PIR interrupts while processing an activation.
disableWakeInterrupt();

// --------------------------------------------------------
// Increment activation counter: 00 -> 01 -> ... -> 99 -> 00
// --------------------------------------------------------

activationCounter++;

if (activationCounter > 99)
{
activationCounter = 0;
}

// Split counter into two decimal digits.
uint8_t tens = activationCounter / 10;
uint8_t ones = activationCounter % 10;


// --------------------------------------------------------
// Display activation count
// --------------------------------------------------------

ledsPowerOn();

// Tens digit
setColor(tens);
delay(LED_ON_TIME);

// Brief blank interval so identical digits can still
// be distinguished, e.g. 11, 22, 33...
clearLeds();
delay(LED_OFF_TIME);

// Ones digit
setColor(ones);
delay(LED_ON_TIME);


// --------------------------------------------------------
// Fade down to low brightness
// --------------------------------------------------------

fadeOut();


// --------------------------------------------------------
// Keep LEDs dim while PIR remains active
// --------------------------------------------------------

while (digitalRead(WAKE_PIN) == HIGH)
{
delay(10);
}


// PIR has returned LOW - turn LEDs completely off.
ledsOff();


// Small settling time before allowing another activation.
delay(100);

// Remove any pending pin-change interrupt from the
// previous PIR transition.
cli();
clearWakeInterrupt();
sei();
}

// Sleep until the next PIR activation.
system_sleep();
}

Power consumption

Battery life was an important part of the design. Testing showed that increasing WS2812 brightness significantly increased current without producing an equally large improvement in perceived brightness through the acrylic. The normal LED brightness was therefore deliberately reduced to save power.

Measured directly from the battery, the complete system consumes approximately:

  1. 52 mA at normal LED brightness
  2. 22 mA in the faded/dim state
  3. 960 µA in standby

Assuming a worst-case scenario of 100 activations per day, with approximately 10 seconds at normal brightness and 20 seconds at low brightness per activation, the estimated consumption is about 49 mAh/day.

With the 2700 mAh battery, this gives a theoretical operating time of approximately:

2700 / 49 ≈ 55 days

Actual battery life will of course depend on the number and duration of PIR activations and the usable battery capacity.

Result

The final design is intentionally very simple: an ATtiny84, PIR sensor, four RGB LEDs, transistor and rechargeable battery supply.

The acrylic plate provides the visual effect, while the software turns the same four LEDs into a basic activity counter. It gives me the information I wanted without Wi-Fi, a display or any external system, while still keeping the design small and low-power.

Update - OLED Display and Battery Monitoring

The reason for the update is that I got much more activations that I expected. In a busy day I counted around 250. By adding a OLED display I now have a counter between 0-9999 and also the possiblity to show the battery level so I know when I expect to charge. This happens around 2.5V.

I added a 0.91" 128×32 SSD1306 OLED display for the counter and the battery-voltage monitoring. This can be also be a 128x64, as long it fits inside the enclosure. Since my plate is acrylic you view the display through the top part.

The OLED provides information directly on the device when motion is detected. It displays two large values: the PIR activation counter and the measured battery voltage. For example:

0001 401

Here, 0001 represents the number of PIR activations and 401 represents a battery voltage of 4.01 V. The display uses Tiny4kOLED with FONT8X16 at ×2 scaling to make the numbers as large and readable as possible on the 128×32 display.

The OLED is normally switched off to minimize standby consumption. When the PIR detects movement, the ATtiny84 wakes, measures the battery voltage, updates the display and activates the WS2812 lighting. The OLED's measured consumption while switched off is only approximately 40 µA, so it remains permanently connected to the 5 V supply rather than being switched by the BC327.

Battery voltage is measured using the ATtiny84's ADC input PA0 (physical pin 13). The 18650 battery positive terminal is connected to PA0 through a 10 kΩ series resistor. Because the battery voltage cannot exceed approximately 4.2 V while the ATtiny84 operates from the boosted 5 V supply, a voltage divider is not required.

To improve measurement stability, the firmware takes eight ADC samples and averages them. The measurement is made shortly after wake-up but before the WS2812 LEDs are powered, avoiding voltage drop caused by the relatively high LED current. The ADC result is converted directly to centivolts, so a measured value of 357 corresponds to 3.57 V.

The operating sequence is now approximately:

  1. PIR detects movement and wakes the ATtiny84.
  2. Battery voltage is measured using PA0.
  3. Activation counter is incremented.
  4. OLED displays the activation count and battery voltage.
  5. BC327 switches power on to the four WS2812 LEDs.
  6. LEDs illuminate at normal brightness for approximately 10 seconds and then fade to a low level.
  7. When the PIR becomes inactive, the LEDs are switched completely off, the OLED is turned off, and the ATtiny84 returns to power-down sleep.

The battery remains a single 18650 Li-ion cell, with the LX-LCBST module providing charging and a regulated 5 V supply. Low-voltage battery protection is handled by the LX-LCBST/HM5434 circuitry rather than by the ATtiny84.

One additional reliability issue was identified during battery-discharge testing: the ADC connection can potentially back-power the ATtiny84 through PA0 when the 5 V supply disappears. The current design retains the 10 kΩ resistor while testing whether enabling the ATtiny84's 4.3 V brown-out detector (BOD) provides reliable automatic recovery after a complete battery discharge and subsequent recharge. If necessary, the ADC input circuitry can later be changed to further reduce this back-power path.

Software is updated using the Tiny4kOLED and with the BOD set to 4.3V.

/*
* ============================================================================
* PIR-Activated LED Activity Counter
* Version 1.2
* 14 Oct 2026 18:16
* ============================================================================
*
* Platform : ATtiny84 (ATTinyCore)
* Purpose : Battery-powered motion/activity counter with LED illumination,
* OLED display and battery voltage monitoring.
*
* HARDWARE
* --------
* MCU : ATtiny84
* PIR : HC-SR505, active HIGH
* LEDs : 4 x WS2812B (NeoPixel, GRB, 400 kHz)
* LED power : BC327 PNP high-side switch
* Display : SSD1306 OLED, 128 x 32, I2C
* Battery : Samsung INR18650-29E, 2900 mAh
* Supply : LX-LCBST charger / 5 V boost converter
*
* PIN ASSIGNMENTS
* ---------------
* PA0 (pin 13) : Battery voltage ADC (10k series resistor)
* PA1 (pin 12) : LED power control (LOW = ON, HIGH = OFF)
* PA2 (pin 11) : WS2812B data
* PA3 (pin 10) : PIR input / PCINT3 (HIGH = motion detected)
*
* OPERATION
* ---------
* 1. MCU remains in power-down sleep until PIR activation.
* 2. PIR HIGH wakes the MCU through pin-change interrupt.
* 3. Battery voltage is measured and activity counter incremented.
* 4. OLED displays activation count and battery voltage.
* 5. LEDs illuminate at full brightness for 10 seconds.
* 6. LEDs fade to a low brightness level while PIR remains active.
* 7. When PIR goes LOW, LEDs and OLED are switched off.
* 8. MCU returns to power-down sleep.
*
* POWER MANAGEMENT
* ----------------
* LED active : ~52 mA
* LED dimmed : ~22 mA
* Power-down sleep : ~0.960 mA (battery-side, including electronics)
*
* Battery life estimate:
* 200 activations/day, approximately 30 seconds each
* With sleep : ~39 days (theoretical)
* Observed : ~35 days
*
* IMPORTANT
* ---------
* - PIR is active HIGH; sleep is permitted only when PIR is LOW.
* - PCINT3 is used for wake-up from SLEEP_MODE_PWR_DOWN.
* - WS2812 data must be LOW before switching off LED power.
* - LED power is disconnected during standby to minimize consumption.
* - Battery ADC uses MCU VCC as reference; calibrate to actual supply.
* - Brown-out detection configured for 4.3 V.
*
* LIBRARIES
* ---------
* Adafruit_NeoPixel
* Tiny4kOLED
* avr/sleep.h
* avr/interrupt.h
*
* ============================================================================
*/

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <Adafruit_NeoPixel.h>
#include <Tiny4kOLED.h>

// ============================================================================
// Hardware
// ============================================================================

#define NEO PIN_PA2 // Physical pin 11 - WS2812 data
#define WAKE_PIN PIN_PA3 // Physical pin 10 - HC-SR505 PIR output
#define LED_POWER PIN_PA1 // Physical pin 12 - BC327 high-side switch
#define BATTERY_PIN PIN_PA0 // Physical pin 13
#define ADC_VREF_MV 5100UL // Change to measured VCC, e.g. 5100

// ============================================================================
// LED configuration
// ============================================================================

#define NUMPIXELS 4
#define NUM_COLORS 10

#define BRIGHTNESS 125
#define LED_ON_TIME 10000UL // 10 seconds at normal brightness

#define FADE_MIN 10 // Lowest stable WS2812 brightness
#define FADE_STEP 5
#define FADE_DELAY 40 // Delay between fade steps in ms

Adafruit_NeoPixel pixels(
NUMPIXELS,
NEO,
NEO_GRB + NEO_KHZ400
);

// ============================================================================
// LED colors
//
// A new color is selected for each PIR activation.
// ============================================================================

struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
};

const Color arrayColors[NUM_COLORS] =
{
{255, 0, 0}, // 0 - Red
{ 0, 255, 0}, // 1 - Green
{ 0, 0, 255}, // 2 - Blue
{255, 255, 0}, // 3 - Yellow
{ 0, 255, 255}, // 4 - Cyan
{255, 0, 255}, // 5 - Magenta
{255, 255, 255}, // 6 - White
{255, 128, 0}, // 7 - Orange
{128, 0, 255}, // 8 - Violet
{255, 128, 192} // 9 - Pink
};

// ============================================================================
// Activation counter
//
// Counts PIR activations from 0000 to 9999.
// Stored only in RAM, so it resets to zero after power loss.
// ============================================================================

uint16_t activationCounter = 0;

// ============================================================================
// Battery centivolt
//
// Read battery voltage in centivolts ie 123 is 1.23V
// ============================================================================
uint16_t batteryCentiVolt = 0;

// ============================================================================
// Pin-change interrupt
//
// The ISR intentionally does nothing.
// The interrupt is used only to wake the ATtiny84 from power-down sleep.
// ============================================================================

ISR(PCINT0_vect)
{
}

// ============================================================================
// PIR wake interrupt control
// ============================================================================

void enableWakeInterrupt()
{
PCMSK0 |= _BV(PCINT3);
}

void disableWakeInterrupt()
{
PCMSK0 &= ~_BV(PCINT3);
}

void clearWakeInterrupt()
{
// AVR interrupt flags are cleared by writing a logic 1.
GIFR = _BV(PCIF0);
}

// ============================================================================
// LED control
// ============================================================================

void ledsPowerOn()
{
// BC327 high-side switch:
// LOW = transistor ON = WS2812s powered.
digitalWrite(LED_POWER, LOW);

// Allow the WS2812 supply to stabilize before sending data.
delay(10);
}


void setNextColor()
{
static uint8_t colorIndex = 0;

// Advance to the next color.
colorIndex++;

if (colorIndex >= NUM_COLORS)
{
colorIndex = 0;
}

pixels.setBrightness(BRIGHTNESS);

Color c = arrayColors[colorIndex];

for (uint8_t i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(
i,
pixels.Color(c.r, c.g, c.b)
);
}

pixels.show();
}

void fadeOut()
{
// Gradually reduce the current LED color from BRIGHTNESS
// down to FADE_MIN.
//
// The LEDs remain illuminated at FADE_MIN when this
// function returns.

for (int brightness = BRIGHTNESS;
brightness >= FADE_MIN;
brightness -= FADE_STEP)
{
pixels.setBrightness(brightness);
pixels.show();

delay(FADE_DELAY);
}
}

void ledsOff()
{
// Send black while the WS2812s are still powered.
pixels.clear();
pixels.show();

// Hold DIN LOW before removing power. This prevents
// back-powering the WS2812s through the data input.
digitalWrite(NEO, LOW);

delay(1);

// HIGH = BC327 OFF = WS2812 power removed.
digitalWrite(LED_POWER, HIGH);

// Restore normal brightness for the next activation.
pixels.setBrightness(BRIGHTNESS);
}

// ============================================================================
// OLED
// ============================================================================

void showCounter()
{
char counterBuf[5];
char batteryBuf[5];

activationCounter++;

if (activationCounter > 9999)
activationCounter = 0;

// Activation counter: 0000-9999
counterBuf[0] = '0' + (activationCounter / 1000);
counterBuf[1] = '0' + ((activationCounter / 100) % 10);
counterBuf[2] = '0' + ((activationCounter / 10) % 10);
counterBuf[3] = '0' + (activationCounter % 10);
counterBuf[4] = '\0';

batteryBuf[0] = '0' + ((batteryCentiVolt / 100) % 10);
batteryBuf[1] = '0' + ((batteryCentiVolt / 10) % 10);
batteryBuf[2] = '0' + (batteryCentiVolt % 10);
batteryBuf[3] = '\0';
oled.clear();

// Large 4-digit activation counter
oled.setCursor(0, 16);
oled.print(counterBuf);

// Small battery percentage
oled.setCursor(80, 16);
oled.print(batteryBuf);

oled.on();
}


void oledOff()
{
// Clear the display RAM and put the OLED controller
// into display-off mode.
//
// OLED power remains connected.
//oled.clear();
oled.off();
}

uint16_t readBatteryVoltage()
{
uint32_t sum = 0;

for (uint8_t i = 0; i < 8; i++)
sum += analogRead(BATTERY_PIN);

uint16_t adc = sum / 8;

return (adc * ADC_VREF_MV) / 10230UL; //Scale by 1023x10
}

// ============================================================================
// Power-down sleep
// ============================================================================

void systemSleep()
{
digitalWrite(LED_POWER, HIGH);
digitalWrite(NEO, LOW);

cli();

disableWakeInterrupt();
clearWakeInterrupt();
enableWakeInterrupt();

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();

if (digitalRead(WAKE_PIN) == LOW)
{
sei();
sleep_cpu();
}
else
{
sei();
}

sleep_disable();
disableWakeInterrupt();
}

// ============================================================================
// Setup
// ============================================================================

void setup()
{
// --------------------------------------------------------------------------
// WS2812
// --------------------------------------------------------------------------

pixels.begin();

pinMode(LED_POWER, OUTPUT);
digitalWrite(LED_POWER, HIGH); // BC327 OFF

pinMode(NEO, OUTPUT);
digitalWrite(NEO, LOW);

// --------------------------------------------------------------------------
// PIR
// --------------------------------------------------------------------------

pinMode(WAKE_PIN, INPUT);

// Enable the PORT A pin-change interrupt group.
GIMSK |= _BV(PCIE0);

// The individual PIR interrupt is enabled only before sleep.
disableWakeInterrupt();

// --------------------------------------------------------------------------
// Initial LED state
// --------------------------------------------------------------------------

ledsOff();

// --------------------------------------------------------------------------
// OLED
// --------------------------------------------------------------------------

oled.begin(
128,
32,
sizeof(tiny4koled_init_128x32),
tiny4koled_init_128x32
);
// Double-size 8x16 font -> 16x32 characters.
oled.setFontX2(FONT8X16);

oledOff();

// Enable global interrupts.
sei();
}


// ============================================================================
// Main loop
// ============================================================================

void loop()
{
// --------------------------------------------------------------------------
// PIR activation
// --------------------------------------------------------------------------

if (digitalRead(WAKE_PIN) == HIGH)
{
// Pin-change interrupt is not needed while processing
// an active PIR event.
disableWakeInterrupt();

delay(25);
batteryCentiVolt = readBatteryVoltage();

// ------------------------------------------------------------------------
// Update and display activation counter
// ------------------------------------------------------------------------

showCounter();

// ------------------------------------------------------------------------
// Turn on LEDs and select next color
// ------------------------------------------------------------------------

ledsPowerOn();
setNextColor();

// Keep normal brightness for the configured period.
delay(LED_ON_TIME);

// ------------------------------------------------------------------------
// Fade to low brightness
// ------------------------------------------------------------------------

fadeOut();

// ------------------------------------------------------------------------
// Remain dim while PIR output is HIGH
// ------------------------------------------------------------------------

while (digitalRead(WAKE_PIN) == HIGH)
{
delay(10);
}

// ------------------------------------------------------------------------
// PIR returned LOW
// ------------------------------------------------------------------------

ledsOff();
oledOff();

// Short settling period before another activation can occur.
delay(100);

// Clear any pin-change flag generated by the PIR falling edge.
cli();
clearWakeInterrupt();
sei();
}

// --------------------------------------------------------------------------
// Sleep until next PIR activation
// --------------------------------------------------------------------------

systemSleep();
}