2

so here is the issue. I have a mega 1280, a handful of neopixels and a HC-SR04. I am using the New Ping library (tried both 1.5 and 1.6). I am trying to make a device that will tell my mom when she has pulled into the garage enough so she don't get hit by the garage door AND doesn't run into the wall. I wrote a bunch of code to handle the lights and what colors to do when and this part works perfectly. My issue is with the sensor. It works great except for 2 specific conditions.

  1. if you are too close. I normally either cover one of the 2 sonic sensor part or just barley touch then with a piece of cardboard (or anything for that matter

  2. If you go outside the scope of the max distance

when either of this are met, the ping sensor stops clicking and it just returns all 0's for the output. I am including the code less the part for the lights because even if i remove that part of the code, it still fails on those 2 conditions.

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#include <NewPing.h>
#define TRIGGER_PIN  52
#define ECHO_PIN     53
#define MAX_DISTANCE 120
#define PIN 6
#define LEDNUM 8

int codeFact = (MAX_DISTANCE / (LEDNUM * 3));
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDNUM, PIN, NEO_RGB +     NEO_KHZ800);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  strip.begin();
    strip.show(); 
    for(uint16_t i=0; i<strip.numPixels(); i++) 
    {
      strip.setPixelColor(i, strip.Color(0, 255, 0));
      strip.show();
      delay(50);
    }
    for(uint16_t i=0; i<strip.numPixels(); i++) 
    {
      strip.setPixelColor(i, strip.Color(255, 255, 0));
      strip.show();
      delay(50);
    }
    for(uint16_t i=0; i<strip.numPixels(); i++) 
    {
      strip.setPixelColor(i, strip.Color(255, 0, 0));
      strip.show();
      delay(50);
    }
    for(uint16_t i=0; i<strip.numPixels(); i++) 
    {
      strip.setPixelColor(i, strip.Color(0, 0, 0));
      strip.show();
      delay(50);
    }

Serial.begin(115200);
}

void loop() 
{
 delay(2000);
  int uS = sonar.ping();
  int dis = uS / US_ROUNDTRIP_CM;
  Serial.println(dis);
  /*
  Light code normally goes here
  */
}
9
  • What is the problem with exceeding the maximum distance (which you have as 120 cm)? The library is supposed to return NO_ECHO which is zero, which is what you say it is doing. As for the minimum distance, possibly the echo return is so short it can't be measured, thus it times out, returning NO_ECHO.
    – Nick Gammon
    Jul 3 '15 at 22:32
  • my issue is once it reaches these conditions, even if you bring some in the 1 - 120cm range, it does nothing at all...still returns 0's...you have to shut off the device and turn it back on to get it to work again.
    – Guy Cothal
    Jul 3 '15 at 22:35
  • Let's tackle this from a different direction. Once it fails, does pressing the Reset button on the Arduino help, or not help? (Without powering off the sensor)
    – Nick Gammon
    Jul 3 '15 at 23:21
  • yes...the reset does the trick (it resets everything and it starts working again
    – Guy Cothal
    Jul 3 '15 at 23:28
  • So just to clarify, once you get too close/far it just prints 0 every 2 seconds, regardless of where the sensor is? Or prints nothing at all?
    – Nick Gammon
    Jul 4 '15 at 1:13
1

The HC-SR04's range is 2cm to 300 cm, if you exceed this range or come below this range, which is exactly what you are doing, the utrasonic sensor will not receive any echo and hence there will be no data on the output. If you bring it too close and since both the transmitter and receiver located close to each other, the receiver will not receive any ultrasonic. Put a function that checks if the ultrasonic comes down to 2-3. If it becomes true beep a buzzer that tells you that you are too close. Don't let it reach 0.

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.