#include <Servo.h>

Servo thumb, index, middle, ring, pinky;

void setup() {
  // Matches Python baud rate
  Serial.begin(115200); 
  
  // Pins from your schematic
  thumb.attach(11);
  index.attach(10);
  middle.attach(9);
  ring.attach(6);
  pinky.attach(5);

  // Initial Boot Position (All Open)
  thumb.write(180);
  index.write(180);
  middle.write(0);
  ring.write(180);
  pinky.write(60);
  
  delay(500);
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\r');
    command.trim();

    // Mapping based on your specific angle limits
    if (command == "T_O") thumb.write(180);
    if (command == "T_C") thumb.write(0);
    
    if (command == "I_O") index.write(180);
    if (command == "I_C") index.write(0);
    
    if (command == "M_O") middle.write(0);
    if (command == "M_C") middle.write(180);
    
    if (command == "R_O") ring.write(180);
    if (command == "R_C") ring.write(0);
    
    if (command == "P_O") pinky.write(60);
    if (command == "P_C") pinky.write(180);
  }
}