Shoot the Ball!

This will probably be the EASIEST and COOLEST basketball machine you would have ever seen in your life. This is a simple game that only requires simple materials to make. From a distance that will be detected by the ultrasonic sensor, the player will then shoot the ball into an opening that will close and open continuously. Then, the ball AUTOMATICALLY follows a track in the box and flows directly back to you. You can then retrieve the ball and shoot again. This game suits both children, adults, and the old, and you will never be bored with it.


Step 1: Materials (Not Related to Arduino)

  1. 1 box with width: 28 lengths: 35 and height: 22 Boxes with other sizes are fine, as long as the height is at least 15 cm. It needs to be big enough for an Arduino board, several LEDs, circuits, and a small track (that will be created later) for the ball.
  2. 1 Utility knife (any knife that can cut is fine since this is just for cutting the boxes)
  3. Some tape
  4. Pen (for marking and decoration)
  5. Ruler (optional) for precise measurements
  6. A nail (for piercing holes)

Step 2: Other Electronic Materials (Arduino)

  1. 1* Arduino Leonardo (Buy here)
  2. 1* Breadboard ( Here)
  3. 7* LEDs ( Here)
  4. 1* Arduino Ultrasonic Sensor ( Here)
  5. 1* USB to Micro USB Wire ( Here)
  6. 16* Male to Female Breadboard Jumper Wire ( Here)
  7. 27* Male to Male Breadboard Jumper Wire (Here)
  8. 8* 1/4w resistor of 5% (Here)
  9. 1* 1/4w resistor of 1% (Here)
  10. 1* Servo Motor (Here)
  11. 1* Photoresistor (Here)

Step 3: Codes

My code is here. Access my code through this link, the link below, the screenshots, or the attached code below.

Three functions are in my code.

1. The first one is the servo motor. Annotations are below in the linked code where you can find only about 4 lines of the code that moves the servo motor.

2. The second one is the detection of the player's distance. The code after the servo motor code is the distance code. The ultrasonic detector detects the distance. If the distance is great enough for the player to shoot the ball (player stands at that fixed position when shooting the ball), the green light flashes. If the player is a bit too close, both green and yellow light flashes. When the player is AGGRESSIVELY close (when the range allows the player to just put the ball in effortlessly), all red, yellow, and green light flashes until the player backs off.

3. The last one is the score detection. After the ball goes in the box, it travels through a track, where there are an LED light and a photoresistor. As the ball moves through the track, it blocks the LED which shines to the photoresistor. As a result, the photoresistor detects darkness. And then activates one of the LED. This indicates one point.

<p>#include <br>Servo servo_pin_4;
int _1_light = 0.0 ;</p><p>const int trigPin = 2;
const int echoPin = 3;
const int LEDlampRed = 9;
const int LEDlampYellow = 10;
const int LEDlampGreen = 11; //PINMODE of each functions (as stated in intro)</p><p>int sound = 500;  </p><p>void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT); // Some basic setups</p><p>  pinMode( 13 , OUTPUT);
  pinMode( 5 , OUTPUT);
  pinMode( 12 , OUTPUT);
  pinMode( 6 , OUTPUT);
  _1_light = 0.0 ;  // More PINMODE</p><p>}</p><p>void loop() {
