so I'm working on a project with a lot of components that need to be running at the same time. Part of this project is where I'm trying to run the NewPing library to get the speed of sound. Here's what I have for this part so far:
#include <NewPing.h>
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
const int maxDistance = 35; //cm
const int trigPin = 10;
const int echoPin = 11;
unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds).
NewPing sonar(trigPin, echoPin, maxDistance);
void setup() {
//sets up the ultrasonic sensor
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
pingTimer = millis(); // Start now.
}
void loop() {
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
if (currentMillis >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set the next ping time.
sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
}
// call the functions that do the work
.... a bunch of functions here
}
void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
// Don't do anything here!
if (sonar.check_timer()) { // This is how you check to see if the ping was received.
// Here's where you can add code.
Serial.print("Ping: ");
Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
Serial.println("cm");
}
// Don't do anything here!
}
My question is, what variable is the final distance stored in? I wanted to add a return statement that returns a floating value for the distance that I can use in another function.
If I added "duration=sonar.ping(); //store the time in micro seconds" as show below, would that work?
void loop() {
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
if (currentMillis >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set the next ping time.
sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
duration=sonar.ping(); //store the time in micro seconds
}
// call the functions that do the work
}
I expect that someone able to compute the speed of sound from information obtained from a ping sensor would be smart enough to know what data was needed, and where the library stores that data.
It turns out that there's a conflict with the timers in the IRreceiver and the NewPing libraries so I reverted back to the standard ultrasonic sensor library. It is not ideal as it uses interrupts but I had no other choice as I'm not very knowledgable in programming - I usually deal with the mechanical side of things.
Now the program is extremely buggy, the servo moves sporadically and jumps around a lot. The LEDs don't light up as they should... I really don't know what's going on now.
Exceedingly peculiar... It seems to be working fine now. It seems to depend on which USB port I plug it into whether it will function or not... Anyway, thanks for the help, I think I have it from here. I will delete my code in the previous post for intellectual property purposes.