Upload code utrasonic sensor

This commit is contained in:
Lymeng LY 2025-03-17 16:13:06 +01:00
parent 9c0c9ce8af
commit 8fcddff666
1 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,22 @@
#include <Arduino.h>
#include "MeMCore.h"
// Define the Ultrasonic Sensor using Makeblock library
MeUltrasonicSensor ultrasonic(PORT_3); // Change PORT_3 to the actual port used
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
double distance_cm = ultrasonic.distanceCm(); // Get distance in cm
double distance_in = ultrasonic.distanceInch(); // Get distance in inches
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.print(" cm | ");
Serial.print(distance_in);
Serial.println(" inches");
delay(500); // Wait for 500ms before the next measurement
}