#include <Servo.h>
#include <LiquidCrystal_I2C.h>

const int servoPin[]  = {9, 10, 11, 12}; // array of servo control pins
const int buttonPin[] = {2, 3, 4, 5};    // array of button pins

const int lcdAddress = 0x27; // LCD IIC address
const int lcdCols = 16;      // LCD columns
const int lcdRows = 2;       // LCD rows
const char lcdBlank[] = {"                "};
LiquidCrystal_I2C lcd(lcdAddress, lcdCols, lcdRows); // create lcd object

const int servoNum = sizeof(servoPin) / sizeof(servoPin[0]); // number of servos/PWM pins
Servo servo[servoNum]; // array of Servo objects/instances
#define DROP 0  // 0 degrees to drop
#define HOME 90 // 90 degrees is home

int buttonState[4]; // array of buttons

void setup()
{
  for (int i = 0; i < servoNum; i++) {
    servo[i].attach(servoPin[i]);  // Initialize servos
    servo[i].write(HOME);
    pinMode(buttonPin[i], INPUT_PULLUP);  // buttons HIGH when not pressed, LOW when pressed
  }

  // Initialize LCD screen
  lcd.begin(lcdCols, lcdRows);
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Press a button.");
}

void loop() {
  for (int i = 0; i < servoNum; i++) {
    buttonState[i] = digitalRead(buttonPin[i]); // read all button states
    if (!buttonState[i]) { // if LOW state
      buttonState[i] == HIGH; // reset state to HIGH
      lcd.setCursor(0, 1); // place the cursor
      lcd.print("Button ");
      lcd.print(i + 1); // display human-readable button number
      lcd.print(" pressed.");
      moveServo(servo[i]); // move the servo
    }
  }
}

void moveServo(Servo servo) {
  servo.write(DROP); // drop the item
  delay(500); // Adjust the delay time as needed
  servo.write(HOME); // return servo home
  lcd.setCursor(0, 1);
  lcd.print(lcdBlank); // blank line
}