#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <NewPing.h>
// Define the pins for the ILI9341 display
#define TFT_CS 47 // Chip select pin for the TFT display
#define TFT_RST 48 // Reset pin for the TFT display
#define TFT_DC 49 // Data/command pin for the TFT display
// Define the pins for the ultrasonic sensors
#define TRIGGER_PIN_1 22
#define ECHO_PIN_1 23
#define TRIGGER_PIN_2 24
#define ECHO_PIN_2 25
#define TRIGGER_PIN_3 26
#define ECHO_PIN_3 27
#define TRIGGER_PIN_4 28
#define ECHO_PIN_4 29
// Define the maximum distance (in millimeters) to measure
#define MAX_DISTANCE_MM 4000
// Create instances of the NewPing library for each sensor
NewPing distance1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE_MM);
NewPing distance2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE_MM);
NewPing distance3(TRIGGER_PIN_3, ECHO_PIN_3, MAX_DISTANCE_MM);
NewPing distance4(TRIGGER_PIN_4, ECHO_PIN_4, MAX_DISTANCE_MM);
// Create instance of the ILI9341 display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setup() {
Serial.begin(9600);
// Initialize display
tft.begin();
tft.setRotation(3); // Adjust rotation if needed
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
// Read distance from each sensor in centimeters
int d1_cm = distance1.ping_cm();
int d2_cm = distance2.ping_cm();
int d3_cm = distance3.ping_cm();
int d4_cm = distance4.ping_cm();
// Convert centimeters to millimeters
int d1_mm = d1_cm * 10;
int d2_mm = d2_cm * 10;
int d3_mm = d3_cm * 10;
int d4_mm = d4_cm * 10;
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
// Print results to display
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Distance 1: ");
tft.print(d1_mm);
tft.println(" mm");
tft.setCursor(10, 40);
tft.print("Distance 2: ");
tft.print(d2_mm);
tft.println(" mm");
tft.setCursor(10, 70);
tft.print("Distance 3: ");
tft.print(d3_mm);
tft.println(" mm");
tft.setCursor(10, 100);
tft.print("Distance 4: ");
tft.print(d4_mm);
tft.println(" mm");
delay(1000); // Delay between readings
}