4 years, 11 months ago.

ultrasonic sensor

Could any one help me with some questions? thanks!! I want to measure distance using an ultrasonic sensor and print the measurement on LCD. I have finished the code but it has some problems and the most serious one is that it cannot return a number from the sensor. I think LCD does not have problems because it can print the words "the distance is". Below is my code.

  1. include "mbed.h"
  2. include"TextLCD.h"

float distance(){ float time_us; Timer timer; float distance; AnalogIn echoin(A2); AnalogOut trigout(A1); trigout=1; wait_us(50); trigout=0; pulsein

while(echoin==0); timer.start(); while(echoin==1); timer.stop(); time_us=timer.read_us(); distance=(time_us*34/100)/2; timer.reset(); return distance; }

int main(){ float a=0; TextLCD lcd (D2,D3,D9,D10,D11,D12); rs,e,d0,d1,d2,d3 lcd.printf("the distance is"); while(1){ a=distance(); lcd.locate(1,1); lcd.printf("aaa"); lcd.printf("%f mm",a); wait(0.3); } }

I'm sorry about the bad example of the code, and I'll ask another question about this. Please ignore it.

posted by gao yuan 17 May 2019
Be the first to answer this question.