/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: **Servo Control**
- Source Code NOT compiled for: ESP32 DevKit V1
- Source Code created on: 2024-11-09 06:36:25
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* smart trash bin using esp32 and ultrasonic sensor */
/* and servo motor */
/****** END SYSTEM REQUIREMENTS *****/
/* START CODE */
/****** DEFINITION OF LIBRARIES *****/
#include //https://github.com/evert-arias/EasyButton
#include //https://github.com/ErickSimoes/Ultrasonic
#include // Include Servo library for controlling servo motors
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t servomotor_PushButton_PIN_D4 = 4;
const uint8_t servomotor_HC-SR04_Echo_PIN_D14 = 14;
// Define pins for HC-SR04 sensor based on your setup
const int trigPin = 19; // Trigger pin connected to GPIO 19
const int echoPin = 18; // Echo pin connected to GPIO 18
// Define the pin for the servo motor
const int servoPin = 27; // Servo signal pin connected to GPIO 27
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t servomotor_HC-SR04_Trigger_PIN_D13 = 13;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool servomotor_HC-SR04_Trigger_PIN_D13_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float servomotor_HC-SR04_Trigger_PIN_D13_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create a Servo object
Servo myServo;
// Create an Ultrasonic object
Ultrasonic ultrasonic(trigPin, echoPin);
// Variables to store the pulse duration and distance calculated
long duration;
float distance;
void setup(void)
{
// put your setup code here, to run once:
pinMode(servomotor_PushButton_PIN_D4, INPUT_PULLUP);
pinMode(servomotor_HC-SR04_Echo_PIN_D14, INPUT);
pinMode(servomotor_HC-SR04_Trigger_PIN_D13, OUTPUT);
// Initialize serial communication at a baud rate of 115200
Serial.begin(115200);
// Attach the servo on GPIO 27
myServo.attach(servoPin);
// Set initial servo position
myServo.write(0); // Start at 0 degrees (initial position)
delay(1000); // Wait for servo to move to 0 degrees
Serial.println("Servo initialized and set to 0 degrees");
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
// Get the distance from the ultrasonic sensor
distance = ultrasonic.read(); // Read distance in cm
// Check if the distance is within the sensor's range (2 cm to 400 cm)
if (distance > 400 || distance < 2) {
Serial.println("Out of range");
} else {
// Display the distance in the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if the distance is less than 10 cm (object very close)
if (distance < 10) {
Serial.println("Object very close, moving servo to 180 degrees");
myServo.write(180); // Move servo to 180 degrees
delay(1000); // Wait for 1 second to allow the servo to move
}
// Check if the distance is between 10-50 cm (object in medium range)
else if (distance < 50) {
Serial.println("Object in medium range, moving servo to 90 degrees");
myServo.write(90); // Move servo to 90 degrees
delay(1000); // Wait for 1 second to allow the servo to move
}
// If the object is far (greater than 50 cm)
else {
Serial.println("Object far away, moving servo to 0 degrees");
myServo.write(0); // Move servo to 0 degrees
delay(1000); // Wait for 1 second to allow the servo to move
}
}
// Wait 1 second before taking the next measurement
delay(1000);
}
void updateOutputs()
{
digitalWrite(servomotor_HC-SR04_Trigger_PIN_D13, servomotor_HC-SR04_Trigger_PIN_D13_rawData);
}
/* END CODE */