#include <Arduino.h>
#include <SPI.h>
#include <ESP32Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <math.h>
const int TRIG_PIN = 27, ECHO_PIN = 19;
const int SERVO_PIN = 26;
const int TFT_CS = 5, TFT_DC = 2;
const int MAX_CM = 200, ALERT_CM = 40;
const int MIN_ANGLE = 15, MAX_ANGLE = 165;
const int STEP_DEG = 3;
const uint32_t STEP_MS = 90, SETTLE_MS = 35;
const uint32_t TRAIL_MS = 5000;
const int SAMPLES = (MAX_ANGLE - MIN_ANGLE) / STEP_DEG + 1;
Adafruit_ILI9341 tft(TFT_CS, TFT_DC);
GFXcanvas16 *scope = nullptr;
Servo scanner;
int historyCm[SAMPLES];
uint32_t historyAt[SAMPLES];
int paintedCm[SAMPLES];
uint16_t paintedColor[SAMPLES];
int angle = MIN_ANGLE, direction = 1, distanceCm = -1;
int paintedAngle = -1, paintedDirection = 1;
int paintedDistance = -999, paintedStatus = -1;
uint32_t movedAt = 0;
bool sampled = false;
const uint16_t BACK = 0x0841, GRID = 0x0224;
const uint16_t BEAM = 0x07E0, TEXT = 0xDF7B;
const uint16_t AMBER = 0xFD20, NEAR = 0xF923;
const int CX = 160, CY = 160, RADIUS = 146;
int readDistanceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
uint32_t echoUs = pulseIn(ECHO_PIN, HIGH, 25000);
if (!echoUs) return -1;
int cm = echoUs / 58;
return cm >= 2 && cm <= 400 ? cm : -1;
}
void polarPoint(int degrees, int radius, int &x, int &y) {
float radians = degrees * PI / 180.0f;
x = CX + int(radius * cosf(radians));
y = CY - int(radius * sinf(radians));
}
void drawGrid() {
scope->fillScreen(BACK);
for (int ring = 1; ring <= 4; ++ring) {
int radius = RADIUS * ring / 4;
for (int degrees = 0; degrees < 180; degrees += 3) {
int x1, y1, x2, y2;
polarPoint(degrees, radius, x1, y1);
polarPoint(degrees + 3, radius, x2, y2);
scope->drawLine(x1, y1, x2, y2, GRID);
}
scope->setCursor(CX + 4, CY - radius + 3);
scope->setTextColor(0x03E7);
scope->setTextSize(1);
scope->print(MAX_CM * ring / 4);
}
for (int degrees = 0; degrees <= 180; degrees += 30) {
int x, y;
polarPoint(degrees, RADIUS, x, y);
scope->drawLine(CX, CY, x, y, GRID);
}
}
void blitScope(int x, int y, int width, int height) {
int right = min(320, x + width), bottom = min(162, y + height);
x = max(0, x); y = max(0, y);
if (right <= x || bottom <= y) return;
tft.startWrite();
tft.setAddrWindow(x, y + 40, right - x, bottom - y);
for (int row = y; row < bottom; ++row)
tft.writePixels(scope->getBuffer() + row * 320 + x, right - x);
tft.endWrite();
}
uint16_t targetColor(int cm, uint32_t age) {
if (cm < 2 || cm > MAX_CM || age > TRAIL_MS) return 0;
bool close = cm <= ALERT_CM;
if (age > TRAIL_MS / 2) return close ? 0x8800 : 0x8220;
return close ? NEAR : AMBER;
}
void drawScan(uint32_t now) {
drawGrid();
for (int i = 0; i < SAMPLES; ++i) {
int cm = historyCm[i];
uint32_t age = now - historyAt[i];
uint16_t color = targetColor(cm, age);
if (!color) continue;
int radius = cm * RADIUS / MAX_CM;
int x, y;
polarPoint(MIN_ANGLE + i * STEP_DEG, radius, x, y);
scope->fillCircle(x, y, 3, color);
}
for (int trail = 9; trail >= 0; trail -= 3) {
int degrees = angle - direction * trail;
if (degrees < MIN_ANGLE || degrees > MAX_ANGLE) continue;
int x, y;
polarPoint(degrees, RADIUS, x, y);
scope->drawLine(CX, CY, x, y, trail ? 0x03C0 : BEAM);
}
// Keep the latest hit visible on top of the sweep line.
if (distanceCm >= 2 && distanceCm <= MAX_CM) {
int x, y;
polarPoint(angle, distanceCm * RADIUS / MAX_CM, x, y);
scope->fillCircle(x, y, 4, distanceCm <= ALERT_CM ? NEAR : AMBER);
}
// Send only the changed beam area, rather than the entire frame over SPI.
if (paintedAngle < 0) blitScope(0, 0, 320, 162);
else {
int left = CX, right = CX, top = CY;
for (int pass = 0; pass < 2; ++pass) {
int a = pass ? angle : paintedAngle;
int d = pass ? direction : paintedDirection;
for (int trail = 0; trail <= 9; trail += 3) {
int degrees = a - d * trail, x, y;
if (degrees < MIN_ANGLE || degrees > MAX_ANGLE) continue;
polarPoint(degrees, RADIUS, x, y);
left = min(left, x); right = max(right, x); top = min(top, y);
}
}
blitScope(left - 4, top - 4, right - left + 9, CY - top + 9);
}
for (int i = 0; i < SAMPLES; ++i) {
uint16_t color = targetColor(historyCm[i], now - historyAt[i]);
if (paintedAngle >= 0 &&
(color != paintedColor[i] || historyCm[i] != paintedCm[i])) {
int x, y, degrees = MIN_ANGLE + i * STEP_DEG;
if (paintedColor[i]) {
polarPoint(degrees, paintedCm[i] * RADIUS / MAX_CM, x, y);
blitScope(x - 4, y - 4, 9, 9);
}
if (color) {
polarPoint(degrees, historyCm[i] * RADIUS / MAX_CM, x, y);
blitScope(x - 4, y - 4, 9, 9);
}
}
paintedCm[i] = historyCm[i]; paintedColor[i] = color;
}
tft.fillRect(10, 216, 144, 24, BACK);
tft.setTextSize(2);
tft.setTextColor(TEXT);
tft.setCursor(10, 218); tft.print(angle); tft.print(" deg");
if (distanceCm != paintedDistance) {
tft.fillRect(170, 216, 150, 24, BACK);
tft.setCursor(170, 218);
if (distanceCm < 0) tft.print("NO ECHO");
else { tft.print(distanceCm); tft.print(" cm"); }
}
int status = distanceCm < 0 ? 0 : distanceCm <= ALERT_CM ? 1
: distanceCm > MAX_CM ? 2 : 3;
if (status != paintedStatus) {
tft.fillRect(244, 4, 73, 27, BACK);
tft.setTextSize(1);
tft.setTextColor(status == 1 ? NEAR : BEAM);
tft.setCursor(248, 9);
tft.print(status == 0 ? "NO ECHO" : status == 1 ? "NEAR!"
: status == 2 ? "OFF SCALE" : "SCANNING");
}
paintedAngle = angle; paintedDirection = direction;
paintedDistance = distanceCm; paintedStatus = status;
}
void setup() {
Serial.begin(115200);
scope = new GFXcanvas16(320, 162);
if (!scope || !scope->getBuffer()) {
Serial.println("Display buffer allocation failed");
while (true) delay(1000);
}
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
scanner.setPeriodHertz(50);
scanner.attach(SERVO_PIN, 500, 2400);
scanner.write(angle);
// MISO is unused: GPIO19 is reserved for the ultrasonic echo.
SPI.begin(18, -1, 23, TFT_CS);
tft.begin();
tft.setRotation(3);
tft.fillScreen(BACK);
tft.setTextColor(TEXT);
tft.setTextSize(1);
tft.setCursor(10, 5); tft.print("KILIZU COMPUTING LAB");
tft.setTextSize(2);
tft.setCursor(10, 19); tft.print("ULTRASONIC RADAR");
tft.drawFastHLine(0, 37, 320, GRID);
tft.drawFastHLine(0, 202, 320, GRID);
tft.setTextSize(1);
tft.setCursor(10, 205); tft.print("ANGLE");
tft.setCursor(170, 205); tft.print("DISTANCE");
for (int i = 0; i < SAMPLES; ++i) historyCm[i] = -1;
drawScan(millis());
movedAt = millis();
}
void loop() {
uint32_t now = millis();
// Move, allow the servo to settle, then measure that direction.
if (!sampled && now - movedAt >= SETTLE_MS) {
distanceCm = readDistanceCm();
int slot = (angle - MIN_ANGLE) / STEP_DEG;
historyCm[slot] = distanceCm;
historyAt[slot] = now;
drawScan(now);
sampled = true;
}
if (sampled && now - movedAt >= STEP_MS) {
if (angle == MAX_ANGLE) direction = -1;
else if (angle == MIN_ANGLE) direction = 1;
angle += direction * STEP_DEG;
scanner.write(angle);
movedAt = now;
sampled = false;
}
delay(1);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4