Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <LiquidCrystal.h>
- Servo myservo;
- LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
- int pos = 0; // initial position of the servo
- const int trigPin = 9;
- const int echoPin = 10;
- float distanceCm, DistanceSec,duration;
- void setup() {
- myservo.attach(11); // attach the servo motor to pin number 11
- lcd.begin(16,2); // Initializes the Lcd Interface with their Dimensions
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- DistanceSec=20;
- }
- void loop(){
- // Make servo go to 0 degrees
- myservo.write(0);
- delay(1000);
- // Make servo go to 20 degrees
- myservo.write(20);
- delay(1000);
- // Make servo go to 40 degrees
- myservo.write(40);
- delay(1000);
- // Make servo go to 60 degrees
- myservo.write(60);
- delay(1000);
- // Make servo go to 80 degrees
- myservo.write(80);
- delay(1000);
- // Make servo go to 100 degrees
- myservo.write(100);
- delay(1000);
- // Make servo go to 120 degrees
- myservo.write(120);
- delay(1000);
- // Make servo go to 140 degrees
- myservo.write(140);
- delay(1000);
- // Make servo go to 160 degrees
- myservo.write(160);
- delay(1000);
- // Make servo go to 180 degrees
- myservo.write(180);
- delay(1000);{ // moves from 0 degree to 180 degree
- // in steps of 1 degree
- myservo.write(pos); // Program the Servo to go to the position (pos)
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH); // delays 10 micro seconds
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distanceCm= duration*0.034/2;
- if (distanceCm <= DistanceSec)
- {
- if(distanceCm <= DistanceSec/2)
- {
- lcd.setCursor(0,0); // position is at 0,0
- lcd.print("Distance:"); // Prints "Distance" on the LCD Dispaly
- lcd.print(distanceCm); // Prints the value in cm on LCD Display
- lcd.print("cm"); // Prints cm on LCD Display
- delay(10);
- lcd.setCursor(0,1);
- lcd.print("Angle:");
- lcd.print(pos);
- lcd.print("deg");
- delay(2000);
- }
- else
- {
- digitalWrite(12, HIGH);
- delay(80);
- digitalWrite(12, LOW);
- lcd.setCursor(0,0); // position is at 0,0
- lcd.print("Distance:"); // prints 'Distance" on the LCD Display
- lcd.print(distanceCm); // Prints the value in cm on LCD
- lcd.print("cm"); // Prints cm on LCD Display
- delay(10); // delays by 10 micro sec
- lcd.setCursor(0,1);
- lcd.print("Angle:");
- lcd.print(pos);
- lcd.print("deg");
- delay(2000);
- }
- }
- else{
- digitalWrite(12, LOW);
- }
- lcd.setCursor(0,0); // position of the cursor is at 0,0
- lcd.print("Distance:"); // Prints "Distance" on the LCD Display
- lcd.print(distanceCm); // Prints value on the LCD DisplayLCD
- lcd.print("cm"); // Prints value in cm on the LCD Display
- delay(10);
- lcd.setCursor(0,1);
- lcd.print("Angle:");
- lcd.print(pos);
- lcd.print("deg");
- delay(80); // waits for 80 mili secound for the servo to look for its position
- }
- // Make servo go to 0 degrees
- myservo.write(0);
- delay(1000);
- // Make servo go to 20 degrees
- myservo.write(20);
- delay(1000);
- // Make servo go to 40 degrees
- myservo.write(40);
- delay(1000);
- // Make servo go to 60 degrees
- myservo.write(60);
- delay(1000);
- // Make servo go to 80 degrees
- myservo.write(80);
- delay(1000);
- // Make servo go to 100 degrees
- myservo.write(100);
- delay(1000);
- // Make servo go to 120 degrees
- myservo.write(120);
- delay(1000);
- // Make servo go to 140 degrees
- myservo.write(140);
- delay(1000);
- // Make servo go to 160 degrees
- myservo.write(160);
- delay(1000);
- // Make servo go to 180 degrees
- myservo.write(180);
- delay(1000); { // // moves from 0 degree to 180 degree
- myservo.write(pos); //
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distanceCm= duration*0.034/2;
- if (distanceCm <= DistanceSec){
- if(distanceCm <= DistanceSec/2)
- {
- lcd.setCursor(0,0); //set the cursor to 0,0 position
- lcd.print("Distance:"); // Prints "Distance" on the LCD Display
- lcd.print(distanceCm); // Prints the value on the LCD Display
- lcd.print("cm"); // Prints the value in cm on the LCD Display
- delay(10);
- lcd.setCursor(0,1);
- lcd.print("Angle:");
- lcd.print(pos);
- lcd.print("deg");
- delay(2000);
- }
- else
- {
- digitalWrite(12, HIGH);
- delay(80);
- digitalWrite(12, LOW);
- lcd.setCursor(0,0); // sets the cursor at 0,0 position
- lcd.print("Distance:"); // Prints "Distance" on LCD Display
- lcd.print(distanceCm); // Prints the value on LCD Display
- lcd.print("cm"); // Prints the value in cm on the LCD Display
- delay(10);
- lcd.setCursor(0,1);
- lcd.print("Angle:")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement