SHARE
TWEET

Untitled

a guest Jun 16th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #include <LiquidCrystal.h>
  3.  
  4. Servo myservo;
  5. LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
  6.  
  7. int pos = 0; // initial position of the servo  
  8. const int trigPin = 9;
  9. const int echoPin = 10;
  10.  
  11. float distanceCm, DistanceSec,duration;
  12.  
  13. void setup() {
  14. myservo.attach(11); // attach the servo motor to pin number 11
  15. lcd.begin(16,2); // Initializes the Lcd Interface with their Dimensions
  16. pinMode(trigPin, OUTPUT);
  17. pinMode(echoPin, INPUT);
  18. DistanceSec=20;
  19. }
  20. void loop(){
  21.  
  22. // Make servo go to 0 degrees
  23.    myservo.write(0);
  24.    delay(1000);
  25.    // Make servo go to 20 degrees
  26.    myservo.write(20);
  27.    delay(1000);
  28.    // Make servo go to 40 degrees
  29.    myservo.write(40);
  30.    delay(1000);
  31.    // Make servo go to 60 degrees
  32.    myservo.write(60);
  33.    delay(1000);
  34.    // Make servo go to 80 degrees
  35.    myservo.write(80);
  36.    delay(1000);
  37.    // Make servo go to 100 degrees
  38.    myservo.write(100);
  39.    delay(1000);
  40.    // Make servo go to 120 degrees
  41.    myservo.write(120);
  42.    delay(1000);
  43.    // Make servo go to 140 degrees
  44.    myservo.write(140);
  45.    delay(1000);
  46.    // Make servo go to 160 degrees
  47.    myservo.write(160);
  48.    delay(1000);
  49.    // Make servo go to 180 degrees
  50.    myservo.write(180);
  51.    delay(1000);{ // moves from 0 degree to 180 degree
  52. // in steps of 1 degree
  53. myservo.write(pos); // Program the Servo to go to the position (pos)
  54. digitalWrite(trigPin, LOW);
  55. delayMicroseconds(2);
  56. digitalWrite(trigPin, HIGH); // delays 10 micro seconds
  57. delayMicroseconds(10);
  58. digitalWrite(trigPin, LOW);
  59. duration = pulseIn(echoPin, HIGH);
  60. distanceCm= duration*0.034/2;
  61. if (distanceCm <= DistanceSec)
  62. {
  63. if(distanceCm <= DistanceSec/2)
  64. {
  65. lcd.setCursor(0,0); // position is at 0,0
  66. lcd.print("Distance:"); // Prints "Distance" on the LCD Dispaly
  67. lcd.print(distanceCm); // Prints the value in cm on LCD Display
  68. lcd.print("cm"); // Prints cm on LCD Display
  69. delay(10);
  70. lcd.setCursor(0,1);
  71. lcd.print("Angle:");
  72. lcd.print(pos);
  73. lcd.print("deg");
  74. delay(2000);
  75. }
  76. else
  77. {
  78. digitalWrite(12, HIGH);
  79. delay(80);
  80. digitalWrite(12, LOW);
  81. lcd.setCursor(0,0); // position is at 0,0
  82. lcd.print("Distance:"); // prints 'Distance" on the LCD Display
  83. lcd.print(distanceCm); // Prints the value in cm on LCD
  84. lcd.print("cm"); // Prints cm on LCD Display
  85. delay(10); // delays by 10 micro sec
  86. lcd.setCursor(0,1);
  87. lcd.print("Angle:");
  88. lcd.print(pos);
  89. lcd.print("deg");
  90. delay(2000);
  91. }
  92. }
  93. else{
  94. digitalWrite(12, LOW);
  95. }
  96. lcd.setCursor(0,0); // position of the cursor is at 0,0
  97. lcd.print("Distance:"); // Prints "Distance" on the LCD Display
  98. lcd.print(distanceCm); // Prints value on the LCD DisplayLCD
  99. lcd.print("cm"); // Prints value in cm on the LCD Display
  100. delay(10);
  101. lcd.setCursor(0,1);
  102. lcd.print("Angle:");
  103. lcd.print(pos);
  104. lcd.print("deg");
  105. delay(80); // waits for 80 mili secound for the servo to look for its position
  106. }
  107. // Make servo go to 0 degrees
  108.    myservo.write(0);
  109.    delay(1000);
  110.    // Make servo go to 20 degrees
  111.    myservo.write(20);
  112.    delay(1000);
  113.    // Make servo go to 40 degrees
  114.    myservo.write(40);
  115.    delay(1000);
  116.    // Make servo go to 60 degrees
  117.    myservo.write(60);
  118.    delay(1000);
  119.    // Make servo go to 80 degrees
  120.    myservo.write(80);
  121.    delay(1000);
  122.    // Make servo go to 100 degrees
  123.    myservo.write(100);
  124.    delay(1000);
  125.    // Make servo go to 120 degrees
  126.    myservo.write(120);
  127.    delay(1000);
  128.    // Make servo go to 140 degrees
  129.    myservo.write(140);
  130.    delay(1000);
  131.    // Make servo go to 160 degrees
  132.    myservo.write(160);
  133.    delay(1000);
  134.    // Make servo go to 180 degrees
  135.    myservo.write(180);
  136.    delay(1000);  { // // moves from 0 degree to 180 degree
  137. myservo.write(pos); //
  138. digitalWrite(trigPin, LOW);
  139. delayMicroseconds(2);
  140. digitalWrite(trigPin, HIGH);
  141. delayMicroseconds(10);
  142. digitalWrite(trigPin, LOW);
  143. duration = pulseIn(echoPin, HIGH);
  144. distanceCm= duration*0.034/2;
  145. if (distanceCm <= DistanceSec){
  146. if(distanceCm <= DistanceSec/2)
  147. {
  148. lcd.setCursor(0,0); //set the cursor to 0,0 position
  149. lcd.print("Distance:"); // Prints "Distance" on the LCD Display
  150. lcd.print(distanceCm); // Prints the value on the LCD Display
  151. lcd.print("cm"); // Prints the value in cm on the LCD Display
  152. delay(10);
  153. lcd.setCursor(0,1);
  154. lcd.print("Angle:");
  155. lcd.print(pos);
  156. lcd.print("deg");
  157. delay(2000);
  158. }
  159. else
  160. {
  161. digitalWrite(12, HIGH);
  162. delay(80);
  163. digitalWrite(12, LOW);
  164. lcd.setCursor(0,0); // sets the cursor at 0,0 position
  165. lcd.print("Distance:"); // Prints "Distance" on LCD Display
  166. lcd.print(distanceCm); // Prints the value on LCD Display
  167. lcd.print("cm"); // Prints the value in cm on the LCD Display
  168. delay(10);
  169. lcd.setCursor(0,1);
  170. lcd.print("Angle:")
  171. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top