#include <Servo.h>
Servo s;
int trig = 2;
int echo = 3;
int distance,duration =0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
s.attach(1);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Set the trigPin on for 10 microseconds to trigger the sensor
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
// Calculate distance in centimeters
distance = duration * 0.034 / 2;
s.write(distance);
// this speeds up the simulation
}