**Course 6 / Lecture 5:**

# Measure distance

If you know your robot's speed you can calculate the travelled distance. You will learn how to build a SNAP program, which calculates the travelled distance using the speed and time measurement. By completing this lecture you will understand how GPS systems can predict and calculate travelled distance.

**Figure 1** - Calculate the distance traveled by your Sniffer

## Requirements

- Ruler
- Calculator
- Ozeki Sniffer: http://www.ozeki.hu/index.php?owpn=6204
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- Lego connection installed: http://www.ozeki.hu/index.php?owpn=6067
- Snap basics: http://www.ozeki.hu/index.php?owpn=6180

## Import the project you created in the previous lecture and run it for 10 seconds!

In this lecture you need to use the program that you created in the previous
lecture. So please open the Snap application and import the Snap code you
wrote in *Lecture 4*. After that, you should start the stopwatch and
your Sniffer at the same time using the 's' key on your keyboard. Please wait
about 10 seconds then press the 't' key to stop the stopwatch and the robot.
On the canvas you can see the exact time passed like **Figure 2** demonstrates.

**Figure 2** - Import and run the project from Lecture 4

## Calculate the distance traveled from the speed you already know and the measured time!

To calculate the distance of the moving robot, you will use the equation on
**Figure 3**. The speed is equal to the distance traveled divided by the
passed time. To get the distance on the right side of the equation, you
should multiply both sides by the passed time. So the speed is equal to the
passed time multiplied by the speed. So in this task you should multiply the
speed calculated in the previous lecture by the passed time read on the canvas
to know how far your Sniffer moved.

**Figure 3** - How to calculate distance from speed and passed time

## Check your calculation by measuring the traveled distance!

After you have calculated the distance that your Sniffer should took,
you can test how accurate your calculation was. To do this, please measure
the distance between the start and end points using your ruler as it demonstared on **Figure 4**.
If it was correctly calculated, you should apporximately get the same result
from the distance measurement as well as from the calculation.

**Figure 4** - Confirm your calculation with a ruler

## Upgrade the stopwatch to measure the traveled distance by knowing the speed!

First create two new variables and name them as 'speed' and 'distance'. The 'speed'
variable will be used as a constant value to help calculating the distance.
The 'distance' variable will always store the value of the
momentary distance traveled. To set their values, please place two '**set
[variable] to [value]**' found in Variables category to the stage.
Using the first one, you need to set the speed
value you calculated in the previous lecture as a value of the 'speed'
variable then place the block under the 'when s key pressed' one. Then using
the second placed 'set' block, you need to set its value using the '**[value] x [value]**'
block to multiply the **speed** by the **passed time**. To refresh the
value of 'distance' variable while the program is running, please place the
associated 'set' block into bottom of the 'repeat until' block as you can see on
**Figure 5**. Thanks to these steps, you can see the passed time and
distance traveled of the Sniffer continuously while the program is running.

**Figure 5** - Modify your program to calculate distance

After you have modified the stopwatch, please start it using the 's' key and
wait about 10 seconds, then press the 't' key to stop your stopwatch and also your Sniffer.
If you checked the checkbox next to the 'distance' variable inside the 'Variables' category,
you should also see the distance (in meters) that your Sniffer is traveled on the top right
corner of the GUI like **Figure 6** shows.

**Figure 6** - Test your modified program and check the distance after stopping it

## Program code

**Figure 7** - Program code that moves the robot and calculates the traveled distance

Download the programThis code shows how to measure distance traveled by your robot using speed and passed time. It is easier to follow the guide if you have the codes in front of you: Download the SNAP code files |

## More information