{"id":2747,"date":"2021-08-22T07:18:23","date_gmt":"2021-08-22T07:18:23","guid":{"rendered":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/?page_id=2747"},"modified":"2023-01-21T05:24:53","modified_gmt":"2023-01-21T05:24:53","slug":"if-then-else-conditions","status":"publish","type":"page","link":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/if-then-else-conditions\/","title":{"rendered":"IF Then Else conditions."},"content":{"rendered":"\n<p><a title=\"Visit Zacobria &amp; Webshop &amp; Universal-Robots solutions.\" href=\"https:\/\/www.zacobria.com\/automation\/webshop\/\">Visit Zacobria Webshop<\/a><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Use of &#8211; IF Then Else conditions.<\/h1>\n\n\n\n<p>In programming one of the most used features is the IF or conditions based statement because that\u2019s the hearth of automatic choice of conditions and this is very often then main purpose of computer program.<\/p>\n\n\n\n<p>In the UR we have the IF statement to use.<\/p>\n\n\n\n<p><strong><em>Note: Comparison of Strings are not available in UR.<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_1\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_1.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_1\" class=\"wp-image-394\"\/><\/a><\/figure>\n\n\n\n<p>Insert an IF statement into the program.<\/p>\n\n\n\n<p>Define the IF statement by clicking on the formula button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_2\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_2.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_2\" class=\"wp-image-395\"\/><\/a><\/figure>\n\n\n\n<p>This will bring up a screen where we can choose conditions for the IF statement to check on. In this case we choose an Input to check on.<\/p>\n\n\n\n<p>In this case it means that if Input 0 is true the IF statement is true and the lines in the IF statement will be executed.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_3\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_3.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_3\" class=\"wp-image-396\"\/><\/a><\/figure>\n\n\n\n<p>Below the IF expression definition field is a check box called \u201dCheck expression continuously\u201d.<\/p>\n\n\n\n<p>If this is checked the robot will check IF \u201ddigital_in[0]\u201d= True is true also during the execution of the program lines in the IF statement. This means that if the \u201d digital_in[0]\u201d becomes 0 during the IF execution then the rest of the program lines inside the IF will not be executed. This can lead to unintended function if not handled correct.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_4\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_4.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_4\" class=\"wp-image-397\"\/><\/a><\/figure>\n\n\n\n<p>Below the IF statement program lines is to be inserted that will be executed if the IF statement is True. This can also be considered a program inside the IF statement and it can be as big as our main program, but often this is short and to set outputs that is dependant of the IF condition.<\/p>\n\n\n\n<p>In this case we choose to set a output high \u2013 wait 2 seconds and set it low again e.g. starting a conveyor for 2 seconds.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check expression continuously.<\/h2>\n\n\n\n<p>This is an explanation for what happens if the \u201cCheck expression continuously\u201d is not handled carefully.<\/p>\n\n\n\n<p>Instead of an Input we will check on a variable instead because then it is easy to see the meaning and difference.<\/p>\n\n\n\n<p>Consider this small program.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_5\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_5.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_5\" class=\"wp-image-398\"\/><\/a><\/figure>\n\n\n\n<p>Initially the variable \u201cvar_1\u201d is set to 0 \u2013 which means the IF statement is false and will not be executed \u2013 until we reach after Waypoint_3 \u2013 then the \u201cvar_1\u201d is set to 1 and therefore the IF statement is true and we be started to be executed.<\/p>\n\n\n\n<p>But in this case we have set the \u201cCheck expression continuously\u201d checked.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_6\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_6.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_6\" class=\"wp-image-399\"\/><\/a><\/figure>\n\n\n\n<p>What happens now \u2013 is that the first few lines under the IF statement will be executed e.g. the Digital output 2 will go on, but when we reach \u201cvar_1 = 0\u201d in the IF statement, then we actually change the condition of the IF expression check \u2013 which now becomes false \u2013 and therefore the program jumps out of this IF routine already \u2013 and the digital output 7 will never go on.<\/p>\n\n\n\n<p>Sometimes we will use Subroutines, but the effect will be the same.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/if-then-else-conditions\/universal_robots_zacobria_programming_if_then_else_7\/\"><img decoding=\"async\" src=\"http:\/\/www.zacobria.com\/universal-robots-zacobria-forum-hints-tips-how-to\/wp-content\/uploads\/2012\/12\/universal_robots_zacobria_programming_if_then_else_7.jpg\" alt=\"universal_robots_zacobria_programming_if_then_else_7\" class=\"wp-image-401\"\/><\/a><\/figure>\n\n\n\n<p>This program will do exactly like the previous program, but we have used SubProgram method and same thing &#8211; the Digital Output 7 will never go on although it says clearly so in the SubProgram, but the IF expression is already False.<\/p>\n\n\n\n<p>Here we have used a variable to show the effect, but it could as well have been a Digital Input we have used for the IF expression check \u2013 and the same will happen if the Digital Input state change while the program are executing the IF program lines \u2013 if the IF expression becomes False during this time \u2013 the rest of the program lines will not be executed. If such state changes right at the moment the IF was true \u2013 (but now false) none of the line in the IF statement are executed.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Combinations of expressions:<\/h1>\n\n\n\n<p>In a conditional expression you can have combinations e.g.<\/p>\n\n\n\n<p>IF input_1 = High AND input_2 = Low<\/p>\n\n\n\n<p>Then do something<\/p>\n\n\n\n<p>But make sure you are using normal mathematically rules \u2013 so use of parentheses are a good thing like this<\/p>\n\n\n\n<p>IF (input_1 = High) AND (input_2 = Low)<\/p>\n\n\n\n<p>Then do somethin<\/p>\n\n\n\n<p>However instead of long mathematically statements \u2013 then better have more IF statements.<\/p>\n\n\n\n<p><strong>Disclaimer:<\/strong> While the Zacobria Pte. Ltd. believes that information and guidance provided is correct, parties must rely upon their skill and judgement when making use of them. Zacobria Pte. Ltd. assumes no liability for loss or damage caused by error or omission, whether such an error or omission is the result of negligence or any other cause. Where reference is made to legislation it is not to be considered as legal advice. Any and all such liability is disclaimed.<\/p>\n\n\n\n<p>If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.<\/p>\n\n\n\n<p>Author:<br><a href=\"https:\/\/plus.google.com\/u\/0\/116832821661215606670?rel=author\">By Zacobria Lars Skovsgaard<\/a><br>Accredited 2015-2018 Universal Robots support Centre and Forum.<br><br><br><\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/share\">Tweet<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/zacobria\">Follow @zacobria<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visit Zacobria Webshop Use of &#8211; IF Then Else conditions. In programming one of the most used features is the IF or conditions based statement because that\u2019s the hearth of automatic choice of conditions and this is very often then&#8230; <\/p>\n<div class=\"more-link-container\"><a class=\"more-link\" href=\"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/if-then-else-conditions\/\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":2481,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"templates\/full-width-page.php","meta":{"footnotes":""},"class_list":["post-2747","page","type-page","status-publish","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/comments?post=2747"}],"version-history":[{"count":4,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2747\/revisions"}],"predecessor-version":[{"id":3090,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/pages\/2747\/revisions\/3090"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media\/2481"}],"wp:attachment":[{"href":"https:\/\/www.zacobria.com\/universal-robots-knowledge-base-tech-support-forum-hints-tips-cb2-cb3\/index.php\/wp-json\/wp\/v2\/media?parent=2747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}