Poopoopeepe

Untitled

Oct 29th, 2023
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | Source Code | 0 0
  1. #include <Servo.h>
  2. #include <Ultrasonic.h>
  3.  
  4. Servo myservo;  // Create a servo object
  5. Ultrasonic ultrasonic(7, 6); // trig pin and echo pin respectively
  6. int servoPin = 9;  // Choose the digital pin connected to the servo
  7.  
  8. Servo myServo;
  9.  
  10. void setup() {
  11.   myservo.attach(servoPin);
  12.   Serial.begin(9600);
  13. }
  14.  
  15. void loop() {
  16.   for (int angle = 0; angle <= 180; angle++) {
  17.     myservo.write(angle);
  18.     delay(100);
  19.  
  20.     int distance = ultrasonic.read();
  21.     // trig to get x and y values based on the distance in cm of the output
  22.     float x = distance * cos(radians(angle));
  23.     float y = distance * sin(radians(angle));
  24.  
  25.     // Output X and Y in CSV format
  26.     Serial.print(x);
  27.     Serial.print(",");
  28.     Serial.println(y);
  29.   }
  30.   // If its too slow for you change this :)
  31.   delay(10000);
  32.  
  33.   for (int angle = 180; angle >= 0; angle--) {
  34.     myservo.write(angle);
  35.     delay(100);
  36.  
  37.     int distance = ultrasonic.read();
  38.     float x = distance * cos(radians(angle));
  39.     float y = distance * sin(radians(angle));
  40.  
  41.     // Output X and Y in CSV format
  42.     Serial.print(x);
  43.     Serial.print(",");
  44.     Serial.println(y);
  45.   }
  46.  
  47.   delay(1000);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment