#include <Ping.h>
Ping ping = Ping(3,74,29);
#define pingPin 7
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3
int sound = 250;
void setup(){
Serial.begin(115200);
pinMode(pingPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(){
ping.fire();
Serial.print("Microseconds: ");
Serial.print(ping.microseconds());
Serial.print(" | Inches ");
Serial.print(ping.inches());
Serial.print(" | Centimeters: ");
Serial.print(ping.centimeters());
Serial.println();
long duration, distance;
distance = (duration/2) / 29.1;
if (distance <= 27) {
digitalWrite(led, HIGH);
sound = 250;
}
else {
digitalWrite(led,LOW);
}
if (distance < 22) {
digitalWrite(led2, HIGH);
sound = 260;
}
else {
digitalWrite(led2, LOW);
}
if (distance < 18) {
digitalWrite(led3, HIGH);
sound = 270;
}
else {
digitalWrite(led3, LOW);
}
if (distance < 13) {
digitalWrite(led4, HIGH);
sound = 280;
}
else {
digitalWrite(led4,LOW);
}
if (distance < 8) {
digitalWrite(led5, HIGH);
sound = 290;
}
else {
digitalWrite(led5,LOW);
}
if (distance < 3) {
digitalWrite(led6, HIGH);
sound = 300;
}
else {
digitalWrite(led6,LOW);
}
if (distance > 27 || distance <= 0){
Serial.println("Out of range");
noTone(buzzer);
}
else {
Serial.print(distance);
Serial.println(" cm");
tone(buzzer, sound);
}
delay(500);
}
The ping sensor is most likely putting out the correct data, but your IF statements are not look at that data, its looking at random data you made up.
This code here is the example code from that library. Notice anything different that it doesn't have?
/* This code initializes a Ping))) sensor on pin 13 and
outputs the information over a serial connection */
#include <Ping.h>
Ping ping = Ping(13,0,0);
void setup(){
Serial.begin(115200);
}
void loop(){
ping.fire();
Serial.print("Microseconds: ");
Serial.print(ping.microseconds());
Serial.print(" | Inches ");
Serial.print(ping.inches());
Serial.print(" | Centimeters: ");
Serial.print(ping.centimeters());
Serial.println();
delay(1000);
}
Did you figure it out? If you didn't, its this part here.
long duration, distance;
distance = (duration/2) / 29.1;
Why did you include this in your code, when it clearly does not have anything to do with the calculated data from the library. You should be using ping.inches() or ping.centimeters() instead of distance which is a random number.
got the code from somewhere else. but thanks.
hopefully i'm close but ive been trying to find the error. im getting feedback from my ping))) but the leds aren't responding they all stay lit up... whats wrong?
#include <Ping.h>
Ping ping = Ping(2,74,29);
#define pingPin 2
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#define buzzer 3
int sound = 250;
void setup(){
Serial.begin(9600);
pinMode(pingPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop(){
ping.fire();
Serial.print("Microseconds: ");
Serial.print(ping.microseconds());
Serial.print(" | Inches ");
Serial.print(ping.inches());
Serial.println();
long duration, inches;
ping.inches() == (duration/2) / 29.1;
if (inches <= 27) {
digitalWrite(led, HIGH);
sound = 250;
}
else {
digitalWrite(led,LOW);
}
if (inches < 22) {
digitalWrite(led2, HIGH);
sound = 260;
}
else {
digitalWrite(led2, LOW);
}
if (inches < 18) {
digitalWrite(led3, HIGH);
sound = 270;
}
else {
digitalWrite(led3, LOW);
}
if (inches < 13) {
digitalWrite(led4, HIGH);
sound = 280;
}
else {
digitalWrite(led4,LOW);
}
if (inches < 8) {
digitalWrite(led5, HIGH);
sound = 290;
}
else {
digitalWrite(led5, LOW);
}
if (inches < 3) {
digitalWrite(led6, HIGH);
sound = 300;
}
else {
digitalWrite(led6, LOW);
}
if (inches > 27 || inches <= 0){
Serial.println("Out of range");
noTone(buzzer);
}
else {
Serial.print(inches);
Serial.println(" in");
tone(buzzer, sound);
}
delay(500);
}
long duration, inches; Take these out of your code, throw them away, then burn the garbage.
ping.inches() == (duration/2) / 29.1;if (ping.inches() <= 27) { // sample IF statement, do this for the rest of them.