//quick vfd tube driver test
// MAX6921 -> datasheet: http://datasheets.maximintegrated.com/en/ds/MAX6921-MAX6931.pdf
// original sketch from https://github.com/echox/arduino-test/blob/master/max6921.ino
// current sketch by Nicu FLORICA (aka niq_ro): http://www.tehnic.go.ro
// & http://www.arduinotehniq.com/

int clk = 3;
int load = 5;
int din = 4;

byte hours = 0;

byte zero[20] = {1,//grid 1
                 0,//grid 2
                 0,//grid 3
                 0,//grid 4
                 0,//grid 5
                 0,//grid 6
                 1,//A
                 1,//B
                 1,//C
                 1,//D
                 0,//DOES NOT WORK
                 1,//F
                 0,//G
                 0,
                 0,
                 0,
                 0, 0, 0, 1//E
                };

byte one[20] = {1,
                0,
                0,
                0,
                0,
                0,
                0,//A
                1,//B
                1,//C
                0,//D
                0,//DOES NOT WORK
                0,//F
                0,//G
                0,
                0,
                0,
                0, 0, 0, 0//E
               };

byte two[20] = {1,
                0,
                0,
                0,
                0,
                0,
                1,//A
                1,//B
                0,//C
                1,//D
                0,//DOES NOT WORK
                0,//F
                1,//G
                0,
                0,
                0,
                0, 0, 0, 1//E
               };

byte three[20] = {1,
                  0,
                  0,
                  0,
                  0,
                  0,
                  1,//A
                  1,//B
                  1,//C
                  1,//D
                  0,//DOES NOT WORK
                  0,//F
                  1,//G
                  0,
                  0,
                  0,
                  0, 0, 0, 0//E
                 };

byte four[20] = {1,
                 0,
                 0,
                 0,
                 0,
                 0,
                 0,//A
                 1,//B
                 1,//C
                 0,//D
                 0,//DOES NOT WORK
                 1,//F
                 1,//G
                 0,
                 0,
                 0,
                 0, 0, 0, 0//E
                };

byte five[20] = {1,
                 0,
                 0,
                 0,
                 0,
                 0,
                 1,//A
                 0,//B
                 1,//C
                 1,//D
                 0,//DOES NOT WORK
                 1,//F
                 1,//G
                 0,
                 0,
                 0,
                 0, 0, 0, 0//E
                };

byte six[20] = {1,
                0,
                0,
                0,
                0,
                0,
                1,//A
                0,//B
                1,//C
                1,//D
                0,//DOES NOT WORK
                1,//F
                1,//G
                0,
                0,
                0,
                0, 0, 0, 1//E
               };

byte seven[20] = {1,
                  0,
                  0,
                  0,
                  0,
                  0,
                  1,//A
                  1,//B
                  1,//C
                  0,//D
                  0,//DOES NOT WORK
                  0,//F
                  0,//G
                  0,
                  0,
                  0,
                  0, 0, 0, 0//E
                 };

byte eight[20] = {1,
                  0,
                  0,
                  0,
                  0,
                  0,
                  1,//A
                  1,//B
                  1,//C
                  1,//D
                  0,//DOES NOT WORK
                  1,//F
                  1,//G
                  0,
                  0,
                  0,
                  0, 0, 0, 1//E
                 };

byte nine[20] = {1,
                 0,
                 0,
                 0,
                 0,
                 0,
                 1,//A
                 1,//B
                 1,//C
                 1,//D
                 0,//DOES NOT WORK
                 1,//F
                 1,//G
                 0,
                 0,
                 0,
                 0, 0, 0, 0//E
                };



void setup() {
  pinMode(din, OUTPUT);
  pinMode(load, OUTPUT);
  pinMode(clk, OUTPUT);

  digitalWrite(clk, LOW);
  digitalWrite(load, LOW);
  digitalWrite(din, LOW);
}

// the loop routine runs over and over again forever:
void loop() {

  hours++;
  if (hours > 9) {
    hours = 0;
  }
 

    if (hours == 0) afisare0(0, zero);
    if (hours == 1) afisare0(0, one);
    if (hours == 2) afisare0(0, two);
    if (hours == 3) afisare0(0, three);
    if (hours == 4) afisare0(0, four);
    if (hours == 5) afisare0(0, five);
    if (hours == 6) afisare0(0, six);
    if (hours == 7) afisare0(0, seven);
    if (hours == 8) afisare0(0, eight);
    if (hours == 9) afisare0(0, nine);
    

  delay(500);
}

void afisare0 (byte pozitie, byte caracter[20]) {
  // first put position of character
  for (int i = 0; i < 20; i++) {
    if (i == pozitie) digitalWrite(din, 0);
    else  digitalWrite(din, 0);
    digitalWrite(clk, HIGH);
    digitalWrite(clk, LOW);
  }
  //   digitalWrite(clk, LOW);

  // second put letter/character
  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, caracter[i]);
    digitalWrite(clk, HIGH);
    digitalWrite(clk, LOW);
  }
  //   digitalWrite(clk, LOW);

  // load the changes
  digitalWrite(load, HIGH);
  digitalWrite(load, LOW);
}
