//Original Program from Elena Cardiel, modified by Agonzalespi. FabAcademy 2021 //First, we need to include the serial communication library #include SoftwareSerial Serial_sonar(0, 2); // tx, rx . This is because RX in my FTDI connector corresponds to pin 2 of the ATTINY45. TX is not actually used //Now we define variables and pin numbers. This may vary if your board is different to mine. //My Echo pin is connected to PB3. My Trigger pin is connected to PB4. Use the ATTINY45 pinout from the datasheet to know where are your pins connected #define echoPin 4 // echo conencted to PB3 #define trigPin 3 // trigger connected to PB4 //Now this is Elena Cardiel's program long duration, distance; //serial communication and pins for input and output void setup() { Serial_sonar.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } // the actual program loop void loop() { //send the pulse digitalWrite(trigPin, LOW); delayMicroseconds(4); //each 1 sec we send a pulse digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); //we measuse the time duration = pulseIn(echoPin, HIGH); //formula for cm feedback. The sound speed is 340m/s = 29us/cm. //we divide the pulse time by 58, the time that the pulse takes to go and come back. distance = duration/58; //send the information to the Serial Monitor Serial_sonar.println(distance); //Serial_sonar.println(" cm"); delay(200); }