#include <Servo.h> 
#include <LiquidCrystal.h>

//define lcd
LiquidCrystal lcd(13, 12, 11, 10, 8, 7);

char * messagePadded = "                Advancing towards better future                ";

//define the servos
Servo base;
Servo arm1;
Servo arm2;
Servo gripper;

//joy pins
#define j1x A0
#define j1y A1
#define j2x A2
#define j2y A3

void setup() {
  //define the servo pins
  base.attach(3);
  arm1.attach(5);
  arm2.attach(9);
  gripper.attach(6);

 //innitial poition
  base.write(90);
  arm1.write(90);
  arm2.write(90);
  gripper.write(90);

  //define joy pins as input units
  pinMode(j1x, INPUT);
  pinMode(j1y, INPUT);
  pinMode(j2x, INPUT);
  pinMode(j2y, INPUT);

  //initialize lcd display
  lcd.begin(16, 2);
  display();
  
}

void loop() {

  //-------------------------------------------
  if(analogRead(j1y) >= 800){
     for (int i = base.read(); i < 180; i++){
      if (analogRead(j1y) < 800){
        break;
      }
      base.write(i);
      delay(20);
     }
  }


  else if(analogRead(j1y) <= 300){
     for (int i = base.read(); i > 0; i--){
      if (analogRead(j1y) > 300){
        break;
      }
      base.write(i);
      delay(20);
     }
  }
//-------------------------------------------
  if(analogRead(j1x) >= 800){
     for (int i = arm1.read(); i < 180; i++){
      if (analogRead(j1x) < 800){
        break;
      }
      arm1.write(i);
      delay(20);
     }
  }


  else if(analogRead(j1x) <= 300){
     for (int i = arm1.read(); i > 0; i--){
      if (analogRead(j1x) > 300){
        break;
      }
      arm1.write(i);
      delay(20);
     }
  }

//-------------------------------------------
  if(analogRead(j2y) >= 800){
     for (int i = arm2.read(); i < 120; i++){
      if (analogRead(j2y) < 800){
        break;
      }
      arm2.write(i);
      delay(20);
     }
  }


  else if(analogRead(j2y) <= 300){
     for (int i = arm2.read(); i > 60; i--){
      if (analogRead(j2y) > 300){
        break;
      }
      arm2.write(i);
      delay(20);
     }
  }

//-------------------------------------------
  if(analogRead(j2x) >= 800){
     for (int i = gripper.read(); i < 120; i++){
      if (analogRead(j2x) < 800){
        break;
      }
      gripper.write(i);
      delay(35);
     }
  }


  else if(analogRead(j2x) <= 300){
     for (int i = gripper.read(); i > 50; i--){
      if (analogRead(j2x) > 300){
        break;
      }
      gripper.write(i);
      delay(35);
     }
  }
}

void scroll_letter(){
  for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) //From 0 to upto n-16 characters supply to below function
  {
    showLetters(0, letter);
    delay(350);
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print("BE4ME: MK2");
  }
}

void showLetters(int printStart, int startLetter)
{
  lcd.setCursor(printStart, 1);
  for (int letter = startLetter; letter <= startLetter + 15; letter++)
  {
    lcd.print(messagePadded[letter]);
  }
  lcd.print(" ");
  delay(250);
}

void display(){
  scroll_letter();
  lcd.clear();
  delay(500);
  lcd.print("BE4ME:Strive for");
  lcd.setCursor(3, 1);
  lcd.print("the best");
  lcd.setCursor(0, 1);
}