Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myServo;
- const int trigPin = 12;
- const int echoPin = 11;
- const int ledPinGreen = 6;
- const int ledPinYellow = 5;
- const int ledPinRed = 4;
- long duration;
- int distance;
- int potVal;
- int angle;
- void setup()
- {
- // put your setup code here, to run once:
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(ledPinRed, OUTPUT);
- pinMode(ledPinYellow, OUTPUT);
- pinMode(ledPinGreen, OUTPUT);
- myServo.attach(9);
- Serial.begin(9600);
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance= duration*0.034/2;
- Serial.print("Distance: ");
- Serial.println(distance);
- if(distance <=10)
- {
- digitalWrite(ledPinRed, HIGH);
- digitalWrite(ledPinYellow, LOW);
- digitalWrite(ledPinGreen, LOW);
- myServo.write(90);
- }
- if(distance > 10 && distance <=20)
- {
- digitalWrite(ledPinRed, LOW);
- digitalWrite(ledPinYellow, HIGH);
- digitalWrite(ledPinGreen, LOW);
- myServo.write(180);
- }
- if(distance > 20)
- {
- digitalWrite(ledPinRed, LOW);
- digitalWrite(ledPinYellow, LOW);
- digitalWrite(ledPinGreen, HIGH);
- myServo.write(180);
- }
- }
Add Comment
Please, Sign In to add comment