#include <ESP32Servo.h> // Allow ESP32 to control servo
Servo myservo; // Create Servo Object to control a servo
const int ServoPin = 23; // Setting pin 23 for servo
const int buzzerPin = 5; // Buzzer pin
unsigned long ADC_previous_millis = 0;
const unsigned int ADC_interval = 100; // Interval to check ADC
int angle = 0; // Set the initial angle of Servo
const unsigned int incrementRate = 2; // Increment rate
void setup()
{
Serial.begin(115200); // Begin Serial Monitor
myservo.attach(ServoPin); // Set up ServoPin using Servo library
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
unsigned long current_millis = millis();
if (current_millis - ADC_previous_millis >= ADC_interval)
{
ADC_previous_millis = current_millis;
int ADC_value = analogRead(A5); // Variable to read the value from analog pin
Serial.print("ADC value now is ");
Serial.println(ADC_value);
if (ADC_value < 2500)
{
// Move servo from current angle to 180 degrees
tone(buzzerPin, 1000); // Turn on the buzzer
for (int angle = 90; angle <= 180; angle += 1)
{
myservo.write(angle);
Serial.print("Angle: 1 ");
Serial.println(angle);
delay(100); // Wait for the servo to reach the position
}
noTone(buzzerPin); // Turn off the buzzer after reaching 180 degrees
}
else if (ADC_value >= 2500 && ADC_value < 4000)
{
// Move servo from current angle to 0 degrees
// Sweep from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle -= 1) {
myservo.write(angle);
Serial.print("Angle: 0");
Serial.println(angle);
delay(100); // Wait for the servo to reach the position
}
delay(1000); // Wait before repeating the sweep
}
}
}