safwan092

code test 003

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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×