safwan092

RX-Code

Apr 1st, 2021
809
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. #define trigPin   9
  6. #define echoPin   10
  7. #define buzzer1   11 //change receiver needs Pin 11
  8. #define ledPin    2
  9. #define ldr_pin   7
  10. #define buzzer2   13
  11.  
  12. long duration;
  13. int distance;
  14. int safetyDistance;
  15. int ldrStatus;
  16. int mss = 0;
  17. uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  18. uint8_t buflen = sizeof(buf);
  19.  
  20. RH_ASK driver;
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.   pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  25.   pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  26.   pinMode(buzzer1, OUTPUT);
  27.   pinMode(ledPin, OUTPUT);
  28.   pinMode(ldr_pin, INPUT);
  29.   pinMode(buzzer2, OUTPUT);
  30.   if (!driver.init())Serial.println("init failed");
  31. }
  32.  
  33.  
  34. void loop() {
  35.   //1: Turn buzzer ON to find device
  36.   //2: Turn buzzer OFF for 5 seconds
  37.   rx433();
  38.   US();
  39.   LDR();
  40.   if (mss == 1) {
  41.     digitalWrite(buzzer1, HIGH);
  42.     digitalWrite(buzzer2, HIGH);
  43.   }
  44.   else if (mss == 2) {
  45.     digitalWrite(buzzer1, LOW);
  46.     digitalWrite(buzzer2, LOW);
  47.     delay(5000);
  48.   }
  49.   if (safetyDistance <= 20)   // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
  50.   {
  51.     digitalWrite(buzzer1, HIGH);
  52.     digitalWrite(ledPin, HIGH);
  53.   }
  54.   else if (safetyDistance > 20) {
  55.     digitalWrite(buzzer1, LOW);
  56.     digitalWrite(ledPin, LOW);
  57.   }
  58.  
  59.   if (ldrStatus == 1) {
  60.     digitalWrite(buzzer2, HIGH);
  61.   }
  62.   else if (ldrStatus == 0) {
  63.     digitalWrite(buzzer2, LOW);
  64.   }
  65.  
  66.   delay(100);
  67. }
  68.  
  69.  
  70. void US() {
  71.   digitalWrite(trigPin, LOW);
  72.   delayMicroseconds(2);
  73.   digitalWrite(trigPin, HIGH);
  74.   delayMicroseconds(10);
  75.   digitalWrite(trigPin, LOW);
  76.   duration = pulseIn(echoPin, HIGH);
  77.   distance = duration * 0.034 / 2;
  78.   safetyDistance = distance;
  79.   Serial.print("Distance: ");
  80.   Serial.println(distance);
  81.  
  82. }
  83.  
  84. void LDR() {
  85.   ldrStatus = digitalRead(ldr_pin);
  86.   Serial.print("Light: ");
  87.   Serial.println(ldrStatus);
  88. }
  89.  
  90. void rx433() {
  91.   if (driver.recv(buf, &buflen)) // Non-blocking
  92.   {
  93.     int i;
  94.  
  95.     // Message with a good checksum received, dump it.
  96.     driver.printBuffer("Got:", buf, buflen);
  97.     mss = atoi(buf);
  98.   }
  99.   Serial.print("mss is set to :");
  100.   Serial.println(mss);
  101. }
  102.  
RAW Paste Data