{"id":12482,"date":"2017-10-19T16:34:15","date_gmt":"2017-10-19T05:34:15","guid":{"rendered":"http:\/\/legoeng.local\/?p=12482"},"modified":"2019-04-02T15:17:47","modified_gmt":"2019-04-02T04:17:47","slug":"how-to-wait-for-more-than-one-sensor-condition","status":"publish","type":"post","link":"http:\/\/legoeng.local\/how-to-wait-for-more-than-one-sensor-condition\/","title":{"rendered":"How to Wait for More than One Sensor Condition"},"content":{"rendered":"
The Wait block is one of the simplest blocks in the EV3 Software, and provides one of the simplest flow control structures. Its basic function is to pause the execution of a program until a particular condition is met, and then\u00a0continue with the program. For example,\u00a0“wait until the touch sensor is pressed”<\/em> or “wait for three seconds”<\/em>.<\/p>\n But what if you want a program to wait until either of two (or more) possible conditions occur? For example, “wait until EITHER the color sensor detects black\u00a0OR\u00a0the touch sensor is pressed”<\/em>.<\/p>\n This learning sequence explains how the Wait block is built in the EV3 Software, its advantages and limitations, and how to expand its programming functions to accommodate multiple conditions.<\/p>\n Through this learning sequence, students will\u00a0gain a deeper understanding of:<\/p>\n In the following example, the program waits until a touch sensor is pressed to start a motor for one rotation.<\/p>\n This flow control structure is very useful for keeping a simple program short. Interestingly, many other programming languages do not have anything directly equivalent.<\/p>\n How could we achieve the same function without using a Wait block?<\/em> Note that a loop can be empty.<\/p>\n If we want to run a motor forever, can we do it with a wait block?<\/em> If we want to use the touch sensor to stop the motor accurately, we can improve this program by adding a Motor Off block after the Wait Touch block. Set the Motor Off block to Brake at End. For example…<\/p>\n How can we achieve the same function with a loop?<\/em><\/p>\n A Loop block can also be set to Unlimited. That is, the loop will never end (unless a Loop Interrupt block is used in another part of the program).<\/p>\n This solution is perhaps more elegant than the earlier one using a Wait block, because it makes explicit that the loop runs forever. Note also that a Wait block doesn’t have an option to set it to Unlimited.<\/p>\n Consider the following program and how it differs from the previous one.<\/p>\n Both programs are equivalent in function, so what are the differences? Let’s say we want to wait until EITHER a touch sensor is pressed OR a black color is detected, THEN start a motor.<\/p>\n Can we wait for more than one condition using Wait blocks? We need to have a separate program sequence for each sensor and use Stop Program blocks to force the program to finish once either of the sequences is done\u2026<\/p>\n This program might work fine for some situations, but it has some problems…<\/p>\n So how can we write a better program?<\/em> A Loop block can be set to take a Logic value. This way you can use Logic blocks to control it.\u00a0This is particularly useful if we want to handle more than one condition.<\/p>\n For example, to wait until EITHER a touch sensor is pressed OR a black color is detected, we can wire the results of the sensors into a Logic OR block that, in turn, is wired into the terminating condition of the Loop structure…<\/p>\n Alternatively, we could wait until both<\/em> conditions are met using a Logic AND block.<\/p>\n If we want to add more conditions, we just need to use more Logic blocks to connect in a chain the sensor outputs of their compare results to the Logic blocks, and connect the last one to the logic input plug of the Loop block.<\/p>\n The Wait block is one of the simplest blocks in the EV3 Software, and provides one of the simplest flow control structures. Its basic function is to pause the execution of a program until a particular condition is met, and then\u00a0continue with the program. For example,\u00a0“wait until the touch sensor is pressed” or “wait for […]<\/p>\n","protected":false},"author":412,"featured_media":12590,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[159],"tags":[143,582,583,146,177,393],"_links":{"self":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts\/12482"}],"collection":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/users\/412"}],"replies":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/comments?post=12482"}],"version-history":[{"count":0,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/posts\/12482\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/media\/12590"}],"wp:attachment":[{"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/media?parent=12482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/categories?post=12482"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legoeng.local\/wp-json\/wp\/v2\/tags?post=12482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
Exercise 1: Wait until a touch sensor is pressed<\/h2>\n
<\/a>
\nA Wait block is actually just a loop that repeats until a particular condition is met. It’s hidden from the user, but if you could see how the Wait Touch block is implemented within the EV3 Software, it would be equivalent to the Loop block in this example…<\/p>\n<\/a>
\nYes, but not elegantly. We need to put the Motor block before a Wait block that is set to a condition that will be never met. For example…<\/p>\n<\/a>
<\/a>
<\/a>
<\/a>
<\/a>
\n<\/em>The first one only runs the Motor block once, whereas the second runs the Motor block at each iteration of the loop. Although this is less efficient, it is perhaps easier to read and understand. This programming style may be useful to debug long programs.<\/p>\nExercise 2: Wait for more than one condition<\/h2>\n
\n<\/em>We could do this, but it’s not elegant and has its limitations.<\/p>\n<\/a>
\n
\nWe need to use a loop.<\/p>\n<\/a>
<\/a>
T<\/em><\/a>o find more examples, videos and an introduction course for teachers to Mindstorms EV3 with a translation to Catalan and Spanish, visit the\u00a0BOGATECH <\/a>website.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"