---
title: "Activity: Ping-Pong Shooter"
date: "2023-10-12"
categories: ["Activities", "All", "Simulator Activities"]
tags: ["Colour Sensors", "Branching", "Iteration", "Algorithm Design", "Advanced"]
excerpt: "Use the ping-pong shooter attachment to locate and topple towers from a distance. The ultrasonic sensor can be used to find the tower by searching in a radar motion. Once the tower is located, the distance can be used to move the rover"
featuredImage: "/images/resources/activity-ping-pong-shooter.jpg"
---

Use the ping-pong shooter attachment to locate and topple towers from a distance. The ultrasonic sensor can be used to find the tower by searching in a radar motion. Once the tower is located, the distance can be used to move the rover close enough to topple the tower.

### Setup

Start with a large empty space on the floor, and place a tower, approximately 20cm tall, with a light object like a Lego figurine on top. The tower should be made of lightweight material like paper or cardboard. The larger the tower, the easier it is for the shooter to hit, so choose a size from 10cm - 5cm wide. Outline a circle of tape on the ground around the tower. This is the shooting limit that the rover must not cross over. Again, the larger the ring, the harder it is to hit the target, so size accordingly.

First, the rover must be placed in a random location near the tower. Then, the robot should spin around and locate the tower. Finally, it must distance itself from the tower without crossing the line and hit the tower with a ping-pong ball.

### Here’s Our Approach

**Constant -** A constant is a variable that never changes when the code is run. In python, we write this in all capitals with underscores between words.

#### 1) Setting the Constants:

![](/images/content/3b085e-constant.png)

Start by making the ‘servo_out’ and ‘servo_in’ constants. ‘servo_out’ is how long the servo spins to shoot a ball. ‘servo_in’ is how long the servo spins to retract the pusher.

The constants we have provided might need to be tweaked for your specific rover. This can be done through trial and error to find the best values for you.

#### **2) The Shoot Function:**

![](/images/content/53bace-shoot.png)

Continuous servos can spin without limit, similar to a motor. Setting the servo to 90 degrees spins it at top speed in one direction, and -90 degrees turns it the other way. Setting the servo to 0 degrees stops the servo.

Here, we set the servo to spin and delay for the constant ‘servo_out’. We stop the servo for half a second and then turn it in the other direction for the duration of ‘servo_in’.

#### **3) Ultrasonic Loop:**

![](/images/content/9b745a-ultrasonic.png)

In the main code, the rover spins around until the ultrasonic detects an object within 50cm. Put a ‘Turn Left’ movement block in a while loop that checks the ultrasonic distance. The slower the rover turns, the easier it will be to find the tower, so adjust the speed accordingly.

If the rover keeps spinning a bit after seeing the tower, put a ‘Turn Right’ on the outside of the loop to correct it.

#### 4) Colour Sensor Loop:

![](/images/content/b713c5-colour.png)

In this stage, the rover moves forward while the brightness reading from the middle colour sensor is low. For our setup, the ground had a brightness of 100 and the white tape had a brightness of 180. According to these values, when the brightness passes halfway from black to white (140), the loop ends.

Make sure to enter your own values for this part.

####
5) Fire!:

![](/images/content/ea4457-fire.png)

The rover will stop the motors and call the ‘*shoot*’ function to hit the tower. This will pause the robot until the entire shoot sequence is complete.

#### Complete Code:

![](/images/content/0b24cc-code.png)

**Tip:** 

Our code aims to get as close as possible to the line and fire. However, another technique is to find the perfect distance from the tower to shoot from and use the ultrasonic to stop the robot.
