Guest User

Untitled

a guest
Nov 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #define Trig_PIN 12 // Pin connect to Trig pin
  2. #define Echo_PIN 13 // Pin connect to Echo pin
  3. #define LED_PIN 11 // Pin connect to LED (Active HIGH) ** Use PIN 11, 10, 9, 6, 5 or 3 only **
  4.  
  5. void setup() {
  6. pinMode(Trig_PIN, OUTPUT);
  7. pinMode(Echo_PIN, INPUT);
  8. pinMode(LED_PIN, OUTPUT);
  9.  
  10. Serial.begin(9600);
  11. }
  12.  
  13. void loop() {
  14. // Read distance from HC-04
  15. digitalWrite(Trig_PIN, LOW);
  16. delayMicroseconds(5);
  17. digitalWrite(Trig_PIN, HIGH);
  18. delayMicroseconds(10);
  19. digitalWrite(Trig_PIN, LOW);
  20. unsigned int PulseWidth = pulseIn(Echo_PIN, HIGH);
  21. unsigned int distance = PulseWidth * 0.0173681;
  22.  
  23. Serial.print("Distance is ");
  24. Serial.print(distance);
  25. Serial.println(" cm.");
  26. // -------------------------
  27.  
  28. int val = map(distance, 2, min(10, distance), 80, 255); // resize distance form 1 - 10 to 80 - 255
  29. analogWrite(LED_PIN, val); // Set brightness of LED
  30.  
  31. delay(100);
  32. }
Add Comment
Please, Sign In to add comment