علاوه بر متغییر آخری که داره تو سریال میاد بیرون باید متغییر us را هم float کنید
از این کد هم میتونید استفاده کنید:

کد:
// Demo sketch // "is sketch will output distance info via the UART port // port assignment // change as may be necessary const int trigger=6; const int echo=7; float distance; void setup(){ Serial.begin(9600); pinMode(trigger,OUTPUT); pinMode(echo,INPUT); } void loop(){ // Trigger US-100 to start measurement // Set up trigger digitalWrite(trigger,LOW); delayMicroseconds(5); // Start Measurement digitalWrite(trigger,HIGH); delayMicroseconds(10); digitalWrite(trigger,LOW); // Acquire and convert to mtrs distance=pulseIn(echo,HIGH); distance=distance*0.01657; // send result to UART Serial.println(distance); delay(50);
اینم یادم نیست تست کردم یا نه ولی فک کنم کار کنه :\

کد:
unsigned int EchoPin = 2; // The Arduino's the Pin2 connection to US-100 Echo / RX unsigned int TrigPin = 3; // The Arduino's Pin3 connected to US-100 Trig / TX unsigned long Time_Echo_us = 0; unsigned long Len_mm = 0; void setup() { //Initialize Serial.begin(9600); // The measurement results through the serial output to the serial port on the PC monitor pinMode(EchoPin, INPUT); // The set EchoPin input mode. pinMode(TrigPin, OUTPUT); // The set TrigPin output mode. } void loop() { // By the Trig / Pin sending pulses trigger US-100 ranging digitalWrite(TrigPin, HIGH); // Send pulses begin by Trig / Pin delayMicroseconds(50); // Set the pulse width of 50us (> 10us) digitalWrite(TrigPin, LOW); // The end of the pulse Time_Echo_us = pulseIn(EchoPin, HIGH); // A pulse width calculating US-100 returned if((Time_Echo_us < 60000) && (Time_Echo_us > 1)) { // Pulse effective range (1, 60000). // Len_mm = (Time_Echo_us * 0.34mm/us) / 2 (mm) Len_mm = (Time_Echo_us*34/100)/2; // Calculating the distance by a pulse width. Serial.print(Len_mm, DEC); // Output to the serial port monitor Serial.println("mm"); // Output to the serial port monitor } delay(100); // Per second (100ms) measured }