Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Fun6 Distance Sensitive Ball

In this project, we utilize an ultrasonic module to control the vertical movement of a ball on the stage. When you click the green flag, place your hand above the ultrasonic module. The ball will ascend if the distance between your hand and the module is less than 15 cm; otherwise, it will descend. When the ball makes contact with a line, it triggers a delightful sound and activates twinkling starlight effects.

Follow these steps to set up the project, and feel free to tweak the effects to your liking once you’re accustomed to how it works.

1. Select Sprites

  • Remove the default sprite and select the Ball, Bowl, and Star sprites.

    ../../_images/ball_choose_sprite.png
  • Position the Bowl sprite at the center bottom of the stage and increase its size.

    ../../_images/ball_set_bowl.png
  • Place the Ball sprite directly above the Bowl sprite, setting its direction to 0 to allow vertical movement.

    ../../_images/ball_set_ball.png
  • Adjust the Star sprite’s size and set its direction to 180 to ensure it falls downward. This can be altered to different angles if preferred.

    ../../_images/ball_set_star.png
  • Add the Stars backdrop for added ambiance.

    ../../_images/ball_select_backdrop.png

2. Draw a Line Sprite

  • Now add a Line sprite.

    ../../_images/ball_select_line.png
  • Navigate to the Costumes page of the Line sprite.

    ../../_images/ball_open_cos.png
  • Slightly reduce the width of the red line on the canvas, duplicate it four times, and align these lines.

    ../../_images/ball_copy_line.png
  • Color each line differently. Select a line, use the Fill tool, and pick a color.

    ../../_images/ball_set_bk_color.png
  • Apply this method to color all lines accordingly.

    ../../_images/ball_select_line_color.png
  • Return to the Code page and position the Line sprite at the top of the stage.

    ../../_images/ball_line_position.png

3. Scripting the Ball Sprite

Here, we script the Ball sprite to move up or down based on the distance detected by the ultrasonic module, with a movement constraint to simulate landing on the Bowl sprite.

  • When the green flag is clicked, set the initial position of the Ball sprite.

    ../../_images/ball_script_ball1.png
  • Use an [if else] block to check if the distance is less than 15. If true, move the Ball sprite up by 10 steps, given its direction is set to 0.

    ../../_images/ball_script_ball3.png
  • Otherwise, let the Ball sprite fall, limiting its Y coordinate to a minimum of -100, adjustable to appear as though it’s landing on the Bowl sprite.

    ../../_images/ball_script_ball4.png
  • Script interaction where the Ball sprite, upon touching the Line sprite, records its Y position to the variable ball_coor and broadcasts a bling message.

    ../../_images/ball_script_ball5.png

4. Scripting the Star Sprite

  • Initially hide the Star sprite when the green flag is clicked. Upon receiving the Bling message, clone the Star sprite.

    ../../_images/ball_script_star1.png
  • Set the clone’s position and sound effects to synchronize with the Ball sprite’s position.

    ../../_images/ball_script_star2.png
  • Allow it to rotate between -80 to 80 degrees randomly.

    ../../_images/ball_script_star3.png
  • Adjust the appearance and behavior of the Star sprite as needed to enhance the visual effect.

    ../../_images/ball_script_star4.png

Programming is complete. Click the green flag to run the script and see if it meets your expectations.