I am using the NewPing library. Here is my code
#include <NewPing.h>
#include <Servo.h>
//Pins
int PirF = 3;
int BootTime = 30;
int clearencemax = 5;
int mouth = 13;
int SonicFtrig = 41 ;
int SonicFecho = 40 ;
int piron = 0;
int forward = 200;
int reverse = 60;
NewPing sonar(SonicFtrig, SonicFecho, clearencemax);
Servo RightServo;
Servo LeftServo;
//Functions
void moveturn() { //Full Turn Around
LeftServo.write(forward);
RightServo.write(reverse);
delay(1000);
movestop();
}
void movestop() { //Full Stop
LeftServo.write(90);
RightServo.write(90);
}
void moveforward() { //Full speed ahead
LeftServo.write(forward);
RightServo.write(forward);
}
void scanforlife() {
Serial.print("Scango");
}
void speak() {
digitalWrite(mouth, HIGH);
//Put Emic Code here
delay(1000);
digitalWrite(mouth, LOW);
}
void shootgun() {
//Shoot the water gun!
}
//Warm up sensors
void setup(){
LeftServo.attach(8);
RightServo.attach(7);
Serial.begin(9600);
pinMode(PirF, INPUT);
digitalWrite(PirF, LOW);
//Booting stuff
Serial.print("booting systems ");
for(int i = 0; i < BootTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("ACTIVE");
delay(50);
}
void loop() {
//HCSR04 Stuff
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
But, in my Serial monitor, all I see is:
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Ping: 0cm
Over and over again!
WHAT IS WRONG??
Setting the maximum distance to 5cm won't be helping. Anything further than 5cm away will return 0. Also, a HC-SR04 doesn't work below 2 or 3cm and will return 0 as well. You're only likely to get a non-zero result if an object is between 3cm and 5cm away.
Increase the maximum distance to a reasonable value.
To make things easier, you could have stripped out all code not associated with the ping sensor, since that's all that happens in the 'loop()'. All the other stuff just confuses the issue, and serves no purpose whatsoever.
This is the only part of your code that actually relates to the problem:-
#include <NewPing.h>
//Pins
int clearencemax = 5;
int SonicFtrig = 41 ;
int SonicFecho = 40 ;
NewPing sonar(SonicFtrig, SonicFecho, clearencemax);
void setup()
{
Serial.begin(9600);
}
void loop()
{
//HCSR04 Stuff
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
OldSteve:
Setting the maximum distance to 5cm won't be helping. Anything further than 5cm away will return 0. Also, a HC-SR04 doesn't work below 2 or 3cm and will return 0 as well. You're only likely to get a non-zero result if an object is between 3cm and 5cm away.
Increase the maximum distance to a reasonable value.
To make things easier, you could have stripped out all code not associated with the ping sensor, since that's all that happens in the 'loop()'. All the other stuff just confuses the issue, and serves no purpose whatsoever.
This is the only part of your code that actually relates to the problem:-
#include <NewPing.h>
//Pins
int clearencemax = 5;
int SonicFtrig = 41 ;
int SonicFecho = 40 ;
NewPing sonar(SonicFtrig, SonicFecho, clearencemax);
void setup()
{
Serial.begin(9600);
}
void loop()
{
//HCSR04 Stuff
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
Wait... So you are saying increase the value of "clearencemax" ? Would that help? Or are you saying that WONT help?
OH! I see, you have to increase the value of "clearencemax" to get it to read anything > 5! Thanks!
TOPIC SOLVED<