Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int servoPin = 3;
- #define trigPin 13
- #define echoPin 12
- #define led 11
- #define led2 10
- Servo Serv;
- void setup() {
- Serial.begin (9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(led, OUTPUT);
- pinMode(led2, OUTPUT);
- Serv.attach(servoPin);
- pinMode(2, OUTPUT);
- }
- void loop() {
- long duration, distance;
- digitalWrite(trigPin, LOW); // Added this line
- delayMicroseconds(2); // Added this line
- digitalWrite(trigPin, HIGH);
- // delayMicroseconds(1000); - Removed this line
- delayMicroseconds(10); // Added this line
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration/2) / 29.1;
- Serial.println(distance);
- if(distance < 10){
- digitalWrite(2, HIGH);
- // Make servo go to 0 degrees
- Serv.write(0);
- Serv.write(180);
- delay(1000);
- }
- else{
- Serv.write(0);
- digitalWrite(2, LOW);
- }
- // if (distance < 4) { // This is where the LED On/Off happens
- // digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
- // digitalWrite(led2,LOW);
- //}
- // else {
- // digitalWrite(led,LOW);
- // digitalWrite(led2,HIGH);
- // }
- // if (distance >= 200 || distance <= 0){
- // Serial.println("Out of range");
- // }
- // else {
- // Serial.print(distance);
- // Serial.println(" cm");
- // }
- // delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement