Reader small image

You're reading from  Build and Code Creative Robots with LEGO BOOST

Product typeBook
Published inNov 2021
PublisherPackt
ISBN-139781801075572
Edition1st Edition
Concepts
Right arrow
Author (1)
Ashwin Shah
Ashwin Shah
author image
Ashwin Shah

Ashwin Shah is an Electronics and Communication engineer from India. Teaching is his passion and he started working at the tender age of 16 as a doubt solver for younger kids at after-school classes. Today, he is a successful edupreneur who runs his own STEM-based robotics, coding, and IoT institute - RoboFun Lab. Being one of the pioneers of STEM education in India, he has taught over 3000 students and trained 200+ educators thus far. He has trained 100+ teams for prestigious STEM competitions with 50+ national and 12+ international awards in the bag. Most of his students are now pursuing careers/education in STEM at prominent universities. Ashwin was a state-level badminton player during his college days.
Read more about Ashwin Shah

Right arrow

Chapter 10: Building a Candy Dispenser Robot

Candy dispensers are one of the most loved things by kids at places such as amusement parks, malls, airports, and elsewhere. They usually come with a simple dispensing mechanism that gives out candy to kids based on the cents inserted into the dispensers. Today, you will be building a candy dispenser that will give out candy based on the color of bricks detected. This project will further enhance your coding skills with the LEGO BOOST color sensor.

You can see an example of an automatic candy dispensing machine here:

Figure 10.1 – Automatic candy dispensing machine

This chapter will cover the following topics:

  • Building a candy dispenser robot
  • Let's code the robot to dispense candies based on different colors of LEGO brick detected
  • Time for a challenge

Technical requirements

In this chapter, you will need the following:

  • LEGO BOOST kit with six AAA batteries, fully charged
  • Laptop/desktop with Scratch 3.0 programming installed and an active internet connection
  • A diary/notebook with a pencil and eraser

Building a candy dispenser

Let's build a robot by following the building instructions given next. This is what our robot should eventually look like:

Figure 10.2

Proceed as follows:

  1. Take your BOOST Hub and ensure that the batteries are fully charged. The hub is illustrated in the following figure:

    Figure 10.3

  2. Take three 1x2 bricks with cross holes and a 1x6 brick and connect them to the BOOST Hub, as shown here:

    Figure 10.4

  3. Take a 1x2 plate and connect it to one of the green bricks placed on the BOOST Hub. Then, take a 1x4 plate and place it onto the other two green-colored bricks, as shown here:

    Figure 10.5

  4. Now, take a 1x2 brick and connect it to the 1x2 plate, as follows:

    Figure 10.5

  5. Then, change the side of the model. Take three 1x2 bricks with cross holes and one 1x6 brick and connect them to the BOOST Hub, as shown here:

    Figure 10.7

  6. Take one 1x2 plate and one 1x4 plate. Connect them to the previously attached 1x2 yellow-colored...

Let's code the robot to dispense candies based on different colors of LEGO brick detected

Take three different-colored LEGO bricks—blue, green, and yellow. Now, through trial and error, find out the number of rotations needed by the motor to dispense one piece of candy, and note it here:

Rotations of port C = ________

Activity #1

Write a program to dispense one piece of candy when the color sensor senses a yellow brick. Display Yellow Brick Detected before dispensing. Once the candy is dispensed, display One Candy Dispensed, as illustrated in the following screenshot:

Figure 10.61 – Sample code

Let's move on to the next activity, to add different dispensing actions to the robot for different colors detected.

Activity #2

Write a program to dispense two pieces of candy when the color sensor senses a blue brick. Display Blue Brick Detected before dispensing. Once the candy is dispensed, display Two Candies Dispensed.

Activity...

Time for a challenge

Challenge #1

Write one single program for the candy dispenser to dispense, as follows:

  1. One piece of candy when yellow is detected
  2. Two pieces of candy when blue is detected
  3. Three pieces of candy when green is detected

Try to add unique features such as display and sound blocks to make this activity more interactive and user-friendly. You will have to use if-else conditions to set up the logic for this activity. You can consider displaying money by the application of variables and see how much a single user can spend.

Summary

In this chapter, you learned how a candy dispenser works. Instead of cents, you used the property of a color sensor to dispense different amounts of candy based on distinct colors detected. You also learned how to build a simple dispensing mechanism using gears and other LEGO elements that can be effectively used for other applications as well. In the next chapter, you will be building an automatic color sorter using a conveyor belt. Again, this is an industrial application used widely for various tasks related to sorting. You will enhance your coding skills using a color sensor and add multiple conditions for the robot to check and act upon.

Further reading

PEZ candy dispensers were extremely popular in the 1990s. Read more about them at https://www.mentalfloss.com/article/70010/10-indispensable-facts-about-pez.

You can build such dispensers for your home for various purposes, not only for candy, by applying your construction skills.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Build and Code Creative Robots with LEGO BOOST
Published in: Nov 2021Publisher: PacktISBN-13: 9781801075572
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ashwin Shah

Ashwin Shah is an Electronics and Communication engineer from India. Teaching is his passion and he started working at the tender age of 16 as a doubt solver for younger kids at after-school classes. Today, he is a successful edupreneur who runs his own STEM-based robotics, coding, and IoT institute - RoboFun Lab. Being one of the pioneers of STEM education in India, he has taught over 3000 students and trained 200+ educators thus far. He has trained 100+ teams for prestigious STEM competitions with 50+ national and 12+ international awards in the bag. Most of his students are now pursuing careers/education in STEM at prominent universities. Ashwin was a state-level badminton player during his college days.
Read more about Ashwin Shah