#include <avr/sleep.h>
#include <avr/interrupt.h>
const int redPin = 0; // PB0
const int greenPin = 1; // PB1
const int bluePin = 4; // PB4
const int tiltPin = 2; // PB2 (Interrupt 0)
void wakeUp() {}
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(tiltPin, INPUT_PULLUP);
}
void setColor(int r, int g, int b) {
analogWrite(redPin, r);
analogWrite(greenPin, g);
// PB4 doesn't support analogWrite easily on all cores,
// so we'll use digitalWrite for a simple Blue on/off
digitalWrite(bluePin, b > 127 ? HIGH : LOW);
}
void loop() {
// --- 1. THE ACTION: Cycle through colors ---
for (int i = 0; i < 3; i++) {
// Pulse Red-ish
setColor(255, 0, 0); delay(200);
// Pulse Green-ish
setColor(0, 255, 0); delay(200);
// Pulse Blue-ish/Cyan
setColor(0, 255, 255); delay(200);
// Pulse White (All on)
setColor(255, 255, 255); delay(200);
}
setColor(0, 0, 0); // Turn off
// --- 2. THE SLEEP ---
ADCSRA &= ~(1 << ADEN);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
sleep_cpu();
// --- 3. WAKEUP ---
sleep_disable();
detachInterrupt(0);
delay(500);
}