Ultrasonic sensor randomly turns 0

usually the sensor reads the right value, but now it reads 0.

#include <NewPing.h>
#include <Bridge.h>
#include <BridgeClient.h>
#include <BridgeServer.h>
#include <BridgeSSLClient.h>
#include <BridgeUdp.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <Servo.h>

// Servo myservo; // create servo object to control a servo
// int pos = 0; // variable to store the servo position

#include <Keypad.h>
//NewPing sonar(trigger_pin, echo_pin ,[ max_cm_distance]);
NewPing sonar(9, 10 , 200);
long duration;
float distance;
float p = 3.14;
float r = 14.06;
float h = 13.14;
float volume;
const int buzzer = 8;
const byte ROWS = 4;
const byte COLS = 3;

char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};

byte rowPins[ROWS] = {11, 12, 7, 6};
byte colPins[COLS] = {5, 4, 3};
String inputString;
long inputInt;

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
Serial.begin(9600);
pinMode(buzzer, OUTPUT); // declare buzzer as output
inputString.reserve(5); //Input string has 5 digits
//myservo.attach(13);
}

void loop() {
char customKey = customKeypad.waitForKey();

if(customKey)
{
Serial.println(customKey);

if (customKey >= '0' && customKey <= '9') //If we enter a number between 0 and 9
{     
  inputString = inputString + customKey;               // Add new digit
} 
else if (customKey == '#') 

{

// for (pos = 0; pos <= 10; pos += 1) { // goes from 0 degrees to 10 degrees
// // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
if (inputString.length() > 0)
{
inputInt = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
Serial.println(inputInt);
inputString = ""; // clear input
// DO YOUR WORK HERE
while(volume<float(inputInt))
{
duration = sonar.ping();
distance = (duration / 2) * 0.0343;
volume = (h-distance)pr;
Serial.println(distance);
Serial.println(volume);
}
if(volume>float(inputInt))
{
// for (pos = 10; pos >= 0; pos -= 1) { // goes from degrees to 0 degrees
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
Serial.println("h is ");
Serial.println(distance);
Serial.print("float input ");
Serial.println(float(inputInt));
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
}
}
}
else if (customKey == '*')
{
inputString = ""; // clear input
}
}
}

Do you have a program that JUST tested the Ultrasonic sensor?

yes, it is not consistant but it does not give 0 either

Instead of Serial.print(distance);
Print the actual value of duration. It should match the output of your test program.

i tried that, however it is not consistant

I expect one of you other include files and their usage is sharing some resource with the newping library.

The easy way to find it is to step by step disable the other functions. You will eventually find which is conflicting.

What Arduino are you using? Are you near the max memory?

If it helps, I use the below code for my sensor:

uint16_t getEchoDistance (void){
  unsigned long duration;
  digitalWrite(trigPin, LOW);
  delay(5);

  // trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(8);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2);
  return distance;
}

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.