0

I would like to make an alarm system using ultrasonic sensor.

My aim is to blink an LED for ever once ultrasonic sensor detect an object is more than 30 cm away.

But the problem is that my code some time fail to detect object accurate distance and even LED blinks but when ultrasonic sensor off LED becomes off.

I am using timer to on the ultrasonic sensor for 2 minutes (timer code not attached) so within that period if it detect an object is more than 30 cm away it blinks the LED till the power is cut.

int echoPin = 10; // pin 10 for the echo 

int trigPin = 9; // pin 9 for trigger
int duration, inches, cm; // Establish variables for duration of the ping, and the distance result
#include <NewPing.h> // Library for improved ultrasonic sensor readings

#include <SoftwareSerial.h>

NewPing sonar(trigPin, echoPin,30); // sets up the sonar function and limits distance to 30 cm

int ledpin=7;
// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 250;           // interval at which to blink (milliseconds

void setup() {
    pinMode(ledpin,OUTPUT);
    Serial.begin(9600); //sets up serial monitor
}

void loop() {

    delay(50);

    Serial.print("Ping: ");
    Serial.print(sonar.ping_cm());
    Serial.println("cm");

    //warning
    Serial.println("cm");

    //warning
    if(sonar.ping_cm()>=30)
    {
      unsigned long currentMillis = millis();

      if (currentMillis - previousMillis >= interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;

        // if the LED is off turn it on and vice-versa:
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }

        // set the LED with the ledState of the variable:
        digitalWrite(ledpin, ledState);
      }

    }
}
0
1

You are initiating the NewPing library with NewPing sonar(trigPin, echoPin,30);. The last parameter is the maximum distance in cm, that will be measured. On the Arduino Playground site of the library it says

sonar.ping_cm(); - Send a ping, returns the distance in centimeters or 0 (zero) if no ping echo within set distance limit

You're not handling the special case of no ping (in the defined limit), which occurs everytime you exceed 30cm in your case, and your value cannot get greater than 30 cm. You can change the if-statement for your warning from

if(sonar.ping_cm()>=30)

to

if(sonar.ping_cm()==0)
6
  • I follow your suggestion its working but when placing the hand near about 10 cm led some time blinking and some time not although when placing the hand more than 10 cm its blinking.But I want when object is 0 to 29 cm away led should not blink and when object is 30+ cm away led should blink and once led start to blink it continue to blink even when object comes near of it. – Josephazim Feb 14 '18 at 15:54
  • Can you confirm, that the distance is read correctly from the sensor (e.g. using the serial monitor)? – chrisl Feb 14 '18 at 16:04
  • yes the distance is correctly showing. – Josephazim Feb 14 '18 at 16:12
  • 1
    I'm not sure, if that is the error, but on the site it also says "Ping sensors consistently and reliably at up to 30 times per second. " You use two consecutive calls to the ´ping_cm()´ function, which follow up on each other very quickly. Try to use it only once and save it in the cm variable. – chrisl Feb 14 '18 at 16:25
  • Led is blinking but when an object is away more than 10 cm from the ultrasonic sensor led is blinking fast and hence when object come closer led is blinking slowly and even some time did not blink.But I want when object is 0 to 29 cm away led should not blink and when object is 30+ cm away led should blink and once led start to blink it continue to blink even when object comes near of it. – Josephazim Feb 14 '18 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.