Ping sensor won't work!!

Hello everybody

I have a problem with my ping sensor, I think it is maybe the programming there are wrong, but I can’t find where it should be. Can you please help me?

I have 4 LED lights on my board and 4 opponents. Then I have a soundmaker. The Sensor work was to turn on the lights and the sound in a loop. But when I update the program the lights and sound is going in a loop constant and will not stop.

I want it to turn on when the sensor have 5 cm from a object. But I can’t find out how to program it.

Here is my program:

// constants won’t change. Used here to set a pin number :
const int ledPin = 13; // the number of the LED pin
const int lyd = 7; // højtaler på ingang D7

// 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 = 400; // interval at which to blink (milliseconds)
bool button_state = false;
bool isBlinking = false;
int last_button_value = 0;
int button_value;

const int trigPin = 9;
const int echoPin = 10;

long duration;
int distanceCm, distanceInch;

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
// Lyd skal være en udgang
pinMode(lyd, OUTPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop() {
// here is where you’d put code that needs to be running all the time.

// check to see if it’s time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

// Serial.println(button_value);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distanceCm = duration * 0.034 / 2;
if(distanceCm <15) {
button_value = 1;
} else {
button_value = 0;

if(button_value == 1&&last_button_value == 0) {
isBlinking = !isBlinking;
last_button_value = 1;
} else if(button_value == 0&&last_button_value == 1) {
last_button_value = 0;

if(isBlinking) {
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;
int r = analogRead(A0);
double ra = r/1023;
int f = 20000*ra;
tone(lyd, 2000, interval);
} else {
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, not ledState);
} else {
ledState = HIGH;

digitalWrite(ledPin, ledState);



Hope someone can solve it for me! or help me so I can find out :wink:

Here is a basic sketch to get your ping readings working :) I tested it to make sure it works!

I had to move the echo pin to pin 2 to use the attachInterrupt() command to use the internal interrupt Code:

const int echoPin = 2; // Changed to pin 2 for interrupt usage
const int trigPin = 9;

volatile unsigned long EchoStart;
volatile unsigned long Duration;
volatile bool AllClear = true;

float microsecondsToInches(long microseconds)
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See:
  return (float) microseconds / 74 / 2;

float microsecondsToCentimeters(long microseconds)
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (float)microseconds / 29 / 2;

void PingTrigger(int Pin)
  if (AllClear)
  { // Wait till we have all the Ping data in before calculating distances
    AllClear = false;
    digitalWrite(Pin, LOW);
    digitalWrite(Pin, HIGH);
    digitalWrite(Pin, LOW);// Trigger another pulse

void setup()
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  [](){ // inline Lambda Function Google Lambda anonymous functions
    if (digitalRead(echoPin) == HIGH) // Ping Echo Pin Rose High
      EchoStart = micros(); // Store Start Time Echo pin went high
      Duration = micros() - EchoStart; // Calculate Duration for sound return time
      AllClear = true;
  } // End of Lambda Funciton
  , CHANGE ); //End of attachInterrupt()

void loop()
  if (AllClear) {
    char S[30];
    Serial.print(dtostrf( microsecondsToCentimeters(Duration), 6, 2, S));
    Serial.print("cm ");
    Serial.print(dtostrf( microsecondsToInches(Duration), 6, 2, S));
    Serial.print("\" ");


Note that pulseIn() returns ZERO if there is a timeout. If the ping sensor does not receive an echo it will not produce an output pulse and pulseIn() will return ZERO. A result of 0 means NO OBJECT DETECTED, not OBJECT AT DISTANCE ZERO. You are treating a zero result as an object less than 15 cm away.