Back to engineering archive

Record 024 / Firmware and control

Wearable motion firmware

ATtiny84A firmware for wearable RGB lighting, saved colour and brightness settings, and inactivity-triggered power-down with vibration-switch wake.

Firmware development
Motion inputLow power firmwareLighting control

Firmware flow

Preparing power-down and restoring operation after wake

Selected AVR sleep helper and its pin-change handler; the vibration interrupt source is configured separately during setup.

The helper clears the event flag, disables the ADC and requests power-down sleep. The pin-change handler marks the wake event. After execution resumes, the helper disables sleep and restores the ADC; lighting restoration belongs to the main loop.

The source shows the intended sleep and wake sequence. Current draw, battery life, wake latency and hardware reliability have not been measured in this review.

Read the implementation

Selected firmware excerpt. The diagram explains the code; it is not a hardware test.

Selected implementation

void enterPowerDownUntilVibration() {
  wokeFromPCINT = false;

  // Disable ADC to save power
  ADCSRA &= ~(1 << ADEN);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  cli();
  sleep_enable();
  #if defined(BODS) && defined(BODSE)
    // Disable Brown-Out during sleep (saves more power)
    sleep_bod_disable();
  #endif
  sei();
  sleep_cpu();      // ---- CPU sleeps here ----

  // Woke up
  sleep_disable();
  ADCSRA |= (1 << ADEN);
}

// ---------- ISRs ----------
ISR(PCINT0_vect) { // PORT A pin-change
  wokeFromPCINT = true;
}
ActionClear the wake flag
ActionDisable the ADC
ActionPrepare power-down sleepSet power-down mode, enable sleep and optionally disable brown-out when the compile-time register symbols exist.
ActionPin-change handler sets the wake flagThe handler records the event; it does not update the LEDs.
ResultDisable sleep and restore the ADCThe main loop uses the flag to restore lighting on its next iteration.

Technical notes

Saved lighting settings

The firmware offers 20 colour entries and 15 brightness levels. Button events update the selected setting, save it to EEPROM and reapply the RGB PWM output; startup restores the saved values with range checks.

An inactivity threshold

Button presses and vibration-switch activity refresh the activity timer. At the configured three-second inactivity threshold, the main loop turns the RGB outputs off, marks the lighting inactive and calls the power-down helper.

Wake flag, then lighting restore

The vibration pin-change handler sets a wake flag. After the CPU resumes, the sleep helper disables sleep and restores the ADC; the next main-loop iteration uses the flag to restore lighting. The source shows this intended sequence, without establishing measured battery life, current draw or wake latency.

Engineering archiveArchive record

Project enquiries

Discuss your project.

Tell us what you’re developing and where you need engineering support.

Discuss your project