#include <Wire.h>
#include <Servo.h>

const int pinLed8 = 8;
const int pinLed9 = 9;
const int servoPin = 3;

Servo myServo;
int servoPosition = 90;  // Initial servo position in degrees

void setup() {
  Wire.begin(9); // Slave 1 address
  Wire.onReceive(receiveEvent); 
  pinMode(pinLed8, OUTPUT);
  pinMode(pinLed9, OUTPUT);
  myServo.attach(servoPin);
  myServo.write(servoPosition);  // Set the initial position of the servo
}

void loop() {
  // No code needed in the loop for a slave
}

void receiveEvent(int howMany) {
  while (Wire.available()) {
    char data = Wire.read();
    
    // Control the servo and LEDs for slave 1
    if (data == 'A') {
      servoPosition = min(servoPosition + 5, 180);
      myServo.write(servoPosition);
      digitalWrite(pinLed8, HIGH);
      digitalWrite(pinLed9, LOW);
    } else if (data == 'R') {
      servoPosition = max(servoPosition - 5, 0);
      myServo.write(servoPosition);
      digitalWrite(pinLed8, LOW);
      digitalWrite(pinLed9, HIGH);
    }
  }
}