digitalWrite( 13 , HIGH );
servo_pin_4.write( 150.0 ); //Servo motor code (the servo motor turns 150 degrees)</p><p>long durationindigit, distanceincm; //Basic setups measuring distance with Ultrasonic Sensor
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit * 0.034) / 2;</p><p>//Code of distance that will be where participant shoots (The range of your participant. This can be altered by changing 135 to any integers.)
if (distanceincm > 135) { 
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
// Yellow light with disance between 85 cm and 135 cm (These two numbers also can be changed to no more than 2 meters.)
else if (distanceincm <= 135 && distanceincm > 85) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
// Distance code when distance is between 35 and 85
else if (distanceincm <= 85 && distanceincm > 35) {
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed, LOW);
// Distance code when distance is not detected (or in other words, less than 0) with only yellow light shining
else if (distanceincm <= 0) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
//Distance code when the actions above never happens
else {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, HIGH);
digitalWrite(LEDlampRed, LOW);
Serial.println(" cm");</p><p>delay(300);</p><p>//The code that detects score. After 3 points are scored, the score returns to 0 automatically. More can be added if you want to.
  Serial.print(analogRead( A0 ));
  Serial.print(" ");
  if (( ( analogRead( A0 ) ) < ( 700.0 ) ))
    _1_light = ( _1_light + 1.0 ) ;
    delay( 1000.0 );
  if (( ( _1_light ) == ( 1.0 ) ))
    digitalWrite( 5 , HIGH );
  if (( ( _1_light ) == ( 2.0 ) ))
    digitalWrite( 5 , HIGH );
    digitalWrite( 12 , HIGH );
  if (( ( _1_light ) == ( 3.0 ) ))
    digitalWrite( 5 , HIGH );
    digitalWrite( 12 , HIGH );
    digitalWrite( 6 , HIGH );
    delay( 2500.0 );
    digitalWrite( 5 , LOW );
    digitalWrite( 12 , LOW );
    digitalWrite( 6 , LOW );
    delay( 1000.0 );
    _1_light = 0.0 ;

Step 4: Assemble the Circuits

Test the circuits. Make sure it functions successfully before putting it inside the box. Altering it is hard once you put the board in the box. The first two photos that look extremely simple are what your board should look like before attaching the Male to Female Breadboard Jumper Wires. The photos of connecting the Male to Female Breadboard Jumper Wire is the third one (so complicated, but not at all)

The circuit made with Fritzing, which provides better visualization, in fact, looks quite complicated. However, most of the circuits consist of connecting LED, undoubtedly the most simple step in Arduino.

Basically, the entire circuit consists of 7 LEDs (one green, one red, one orange, one white, the rest any color), an ultrasonic sensor, a servo motor, and a photoresistor. If you know how to connect these personally, you don't need to follow my circuit.

Step 5: Basic Box Setup

As you can see from the top, the box you choose should contain four opening sides. Use the knife to cut both of the longer sides of the box into half (Those two with half remaining. The side on the right is where the servo motor moves the box.). Cut them so that the servo motor can move the right side without hitting the two remaining sides.

Other parts of the box that needs to be cut out are shown in the second photo where there's an ultrasonic sensor. The larger hole, which is about 15 cm X 10 cm, is for the ball to flow out of the box after going in. The size can be smaller according to the size of the ball. This hole is definitely way too big for my ball, but the track (which will be introduced later) is able to get the ball out of the box effectively. The smaller hole is for the ultrasonic sensor, which is cut out according to the size of the sensor.

Step 6: The Servo Motor

This is the right side of the box that I described in the previous step. The servo motor is attached directly at the edge of the box. The center of the white part (Yes, the middle) needs to be at the same height as the joint of the box to move the edge of the box. As the white component of the servo motor moves, the edge of the box also moves as well. Use some tape to attach the white component (which moves when the servo motor is powered) to the box to let it move. If you use this type of white component as me, make sure to cut a small hole at the edge of the box so that if the white component moves, its movement won't be blocked by the edge of the box.

Then, connect the motor to your Arduino Leonardo with 2 Male to Female Breadboard Jumper Wire.

Step 7: LED Setup

Use a nail (as shown in the photo) to pierce 12 holes (6 for each side). 2 holes in a line. Then, put the lightbulbs, which each has two legs, through the holes. As a result, the top part of the light bulb will be shown outside the box, while the legs are in the box. (This can be seen in my photo) This enhances the overall effect of the lightbulbs, without the unattractive legs outside (as shown in Picture 2).

The LEDs detecting distance should be in this sequence from top to bottom: green, yellow, red.

Picture 2 (with the lightbulbs) is what should look like in the front. Picture 3 is the interior of the box.

After setting the lightbulb to your desired position (to the point where the light bulbs can hang without falling), do the same for the rest of the lightbulbs.

Step 8: Last Set-up for the LEDs

Connect the Male to Female Breadboard Jumper Wires to the LEDs (which are in the interior of the box) as shown in the last instruction. As a result, the lightbulb can be powered. Do this for both sides: the 3 lightbulbs measuring distance on the left and the 3 lightbulbs calculating the score on the right.

*There are 12 jumper wires to connect to the lightbulbs in this step, which may be quite challenging.*

Picture 3 is what the front of the box (only the LED, as the track, hasn't been attached yet) should look like after completing this step.

Step 9: The Track

Since there's a lot of additional space in the box, a track that's like an inclined plane needs to be placed for the ball to flow out of the box after a player scores.

For the inclined plane, cut out a piece from the box with size about 28 X 40 cm. Then fold at one end for about 5 cm.

Then, cut out two small pieces of the box (with sizes approximately 8 X 35 cm). Then, set them up (let them stand up like Picture 2), and then bend both pieces at the end. The higher part of the inclined plane has a wider opening, and vice versa (the lower part has a narrow opening). It is placed like this since the ball enters from the wider opening and is able to flow out through the narrow opening in a fixed direction. Thus players can catch the ball flowing out. Use tape to fix them at that "standing position".

This track should be placed into the box at last once everything is functioning correctly.

Step 10: Using the Photoresistor

From the last inclined plane (the previous step), pierce two holes at both side of the "standing pieces." Then, connect a new "WHITE" LED (with Male to Female Breadboard jumper wires) to the inclined plane. It NEEDS to be a white LED or this function won't work. Place the LED through the hole that is just pierced, and place the photoresistor to the hole at the other side.

This is the trickiest part of the entire system. When the ball slides down the track, make sure the ball isn't stuck at the opening or moves too fast, as the photoresistor would not be able to detect the ball passing through it. Adjust the whole thing (this takes a lot of time) to the point when the ball slides down the track, one of the LED immediately lightens.

Step 11: Put Everything in the Box and Make Sure Everything Work Out Well.

This is the last step (finally). Place the Arduino Leonardo board into the box (to the edge where a huge hole is being pierced from the previous steps). Then, as the LED and the photoresistor on the ramp are connected to the Arduino Leonardo board, place the inclined ramp (Yes, the one with the track on it) into the box.

This provides an opening that allows the ball to flow out of the track, returning the ball back to the player after scoring. And placing the ramp so the higher part is against the opening allows you to place the Arduino Leonardo under this ramp. (in the second photo, the board is placed under the ramp)

Step 12: Video!!!

Here is my link.

As in the introduction, the left part of the LED light is the distance where you should shoot. If the light appears in green, it means your distance is far enough from the box. (This avoids cheating!) If the light appears in another color, such as yellow or red, it means you are too close. Therefore, this is a mere indication of your shooting distance. You can change it from the codes if you want a farther shooting distance.

The right part of the LED is your scoring, which there's a track attaching a photoresistor. When the ball flows in the box through the track inside the box, one of the light on the right-hand side would light up, indicating one point. There may be anomalies in this step, yet they are not common.



    • Backyard Contest

      Backyard Contest
    • Beauty Tips Contest

      Beauty Tips Contest
    • Classroom Science Contest

      Classroom Science Contest