#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

unsigned int width = 15;
unsigned int height = 1;

void setup() 
{
 lcd.begin(16, 2);   //starting the LCD as a 16 character, 2 line display
 lcd.clear();
}

void loop() 
{  
 //objectiveOne(750);
 //objectiveTwo(50, 1500);
 //objectiveThree(750, 250);
 objectiveFour(750, 250);
}


void objectiveOne(int pace)
{
  int i = 0;
  while (i < width)
  {
    lcd.setCursor(i, 1);
    lcd.print("f");
    delay(pace);
    lcd.clear();
    i++;
  }
}

void objectiveTwo(int pace, int finalDelay)
{
  int i = 0;
  int x = 0;
  int y = 0;
  while (!(x==width) && !(y==height))
  {
    lcd.clear();
    lcd.setCursor(x, y);
    lcd.print("f");
    delay(pace);
    y = (y == 1 ) ? 0 : 1;

    lcd.clear();
    lcd.setCursor(x, y);
    lcd.print("f");
    delay(pace);
    x = (x == width) ? width : x + 1;
    y = (y == 1 ) ? 0 : 1;
  }
  
  delay(finalDelay);
  lcd.clear();
  while(true) {delay(10);}
}

void objectiveThree (int paceF, int paceE)
{
  int i = 0;
  int x = 0;
  int y = 0;

  while(true)
  {
    while (true)
    {
      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("f");
      delay(paceF);
      y = (y == 1 ) ? 0 : 1;

      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("f");
      delay(paceF);
      if (x==width && y==height)
      {
        break;
      }
      x = x + 1;
      y = (y == 1 ) ? 0 : 1;
    }
  
    while (true)
    {
      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("e");
      delay(paceE);
      y = (y == 1 ) ? 0 : 1;

      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("e");
      delay(paceE);
      if (x==0 && y==0)
      {
        break;
      }
      x = x - 1;
      y = (y == 1 ) ? 0 : 1;
    }
    lcd.clear();
  }
}

void objectiveFour (int paceF, int paceE)
{
  int i = 0;
  int x = 0;
  int y = 0;
  int freq = 200;
  bool first = true;

  while(true)
  {
    while (true)
    {
      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("f");
      if (first)
      {
        tone(7, freq, paceF);
        freq = freq+25;
        first = false;
      }
      delay(paceF);
      y = (y == 1 ) ? 0 : 1;

      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("f");
      delay(paceF);
      if (x==width && y==height)
      {
        break;
      }
      x = x + 1;
      y = (y == 1 ) ? 0 : 1;
    }
  
    while (true)
    {
      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("e");
      tone(7, freq, paceE);
      freq = freq+25;

      delay(paceE);
      y = (y == 1 ) ? 0 : 1;

      lcd.clear();
      lcd.setCursor(x, y);
      lcd.print("e");
      tone(7, freq, paceE);
      freq = freq+25;
      delay(paceE);
      if (x==0 && y==0)
      {
        break;
      }
      x = x - 1;
      y = (y == 1 ) ? 0 : 1;
    }
  }
}