{"id":1040,"date":"2023-06-20T09:32:45","date_gmt":"2023-06-20T09:32:45","guid":{"rendered":"https:\/\/learnlearn.uk\/microbit\/?page_id=1040"},"modified":"2023-06-20T09:32:45","modified_gmt":"2023-06-20T09:32:45","slug":"microbit-line-following-robot-code-2-servos-using-a-b-calibration","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/","title":{"rendered":"Microbit Line Following Robot Code 2 Servos using A\/B calibration"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Introduction<\/h3>\n<p>One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the values for the high (when the LDR is over white) and low(when the LDR is over black) then you will have to change your program code to make it work with the new light values.<\/p>\n<p>A simple solution to this is to create calibration code that allows you to set the high and low values when the program first starts.<\/p>\n<p>This algorithm works by:<\/p>\n<ol>\n<li>Put both sensors over a white area and then press the A button. This records the high value for the 0 and 1 pins<\/li>\n<li>Put both sensors over the black and then press the B button. This records the low values.<\/li>\n<li>Calculate the half-way point between the high and low values for each sensor and save them in a middle value variable<\/li>\n<li>Start the main loop by calling the &#8216;Go&#8217; function.<\/li>\n<li>If Pin1 is less than pin1 middle value, turn left<\/li>\n<li>Else if Pin0 is less than pin1 middle value, turn right<\/li>\n<li>Else move forward.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Blocks<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Makecode Blocks<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-1041\" src=\"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code.png\" alt=\"\" width=\"1497\" height=\"752\" srcset=\"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code.png 1497w, https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code-300x151.png 300w, https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code-1024x514.png 1024w, https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code-768x386.png 768w\" sizes=\"(max-width: 1497px) 100vw, 1497px\" \/><\/p>\n\n<\/div><h2 class=\"tabtitle\">Python Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Python Code<\/h3>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\">pin0high <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\npin1high <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\npin0low <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\npin1low <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\nmiddle0 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\nmiddle1 <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">on_button_pressed_a<\/span>():\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> pin0high, pin1high\r\n    pin0high <span style=\"color: #333333;\">=<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P0)\r\n    pin1high <span style=\"color: #333333;\">=<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P1)\r\n<span style=\"color: #007020;\">input<\/span><span style=\"color: #333333;\">.<\/span>on_button_pressed(Button<span style=\"color: #333333;\">.<\/span>A, on_button_pressed_a)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">on_button_pressed_b<\/span>():\r\n    <span style=\"color: #008800; font-weight: bold;\">global<\/span> pin0low, pin1low, middle0, middle1\r\n    pin0low <span style=\"color: #333333;\">=<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P0)\r\n    pin1low <span style=\"color: #333333;\">=<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P1)\r\n    middle0 <span style=\"color: #333333;\">=<\/span> (pin0high <span style=\"color: #333333;\">-<\/span> pin0low) <span style=\"color: #333333;\">\/<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">+<\/span> pin0low\r\n    middle1 <span style=\"color: #333333;\">=<\/span> (pin1high <span style=\"color: #333333;\">-<\/span> pin1low) <span style=\"color: #333333;\">\/<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">+<\/span> pin1low\r\n    go()\r\n<span style=\"color: #007020;\">input<\/span><span style=\"color: #333333;\">.<\/span>on_button_pressed(Button<span style=\"color: #333333;\">.<\/span>B, on_button_pressed_b)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">go<\/span>():\r\n    <span style=\"color: #008800; font-weight: bold;\">while<\/span> <span style=\"color: #007020;\">True<\/span>:\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P1) <span style=\"color: #333333;\">&lt;<\/span> middle1:\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P2, <span style=\"color: #0000dd; font-weight: bold;\">80<\/span>)\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P8, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n        <span style=\"color: #008800; font-weight: bold;\">elif<\/span> pins<span style=\"color: #333333;\">.<\/span>analog_read_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P0) <span style=\"color: #333333;\">&lt;<\/span> middle0:\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P2, <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>)\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P8, <span style=\"color: #0000dd; font-weight: bold;\">60<\/span>)\r\n        <span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P2, <span style=\"color: #0000dd; font-weight: bold;\">80<\/span>)\r\n            pins<span style=\"color: #333333;\">.<\/span>analog_write_pin(AnalogPin<span style=\"color: #333333;\">.<\/span>P8, <span style=\"color: #0000dd; font-weight: bold;\">70<\/span>)\r\n        basic<span style=\"color: #333333;\">.<\/span>pause(<span style=\"color: #0000dd; font-weight: bold;\">50<\/span>)\r\n<\/pre>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the values for the high (when the LDR is over white) and low(when the LDR is over black) then you will have to change your program code&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Microbit Line Following Robot Code 2 Servos using A\/B calibration<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk<\/title>\n<meta name=\"description\" content=\"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk\" \/>\n<meta property=\"og:description\" content=\"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\" \/>\n<meta property=\"og:site_name\" content=\"Microbit\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\",\"url\":\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\",\"name\":\"Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#website\"},\"datePublished\":\"2023-06-20T09:32:45+00:00\",\"dateModified\":\"2023-06-20T09:32:45+00:00\",\"description\":\"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Microbit Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/microbit\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Microbit Line Following Robot Code 2 Servos using A\/B calibration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#website\",\"url\":\"https:\/\/learnlearn.uk\/microbit\/\",\"name\":\"Microbit\",\"description\":\"Tutorials, Projects &amp; Challenges\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/microbit\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#organization\",\"name\":\"Microbit\",\"url\":\"https:\/\/learnlearn.uk\/microbit\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2019\/06\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2019\/06\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Microbit\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/microbit\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk","description":"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/","og_locale":"en_GB","og_type":"article","og_title":"Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk","og_description":"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the","og_url":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/","og_site_name":"Microbit","og_image":[{"url":"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2023\/06\/line-following-robot-blocks-code.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/","url":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/","name":"Microbit Line Following Robot Code 2 Servos using A\/B calibration - Microbit - LearnLearn.co.uk","isPartOf":{"@id":"https:\/\/learnlearn.uk\/microbit\/#website"},"datePublished":"2023-06-20T09:32:45+00:00","dateModified":"2023-06-20T09:32:45+00:00","description":"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/microbit\/microbit-line-following-robot-code-2-servos-using-a-b-calibration\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Microbit Unit Home","item":"https:\/\/learnlearn.uk\/microbit\/"},{"@type":"ListItem","position":2,"name":"Microbit Line Following Robot Code 2 Servos using A\/B calibration"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/microbit\/#website","url":"https:\/\/learnlearn.uk\/microbit\/","name":"Microbit","description":"Tutorials, Projects &amp; Challenges","publisher":{"@id":"https:\/\/learnlearn.uk\/microbit\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/microbit\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/microbit\/#organization","name":"Microbit","url":"https:\/\/learnlearn.uk\/microbit\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/microbit\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2019\/06\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/microbit\/wp-content\/uploads\/sites\/2\/2019\/06\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Microbit"},"image":{"@id":"https:\/\/learnlearn.uk\/microbit\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/microbit\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction One of the problems with the simple version of the robot control code is that if the ambient light changes from one day to the next then the values for the high (when the LDR is over white) and low(when the LDR is over black) then you will have to change your program code&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/pages\/1040"}],"collection":[{"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/comments?post=1040"}],"version-history":[{"count":1,"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/pages\/1040\/revisions"}],"predecessor-version":[{"id":1042,"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/pages\/1040\/revisions\/1042"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/microbit\/wp-json\/wp\/v2\/media?parent=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}