EasyManua.ls Logo

Freenove ESP32 - Page 111

Default Icon
159 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Need support? support@freenove.com
109
Chapter 10 Ultrasonic Ranging
www.freenove.com
The following is the code:
1
2
3
4
5
6
7
8
9
10
11
12
#include "UltrasonicRanging.h"
void setup() {
Serial.begin(115200);
setupSonar();
}
void loop() {
float dist = getSonar();
Serial.printf("dist: %.2fcm\r\n", dist);
delay(500);
}
Initialize the pins of the ultrasonic module.
6
setupSonar();
Get the data value of the ultrasonic module, the unit is cm.
13
float dist = getSonar();
UltrasonicRanging.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _ULTRASONICRANGING_h
#define _ULTRASONICRANGING_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define PIN_TRIG 32 //Ultrasonic trig pin
#define PIN_ECHO 12 //Ultrasonic echo pin
void setupSonar();
float getSonar();
#endif

Related product manuals