#include #include // Pin that the distance sensor's trig pin is connected to const int trigPin = 7; // Pin that the distance sensor's echo pin is connected to const int echoPin = 8; // The maximum distance that the distance sensor can measure (in centimeters) const int maxDistance = 200; // The minimum and maximum vibration values that the phone can produce (0-255) const int minVibrationValue = 0; const int maxVibrationValue = 255; void setup() { // Initialize serial communication Serial.begin(9600); // Initialize Bluetooth Bluetooth.begin("YourBluetoothDeviceName"); // Set the pin modes for the distance sensor pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // Send a pulse to the distance sensor's trig pin digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the pulse width of the echo pulse int pulseWidth = pulseIn(echoPin, HIGH); // Calculate the distance based on the pulse width int distance = pulseWidth / 58; // Clamp the distance to the maximum distance that the sensor can measure distance = min(distance, maxDistance); // Calculate the vibration value based on the distance int vibrationValue = map(distance, 0, maxDistance, maxVibrationValue, minVibrationValue); // Send the vibration value to the phone Bluetooth.println(vibrationValue); }