#define survo_pin 22
#define TRIG_PIN 34
#define ECHO_PIN 35
#define SCL_pin 22
#define SDA_pin 21
int angle = 0;
boolean up= true;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
mySurvo.attack(survo_pin);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN,INPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
Serial.print(angle);
mySurvo.write(angle);
delay(100);
if (up==true){
angle+=30;
}else{
angle-=30;
}
if (angle==0||angle==180){
up=!up;
}
float distance = readDistanceCM();
bool isNearby = distance < 100;
digitalWrite(LED_BUILTIN, isNearby);
Serial.print("Measured distance: ");
Serial.println(readDistanceCM());
}