// Minimal sketch from Arduino "Ping" example // Modified for separate trig and echo pins // Same pins used for all three examples // Most recent cores as of April 4, 2014 // Arduino 1.8.13 on Linux Mint const int trigPin = PIN_PA2; const int echoPin = PIN_PA3; long duration, inches, cm; void setup() { Serial.begin(115200); delay(5000); // time to toggle display Serial.println("\n\n\n\t\tStartup AVR64DD14"); //Serial.println("\n\n\n\t\tStartup ATTiny1616"); //Serial.println("\n\n\n\t\tStartup ATtiny402"); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { for (int n = 0; n < 10; n ++) { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(5); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print("\t"); Serial.print(inches); Serial.print(" in\t"); Serial.print(cm); Serial.print(" cm\t"); Serial.print(duration); Serial.print(" duration\n"); delay(50); } exit(0); } long microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; }