This is my next trial of HC-SR04, using a new coding.
//---------------------------------------------------------
#include <Ultrasonic.h>
#define TRIGGER_PIN 3
#define ECHO_PIN 2
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
void setup()
{
Serial.begin(9600);
}
void loop()
{
float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
Serial.print("MS: ");
Serial.print(microsec);
Serial.print(", CM: ");
Serial.print(cmMsec);
Serial.print(", IN: ");
Serial.println(inMsec);
delay(1000);
}
//---------------------------------------------------------------
And this is what i get from serial monitor.
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1414, CM: 25.59, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1414, CM: 25.59, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1422, CM: 25.74, IN: 10.13
MS: 1422, CM: 25.74, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1415, CM: 25.61, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
MS: 1415, CM: 25.61, IN: 10.08
MS: 1415, CM: 25.61, IN: 10.08
MS: 1421, CM: 25.72, IN: 10.13
MS: 1421, CM: 25.72, IN: 10.13
The posision of sensor and object doesn't change. Not moving. But, the result of "MS" like we see above. Not stable.
What is wrong ???
I'm in desperate mode now. =(