//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;

int ics[20] = {0,
               0,
               0,
               0,
               0,
               1,
               0,
               0,
               0,
               0,//A
               0,//B
               0,//C
               0,//D
               0,//
               0,//F
               0,//G
               0,
               0,
               0,
               0
              };

int a[20] = {0,
             0,
             0,
             0,
             0,
             1,
             1,//A
             0,//B
             0,//C
             0,//D
             0,//E
             0,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };

int b[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             1,//B
             0,//C
             0,//D
             0,//E
             0,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };

int c[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             0,//B
             1,//C
             0,//D
             0,//E
             0,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };

int d[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             0,//B
             0,//C
             1,//D
             0,//E
             0,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };

int e[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             0,//B
             0,//C
             0,//D
             0,//
             0,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 1//E
            };

int f[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             0,//B
             0,//C
             0,//D
             0,//E
             1,//F
             0,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };

int g[20] = {0,
             0,
             0,
             0,
             0,
             1,
             0,//A
             0,//B
             0,//C
             0,//D
             0,//E
             0,//F
             1,//G
             0,
             0,
             0,
             0, 0, 0, 0
            };
int k = 0;

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() {

  // first put position of character
  for (int i = 0; i < 20; i++) {
    if (i == k) digitalWrite(din, 1);
    else  digitalWrite(din, 0);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }
  digitalWrite(clk, LOW);
  /*
    // second put letter/character
    for (int i = 19; i >= 0; i--) {
      digitalWrite(din, ics[i]);
      digitalWrite(clk, HIGH);
      delay(10);
      digitalWrite(clk, LOW);
      delay(10);
    }
  */
  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, a[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, b[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, c[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, d[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, e[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, f[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  for (int i = 19; i >= 0; i--) {
    digitalWrite(din, g[i]);
    digitalWrite(clk, HIGH);
    delay(10);
    digitalWrite(clk, LOW);
    delay(10);
  }

  // load the changes
  digitalWrite(load, HIGH);
  delay(10);
  digitalWrite(load, LOW);

  delay(200);

  k = k + 1;
  if (k > 19) k = 0;

}
