## Time of flight measurement * A 5V relay is opened, and the electromagnet is exposed. Its driver pin is connected to B0. * The program makes B0 HIGH, and therefore the electromagnet is energized. * The user now fixes this relay at a fixed height from the floor/table, and attaches * a small steel ball to it. * When the user sends binary number 1 via UART, the relay is turned off and the ball will automatically fall down. It should hit a contact switch made with 2 metal * plates connected between PA0 and GND. * The microcontroller starts a timer when the relay is turned off, and stops it when the contact switch * is triggered, thereby measuring the time taken for the ball to travel the known distance. * Results are returned via the serial port ![Screenshot](../images/ide/tof.jpeg) The code !!! tip "examples/C/time_of_flight_uart.c" ```c #include #include #include #include #define PIN_RELAY 0 #define RELAY_OFF PORTB |= (1<12)break; } uart_send_byte('\n'); distance = 0.5*981*distance*distance/1000./1000.; // S = 0.5 * g * t^2 uart_send_string("Distance(mm): "); ltoa(distance*10,buffer,10); // for(cnt = 0;buffer[cnt]!='\0';cnt++){ uart_send_byte(buffer[cnt]); if(cnt>12)break; } uart_send_byte('\n'); } } ``` ## Serial Monitor Output ![Screenshot](../images/ide/sermon_tof.png)