Try :
const float speedOfSound = 343.0 / 1000.0; // Convert to m/us
which is more accurate.
Try :
const float speedOfSound = 343.0 / 1000.0; // Convert to m/us
which is more accurate.
And replace long duration with this :
long duration = pulseIn(echoPin, HIGH, 26000); // Adjust this timeout if needed
if (duration > 0) {
// Calculate distance in meters
float distance = duration * speedOfSound / 2.0;
result = static_cast<long>(distance * 100); // Convert to cm
The chip on the Due has some special hardware for reading encoders (does it in hardware without needing an interrupt for each step), but I don't know if there is an Arduino Library that would make this easy to use.
you could throw a 2nd Arduino at the problem - one to deal with the motors, one to deal with the ultrasonics. you probably don't need all 30k steps per revolution; the encoder task could "concentrate" that, either just only forwarding some pulses, or sending less frequent messages that say "the encoder moved 22 ticks."
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.