Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <RH_ASK.h>
- #include <SPI.h> // Not actualy used but needed to compile
- // defines pins numbers for ultrasonic
- const int trigPin = 2;
- const int echoPin = 3;
- const int buzzer = 12;//used to be 11 now it is 12 [11 for receiver]
- const int ledPin = 13;
- // defines pins numbers for LDR
- const int ldr_pin = 7;
- const int led_pin = 4;
- // Project by - Be innovative with Prasad
- // title - ultrasonic sensor project with buzzer and Arduino
- // defines variables
- long duration;
- int distance;
- int safetyDistance;
- int mss = 0;
- uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
- uint8_t buflen = sizeof(buf);
- RH_ASK driver;
- void setup() {
- //ultrasonic
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- pinMode(buzzer, OUTPUT);
- pinMode(ledPin, OUTPUT);
- Serial.begin(9600); // Starts the serial communication
- // put your setup code here, to run once:LDR
- pinMode(ldr_pin, INPUT);
- pinMode(led_pin, OUTPUT);
- Serial.begin(9600);
- if (!driver.init())Serial.println("init failed");
- }
- void loop() {
- int j = 5;
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(echoPin, HIGH);
- // Calculating the distance
- distance = duration * 0.034 / 2;
- Serial.print("Distance: ");
- Serial.println(distance);
- safetyDistance = distance;
- //Prints the distance on the Serial Monitor
- if (safetyDistance <= 150 && safetyDistance >= 5) // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
- {
- j = safetyDistance * j;
- digitalWrite(buzzer, HIGH);
- digitalWrite(ledPin, HIGH);
- delay(j);
- digitalWrite(buzzer, LOW);
- digitalWrite(ledPin, LOW);
- delay(j);
- }
- delay(1);
- // put your main code here, to run repeatedly:LDR
- if ( digitalRead( ldr_pin ) == 1) {
- if (driver.recv(buf, &buflen)) // Non-blocking
- {
- int i;
- // Message with a good checksum received, dump it.
- driver.printBuffer("Got:", buf, buflen);
- mss = atoi(buf);
- }
- Serial.print("mss is set to :");
- Serial.println(mss);
- if (mss == 1) {
- digitalWrite( led_pin, LOW);
- }
- else {
- digitalWrite( led_pin, HIGH);
- }
- }
- else {
- digitalWrite( led_pin , LOW);
- }
- Serial.println( digitalRead( ldr_pin ));
- delay(100);
- } // Project by - Be innovative with Prasad
- // title - ultrasonic sensor project with buzzer and Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement