safwan092

US+LDR

Feb 7th, 2021
643
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define echoPin 2
  2. #define trigPin 3
  3. #define ledPin 13
  4. #define ldrPin A0
  5.  
  6. long duration;
  7. int distance;
  8.  
  9. void setup() {
  10.   pinMode(trigPin, OUTPUT);
  11.   pinMode(echoPin, INPUT);
  12.   pinMode(ledPin, OUTPUT);
  13.   pinMode(ldrPin, INPUT);
  14.   Serial.begin(9600);
  15. }
  16.  
  17. void loop() {
  18.   US();
  19.   LDR();
  20. }
  21.  
  22. void US() {
  23.   digitalWrite(trigPin, LOW);
  24.   delayMicroseconds(2);
  25.   digitalWrite(trigPin, HIGH);
  26.   delayMicroseconds(10);
  27.   digitalWrite(trigPin, LOW);
  28.   duration = pulseIn(echoPin, HIGH);
  29.   distance = duration * 0.034 / 2;
  30.   Serial.print("Distance: ");
  31.   Serial.print(distance);
  32.   Serial.println(" cm");
  33. }
  34.  
  35. void LDR() {
  36.   int ldrStatus = analogRead(ldrPin);
  37.   Serial.print(ldrStatus);
  38.   if (ldrStatus <= 200) {
  39.     digitalWrite(ledPin, HIGH);
  40.     Serial.println("Its DARK, Turn ON the LED");
  41.   }
  42.   else {
  43.     digitalWrite(ledPin, LOW);
  44.     Serial.println("Its BRIGHT, Turn OFF the LED");
  45.   }
  46. }
  47.  
RAW Paste Data