Advertisement
pleasedontcode

**Servo Control** rev_01

Jun 12th, 2025
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Servo Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-06-12 06:56:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The LED lights up when the distance is less than */
  21.     /* 10 and if the distance increases, the LED goes */
  22.     /* out.The servo rotates according to the joystick. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Servo.h>  // Including the Servo library
  29. #include <Ultrasonic.h>  // Including the Ultrasonic library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // Pin definitions
  36. #define LED_PIN 9
  37. #define SERVO_PIN 10
  38. #define JOYSTICK_PIN 2
  39. #define ULTRASONIC_TRIG_PIN 3
  40. #define ULTRASONIC_ECHO_PIN A0
  41. #define JOYSTICK_X_PIN A1
  42.  
  43. Servo servo;  // Create a Servo object
  44. Ultrasonic ultrasonic(ULTRASONIC_TRIG_PIN, ULTRASONIC_ECHO_PIN); // Create an Ultrasonic object
  45.  
  46. void setup(void)
  47. {
  48.     // put your setup code here, to run once:
  49.     pinMode(LED_PIN, OUTPUT);
  50.     servo.attach(SERVO_PIN);
  51.     pinMode(JOYSTICK_PIN, INPUT);
  52.     Serial.begin(9600);  // Initialize serial communication for debugging
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     // put your main code here, to run repeatedly:
  58.     int joystickX = analogRead(JOYSTICK_X_PIN);
  59.     int servoAngle = map(joystickX, 0, 1023, 0, 180);
  60.     servo.write(servoAngle);
  61.  
  62.     unsigned int distance = ultrasonic.read(); // Read distance from ultrasonic sensor
  63.  
  64.     Serial.print("Distance: ");
  65.     Serial.println(distance);
  66.  
  67.     if (distance < 10) {
  68.         digitalWrite(LED_PIN, HIGH);  // Turn on LED
  69.     } else {
  70.         digitalWrite(LED_PIN, LOW);   // Turn off LED
  71.     }
  72.    
  73.     delay(100);
  74. }
  75.  
  76. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement