Hungry mode

This mode is a review of the Hungry mode we programmed for the Timmyton in the last chapter, as it is fairly similar. The code for this mode will go inside the true case of the color sensor switch.

The first few blocks will initialize the operating mode. Add a display block that prints Hungry Mode to the EV3 screen, a brick LED block that shuts the LED's off, and a switch (Color Sensor | Measure | Color, port 3). After the switch, place a move tank block that turns motors  B and  C off:

But wait, what is that blue block after the display block? That is a comment. What does it do? Nothing! That's right. Comments are a way for a programmer to leave notes within a program and they do not affect the way that the robot executes the code. Comments are extremely useful because they can store information about what the program is doing at that point. Here, a comment is used as a label to indicate that this is the section of the program that controls Hungry mode. Using comments is not only a good programming habit, but it becomes increasingly important as the program becomes more complex. In a text-based programming language such as C, Python, or Java, a comment can even be used to temporarily remove a section of code, which makes them a great tool for debugging!

Use comments throughout your code; they will not only make your program easier to navigate, but will also help someone who is not familiar with your code understand it more easily. Commenting code is a practice used by the most successful professional programmers, so start getting into the habit of adding them today!

Now, we should set up that switch. Add a few more cases and assign each case to a color that you want Grunt to react to. Make sure that you also include a null case that executes when no color is detected and make it the default case:

Inside each of the switch cases, we will program Grunt's reaction to the assigned color. You have a lot of freedom to program some creative reactions to the different color blocks the user may feed him.

Here are some sample reactions Grunt can respond with when fed a certain color. Each time he is fed a block, the brick LED lights up to indicate whether he likes the food, he says the color of the block, and he reacts.

If he is fed a blue block (a type of food he likes), the brick LED will turn green and Grunt will express his approval by saying the color name and saying Mmmm, then make a crunching sound while he chews on it for a few seconds. The loop block moves his jaws to simulate a chewing effect. Then, he opens his mouth to let the block drop out in anticipation for the next block of food and closes his mouth:

Note that Hungry mode uses some custom sound files, which were made using the Sound Editor within the EV3 programming environment. The Sound Editor is found in the Tools menu. Using the Sound Editor is fairly straightforward; it allows you to record, import, and edit sounds, then save them for use in your project.

Red is another type of food that Grunt enjoys eating. If presented with a red block, he will exclaim Yum!, and react in a similar manner:

Grunt does not like yellow blocks! If he is fed one, the brick LED's will light up orange, and he will express his disapproval by spitting the block out:

Grunt also dislikes green foods and will reject them in a similar fashion:

A brown block elicits the most interesting reaction! The brick LEDs will light up red, and Grunt will groan with an Ugh. Then, he will go berserk: he will throw both arms into the air, open his mouth, and aggressively shuffle around:

Finally, we have the null case, which tells Grunt to do nothing when no block is present in his mouth. It is somewhat redundant, but it acts as a fail-safe that keeps the program running smoothly:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.197