#include <tiny2313.h>
#include <delay.h>

 void main(void) {
 char _states[] = {0, 0};
 char _run[] = {0b11111101, 0b11110111, 0b11011111, 0b01111111};
 char _index = 0, _light = 0, _mode = 1;
  DDRB = 0b11111111;
  while (PIND) {}
  while (1) {
   if (_light) {
    switch (_mode) {
     case 1: PORTB = 0b01010101; break;
     case 2: PORTB = 0b11110101; break;
     case 3: PORTB = 0b11010101; break;
     case 4:
      PORTB = _run[_index++];
      if (_index == 4) _index = 0;
      _delay_ms(100);}
   PORTD = 0b00000011;}
   else {
    PORTB = 0b11111111;
    PORTD = 0b11111111;}        
   if ((PIND & 0b00000001) != _states[0]) {
      if (_states[0]) _light = !_light;
      _states[0] = PIND & 0b00000001;}
    if ((PIND & 0b00000010) != _states[1]) {
      if (_states[1] && ++_mode == 5) _mode = 1;
      _states[1] = PIND & 0b00000010;}}}