Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: Arduino Uno
- - Source Code created on: 2025-06-12 06:56:08
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The LED lights up when the distance is less than */
- /* 10 and if the distance increases, the LED goes */
- /* out.The servo rotates according to the joystick. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> // Including the Servo library
- #include <Ultrasonic.h> // Including the Ultrasonic library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Pin definitions
- #define LED_PIN 9
- #define SERVO_PIN 10
- #define JOYSTICK_PIN 2
- #define ULTRASONIC_TRIG_PIN 3
- #define ULTRASONIC_ECHO_PIN A0
- #define JOYSTICK_X_PIN A1
- Servo servo; // Create a Servo object
- Ultrasonic ultrasonic(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN); // Create an Ultrasonic object
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(LED_PIN, OUTPUT);
- servo.attach(SERVO_PIN);
- pinMode(JOYSTICK_PIN, INPUT);
- Serial.begin(9600); // Initialize serial communication for debugging
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- int joystickX = analogRead(JOYSTICK_X_PIN);
- int servoAngle = map(joystickX, 0, 1023, 0, 180);
- servo.write(servoAngle);
- unsigned int distance = ultrasonic.read(); // Read distance from ultrasonic sensor
- Serial.print("Distance: ");
- Serial.println(distance);
- if (distance < 10) {
- digitalWrite(LED_PIN, HIGH); // Turn on LED
- } else {
- digitalWrite(LED_PIN, LOW); // Turn off LED
- }
- delay(100);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement