//ESP32 S Led_Servo_cu_PC bluetooth

  #include <ESP32Servo.h> 

#include "BluetoothSerial.h"  
BluetoothSerial SerialBT;
  
  Servo myservo;  
  #define ledPin 2
  int state = 0;
  
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(-1);
  
  void setup() {
    
  Serial.begin(115200);
   SerialBT.begin("ESP32 BT");
   
  myservo.attach(13);
  pinMode(ledPin, OUTPUT); 
  digitalWrite(ledPin, LOW); 

  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("ESP32 BT Servo Led");
  display.display();
  delay(1000); 
      }
      
  void loop() {
  
  if(SerialBT.available() > 0)
  { state = SerialBT.read(); }
  if (state == '0') 
  { digitalWrite(ledPin, LOW); 
  
  SerialBT.println("LED: Rx OFF"); 
  // Arduino trimite spre computer mesajul " LED: Rx OFF "
  state = 0;
    }
  else if (state == '1') { digitalWrite(ledPin, HIGH);
 
  SerialBT.println("LED: Rx ON"); //trimite bine ianapoi
  state = 0;
    } 
    
  byte byteRead;
  if (SerialBT.available()) 
  { byteRead = SerialBT.read(); 
  myservo.write(byteRead);  

  Serial.println("byteRead"); 
  Serial.println(byteRead); 

  //citeste datele analogice de la slider şi roteşte servo cu valoare byteRead 
    delay(10); 
/*
   display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("byteRead =");
  display.print(byteRead);  
  display.display();
  */
        }
    }
  
