/*
 * Copyright (C) 2022, Andrei Egorov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

byte digit[]={0B11000000, 0B11111001, 0B10100100, 0B10110000, 0B10011001,
              0B10010010, 0B10000010, 0B11111000, 0B10000000, 0B10010000};
byte d[]={0,0,0,0};
int ms=0;

void setup() {
  DDRB=0x0F;
  DDRD=0xFF;
  pinMode(13, OUTPUT);
;}

void loop() {
  static uint32_t m=millis();
  static uint32_t m1=millis();
  if ((millis()-m1)>=500) {
    m1+=500;
    digitalWrite(13,!digitalRead(13));
  }
  if ((millis()-m)>=1000) {
    m+=1000;
    ms++;
    d[0]=ms/1000;
    d[1]=ms%1000/100;
    d[2]=ms%100/10;
    d[3]=ms%10;
  }
  for (byte i=0,j=1;i<=3;i++) {
    PORTD=digit[d[i]];
    PINB=j;
    delay(5);
    PINB=j;
    j<<=1;
  }
}