#define PIN_TRIG 15
#define PIN_ECHO 2
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
if(Serial.available()>0){
String command = Serial.readStringUntil('\n');
if(command=="abstand"){
sensor_hcsr04();
}
}
delay(1000); // this speeds up the simulation
}
void sensor_hcsr04(){
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
float duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Abstand in cm: ");
Serial.println(duration / 58.8);
}