33 pinMode(URTRIG,OUTPUT); // A low pull on pin COMP/TRI
G
34 digitalWrite(URTRIG,HIGH); // Set to HIGH
35 pinMode(URECHO, INPUT); // Sending Enable PWM mode co
mmand
36 delay(500);
37 Serial.println("Init the sensor");
38
39 }
40 void loop()
41 {
42 PWM_Mode();
43 delay(100);
44 }
45
46 void PWM_Mode() // a low pull on pin COMP/TRI
G triggering a sensor reading
47 {
48 Serial.print("Distance Measured=");
49 digitalWrite(URTRIG, LOW);
50 digitalWrite(URTRIG, HIGH); // reading Pin PWM will output
pulses
51 if( Measure)
52 {
53 unsigned long LowLevelTime = pulseIn(URECHO, LOW) ;
54 if(LowLevelTime>=45000) // the reading is invalid.
55 {
56 Serial.print("Invalid");
57 }
58 else{
59 DistanceMeasured = LowLevelTime /50; // every 50us low level stands
for 1cm
60 Serial.print(DistanceMeasured);
61 Serial.println("cm");
62 }
63