#define ledData 2
#define ledShift 3
#define ledLatch 4
#define potPin A0
#define gridSize 8
#define changeSpeed 1000
int delayTime(0);
byte led[gridSize];
unsigned long lastChange(0);
int currentLetter(-1);
const byte capH[gridSize] = {
  0b11100111,
  0b11100111,
  0b11100111,
  0b11111111,
  0b11111111,
  0b11100111,
  0b11100111,
  0b11100111
};
const byte capE[gridSize] = {
  0b11111111,
  0b11111111,
  0b11100000,
  0b11111110,
  0b11111110,
  0b11100000,
  0b11111111,
  0b11111111
};
const byte capL[gridSize] = {
  0b11100000,
  0b11100000,
  0b11100000,
  0b11100000,
  0b11100000,
  0b11100000,
  0b11111111,
  0b11111111
};
const byte capP[gridSize] = {
  0b11111110,
  0b11111111,
  0b11100011,
  0b11111111,
  0b11111110,
  0b11100000,
  0b11100000,
  0b11100000
};
void setup()
{
  pinMode(ledData, OUTPUT);
  pinMode(ledShift, OUTPUT);
  pinMode(ledLatch, OUTPUT);
  pinMode(potPin, INPUT);
  digitalWrite(ledLatch, LOW);
  led[0] = 0b00000000;
  led[1] = 0b01100110;
  led[2] = 0b01100110;
  led[3] = 0b00000000;
  led[4] = 0b11000011;
  led[5] = 0b01100110;
  led[6] = 0b00111100;
  led[7] = 0b00000000;
  
  lastChange = millis();
}
void loop()
{
  int delayRead = analogRead(potPin);
  delayTime = map(delayRead,0,1023,0,100);
  
  byte row = 0b00000001;
  for (int i = 0; i != gridSize; i++)
  {
    digitalWrite(ledLatch, HIGH);
    shiftOut(ledData, ledShift, LSBFIRST, led[i]);
    shiftOut(ledData, ledShift, MSBFIRST, row);
    digitalWrite(ledLatch, LOW);
    delay(delayTime);
    row = row << 1;
  }
  int currentMillis = millis();
  if(changeSpeed < (currentMillis - lastChange))
  {
    lastChange = currentMillis;
    nextLetter();
  } 
}
void clearGrid()
{
  for (int i = 0; i != gridSize; i++)
  {
    led[i] = 0b00000000;
  }
}
void nextLetter()
{
  currentLetter++;
  currentLetter %= 6;
  switch(currentLetter)
  {
    case 0:
      for(int i=0; i!=8; i++)
      {
        led[i] = capH[i];
      }
      break;
    case 1:
      for(int i=0; i!=8; i++)
      {
        led[i] = capE[i];
      }
      break;
    case 2:
      for(int i=0; i!=8; i++)
      {
        led[i] = capL[i];
      }
      break;
    case 3:
      for(int i=0; i!=8; i++)
      {
        led[i] = capP[i];
      }
      break;
    case 4:
      clearGrid();
      break;
    case 5:
      clearGrid();
      break;
  }
}