This Vending Machine project is one of the "bonus projects" available on the CD LEGO MINDSTORMS NXT 2.0 by Example from nxtprograms.com. A preview and video of the project are available on the website (see video below), and full building instructions and the fully commented NXT-G program are available on the CD.
Built with a single NXT 2.0 kit, the machine displays four LEGO parts for sale on the rack in front. The colored balls act as different coin values, with the color sensor detecting them as they are inserted. Using the display and buttons on the NXT, you choose your item from a menu on screen, then you insert balls to pay for it. If you inserted enough money, the selected item is dispensed down the slide in the front, and if you inserted extra money, the machine automatically computes and dispenses the correct amount of change via a hidden change reservoir into the change cup in front. The machine also handles various other error cases such as not inserting enough money, inserting too much money for the change reservoir to handle, and invalid coins. The user can cancel a transaction via a "coin return" lever, which returns the coins inserted and informs the machine via a touch sensor.
The program is in NXT-G, and since the task is longer and more complex than a typical NXT-G program, it is split into several My Blocks to break up the task into various parts, such as processing the on-screen menu, detecting coins, computing change, handling errors, etc. The My Blocks that process the on-screen user interface are "general purpose" (not specific to the Vending Machine project) and could be re-used for similar user-interface tasks in other programs.
Here is a short video of the Vending Machine doing a transaction:
Built with a single NXT 2.0 kit, the machine displays four LEGO parts for sale on the rack in front. The colored balls act as different coin values, with the color sensor detecting them as they are inserted. Using the display and buttons on the NXT, you choose your item from a menu on screen, then you insert balls to pay for it. If you inserted enough money, the selected item is dispensed down the slide in the front, and if you inserted extra money, the machine automatically computes and dispenses the correct amount of change via a hidden change reservoir into the change cup in front. The machine also handles various other error cases such as not inserting enough money, inserting too much money for the change reservoir to handle, and invalid coins. The user can cancel a transaction via a "coin return" lever, which returns the coins inserted and informs the machine via a touch sensor.
The program is in NXT-G, and since the task is longer and more complex than a typical NXT-G program, it is split into several My Blocks to break up the task into various parts, such as processing the on-screen menu, detecting coins, computing change, handling errors, etc. The My Blocks that process the on-screen user interface are "general purpose" (not specific to the Vending Machine project) and could be re-used for similar user-interface tasks in other programs.
Here is a short video of the Vending Machine doing a transaction: