/********* 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: Distance Servo - Source Code compiled for: Arduino Uno - Source Code created on: 2024-02-14 01:10:05 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* the servo motor is underneath the ultrasonic and */ /* rotates 90 degrees. Ultrasonic measures the */ /* distance to an obstacle */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include // https://github.com/ErickSimoes/Ultrasonic #include // https://github.com/arduino-libraries/Servo /****** FUNCTION PROTOTYPES *****/ void setup(); void loop(); void updateOutputs(); /***** DEFINITION OF DIGITAL INPUT PINS *****/ const uint8_t ultrasonic_Echo_PIN = 3; /***** DEFINITION OF DIGITAL OUTPUT PINS *****/ const uint8_t ultrasonic_Trigger_PIN = 2; /***** DEFINITION OF PWM OUTPUT PINS *****/ const uint8_t servo_PWMSignal_PIN = 5; /***** DEFINITION OF OUTPUT RAW VARIABLES *****/ bool ultrasonic_Trigger_PIN_rawData = false; uint8_t servo_PWMSignal_PIN_rawData = 90; // Rotate at 90 degrees default /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/ float ultrasonic_Trigger_PIN_phyData = 0.0; float servo_PWMSignal_PIN_phyData = 0.0; /****** DEFINITION OF LIBRARY CLASS INSTANCES *****/ Ultrasonic ultrasonic(ultrasonic_Trigger_PIN, ultrasonic_Echo_PIN); Servo myservo; void setup() { // put your setup code here, to run once: pinMode(ultrasonic_Echo_PIN, INPUT); pinMode(ultrasonic_Trigger_PIN, OUTPUT); pinMode(servo_PWMSignal_PIN, OUTPUT); myservo.attach(servo_PWMSignal_PIN); } void loop() { // put your main code here, to run repeatedly: ultrasonic_Trigger_PIN_rawData = true; delayMicroseconds(10); ultrasonic_Trigger_PIN_rawData = false; unsigned int distance_cm = ultrasonic.read(); if (distance_cm > 0 && distance_cm < 100) { int angle = map(distance_cm, 0, 100, 0, 180); myservo.write(angle); } delay(200); }