/**
  ATtiny85 Menora for Hanukkah with Charlieplexing.

  Learn more about Charlieplexing: https://goodarduinocode.com/guides/charlieplexing

  Copyright (C) 2020, Uri Shaked
*/

#define BUTTON_PIN PB3

// LEDs are connected to PB0, PB1, PB2, and PB4, in Charlieplexing configuration.
// See diagram.json for more information about the actual connections.
byte LEDS[][2] = {
  {PB0, PB1},
  {PB1, PB0},
  {PB0, PB2},
  {PB2, PB0},
  {PB2, PB1},
  {PB4, PB0},
  {PB4, PB1},
  {PB4, PB2},
  {PB1, PB2},
};

void lightCandle(int index) {
  pinMode(PB0, INPUT);
  pinMode(PB1, INPUT);
  pinMode(PB2, INPUT);
  pinMode(PB4, INPUT);
  pinMode(LEDS[index][0], OUTPUT);
  pinMode(LEDS[index][1], OUTPUT);
  digitalWrite(LEDS[index][0], LOW);
  digitalWrite(LEDS[index][1], HIGH);
}

void setup() {
  pinMode(PB3, INPUT_PULLUP);
}

int candle = 0;
void loop() {
  if (digitalRead(PB3) == LOW) {
    candle = (candle + 1) % 8;
    delay(200);
  }
  lightCandle(8);
  for (int i = 0; i <= candle; i++) {
    lightCandle(i);
  }
}
ATTINY8520PU