safwan092

Rx-Code-OG

Apr 15th, 2021
911
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <RH_ASK.h>
  2. #include <SPI.h> // Not actualy used but needed to compile
  3.  
  4. // defines pins numbers
  5. const int trigPin = 9;
  6. const int echoPin = 10;
  7. const int buzzer = 12;//used to be 11 now it is 12 [11 for receiver]
  8. const int ledPin = 2;
  9. const int ldr_pin = 7;
  10. const int led_pin = 13;
  11. //  Project by - Be innovative with Prasad
  12. //   title - ultrasonic sensor project with buzzer and Arduino
  13. // defines variables
  14. long duration;
  15. int distance;
  16. int safetyDistance;
  17.  
  18. int mss = 0;
  19. uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  20. uint8_t buflen = sizeof(buf);
  21. RH_ASK driver;
  22.  
  23. void setup() {
  24.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  25.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  26.   pinMode(buzzer, OUTPUT);
  27.   pinMode(ledPin, OUTPUT);
  28.   Serial.begin(9600); // Starts the serial communication
  29.   // put your setup code here, to run once:
  30.   pinMode(ldr_pin, INPUT);
  31.   pinMode(led_pin, OUTPUT);
  32.   Serial.begin(9600);
  33.   if (!driver.init())Serial.println("init failed");
  34. }
  35.  
  36.  
  37. void loop() {
  38.   // Clears the trigPin
  39.   digitalWrite(trigPin, LOW);
  40.   delayMicroseconds(2);
  41.  
  42.   // Sets the trigPin on HIGH state for 10 micro seconds
  43.   digitalWrite(trigPin, HIGH);
  44.   delayMicroseconds(10);
  45.   digitalWrite(trigPin, LOW);
  46.  
  47.   // Reads the echoPin, returns the sound wave travel time in microseconds
  48.   duration = pulseIn(echoPin, HIGH);
  49.  
  50.   // Calculating the distance
  51.   distance = duration * 0.034 / 2;
  52.  
  53.   safetyDistance = distance;
  54.   if (safetyDistance <= 20)   // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
  55.   {
  56.     digitalWrite(buzzer, HIGH);
  57.     digitalWrite(ledPin, HIGH);
  58.   }
  59.   else {
  60.     digitalWrite(buzzer, LOW);
  61.     digitalWrite(ledPin, LOW);
  62.   }
  63.  
  64.   // Prints the distance on the Serial Monitor
  65.   Serial.print("Distance: ");
  66.   Serial.println(distance);
  67.   // put your main code here, to run repeatedly:
  68.   if ( digitalRead( ldr_pin ) == 1) {
  69.     if (driver.recv(buf, &buflen)) // Non-blocking
  70.     {
  71.       int i;
  72.  
  73.       // Message with a good checksum received, dump it.
  74.       driver.printBuffer("Got:", buf, buflen);
  75.       mss = atoi(buf);
  76.     }
  77.     Serial.print("mss is set to :");
  78.     Serial.println(mss);
  79.     if (mss == 1) {
  80.       digitalWrite( led_pin, LOW);
  81.     }
  82.     else {
  83.       digitalWrite( led_pin, HIGH);
  84.     }
  85.  
  86.   }
  87.   else {
  88.     digitalWrite( led_pin , LOW);
  89.   }
  90.  
  91.   Serial.println( digitalRead( ldr_pin ));
  92.   delay(100);
  93. }                                   //  Project by - Be innovative with Prasad
  94. //   title - ultrasonic sensor project with buzzer and Arduino
RAW Paste Data