
{"id":153,"date":"2017-04-08T23:06:35","date_gmt":"2017-04-08T13:06:35","guid":{"rendered":"http:\/\/bakke.online\/?p=153"},"modified":"2017-04-08T23:06:35","modified_gmt":"2017-04-08T13:06:35","slug":"arduino-traffic-lights-simulator-part-2","status":"publish","type":"post","link":"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-2\/","title":{"rendered":"Arduino traffic lights simulator, part 2"},"content":{"rendered":"<p>Welcome back.<\/p>\n<p>In the <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-1\/\">previous part<\/a>, I showed you how to connect the circuit for the traffic lights simulator. \u00a0This time, I&#8217;ll show you the sketch to control them.<\/p>\n<p>If you do not have the Arduino software installed already, it can be downloaded from the <a href=\"https:\/\/www.arduino.cc\/en\/Main\/Software\">Arduino<\/a> website. \u00a0You&#8217;ll need the Arduino IDE, I have not tested any of this with the Online IDE.<\/p>\n<p><!--more--><\/p>\n<p>To start, create a new sketch. \u00a0First, at the top of the sketch we&#8217;re going to add some defines to tell the compiler which pins we are using for the LEDs. \u00a0This lets us refer to the pins by \u00a0name, rather than number. \u00a0This way, if we change the pin numbers, there&#8217;s only one place to change, rather than all over the code.<\/p>\n<pre><span style=\"color: #808000;\">#define<\/span> RED_RIGHT    8\n<span style=\"color: #808000;\">#define<\/span> YELLOW_RIGHT 6\n<span style=\"color: #808000;\">#define<\/span> GREEN_RIGHT  5\n\n<span style=\"color: #808000;\">#define<\/span> RED_LEFT    9\n<span style=\"color: #808000;\">#define<\/span> YELLOW_LEFT 10\n<span style=\"color: #808000;\">#define<\/span> GREEN_LEFT  11<\/pre>\n<p>Review the previous part and confirm that your circuit have the LEDs connected to the pins shown:<\/p>\n<p><a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_185750_2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-147 size-medium\" src=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/20170408_185750_2-300x281.jpg\" width=\"300\" height=\"281\" \/><\/a><\/p>\n<p>The setup() function contains code that is run once, when the microcontroller starts. \u00a0We&#8217;re starting by configuring the 6 pins we&#8217;re using as outputs. \u00a0Once the pins are configured as output, we can switch an LED on by writing a HIGH to it, and off by writing a LOW. \u00a0To make sure the LEDs are all in a known state when the sketch starts, we&#8217;ll set them all to LOW.<\/p>\n<pre><span style=\"color: #33cccc;\">void<\/span> <span style=\"color: #808000;\">setup<\/span>() {\n  <span style=\"color: #999999;\">\/\/ Configure the LED pins as outputs<\/span>\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( RED_RIGHT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( YELLOW_RIGHT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( GREEN_RIGHT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( RED_LEFT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( YELLOW_LEFT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">pinMode<\/span>( GREEN_LEFT, <span style=\"color: #33cccc;\">OUTPUT<\/span> );\n\n  <span style=\"color: #999999;\">\/\/ Make sure all LEDs are off when the sketch starts<\/span>\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( RED_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( YELLOW_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( GREEN_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( RED_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( YELLOW_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( GREEN_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n}<\/pre>\n<p>After the setup() function has completed, the microcontroller will run the loop() function over and over again until it is powered down. \u00a0For a cyclical operation like a traffic light, we can let the loop() function take care of one cycle and it will automatically start the next cycle as soon as it reaches the end of the function.<\/p>\n<p>The first step is to set the right channel to red and left to green. \u00a0Then we&#8217;ll tell the microcontroller to wait for 15 seconds.<\/p>\n<pre><span style=\"color: #33cccc;\">void<\/span> <span style=\"color: #808000;\">loop<\/span>() {\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( RED_RIGHT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( GREEN_LEFT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">digitalWrite<\/span>( RED_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n\u00a0 <span style=\"color: #ff6600;\">delay<\/span>( 15000 ); \/\/ Left green, right red, 15 seconds<\/pre>\n<p>Don&#8217;t worry about the RED_LEFT being set to LOW, that&#8217;s there to help the transition from the end of the cycle back to the beginning.<\/p>\n<p>Next, after the 15 seconds are up, the left channel will go yellow for 3 seconds. \u00a0Notice that we&#8217;re not setting every pin every time, we&#8217;re only setting those pins that change.<\/p>\n<pre><span style=\"color: #ff6600;\"> digitalWrite<\/span>( YELLOW_LEFT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n <span style=\"color: #ff6600;\">digitalWrite<\/span>( GREEN_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n <span style=\"color: #ff6600;\">delay<\/span>( 3000 ); \/\/ Left yellow, 3 seconds<\/pre>\n<p>Now it&#8217;s time to set the left channel to red, but we can&#8217;t give the right channel the green light just yet. \u00a0We&#8217;ll keep both on red for 2 seconds.<\/p>\n<pre><span style=\"color: #ff6600;\"> digitalWrite<\/span>( RED_LEFT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n <span style=\"color: #ff6600;\">digitalWrite<\/span>( YELLOW_LEFT, <span style=\"color: #33cccc;\">LOW<\/span> );\n <span style=\"color: #ff6600;\">delay<\/span>( 2000 ); \/\/ Both red, 2 seconds<\/pre>\n<p>Then it&#8217;s finally time for the right channel to get the green light for 15 seconds.<\/p>\n<pre><span style=\"color: #ff6600;\"> digitalWrite<\/span>( RED_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n <span style=\"color: #ff6600;\">digitalWrite<\/span>( GREEN_RIGHT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n <span style=\"color: #ff6600;\">delay<\/span>( 15000 ); \/\/ Left red, right green, 15 seconds<\/pre>\n<p>Next, the right channel goes to yellow for 3 seconds.<\/p>\n<pre><span style=\"color: #ff6600;\"> digitalWrite<\/span>( GREEN_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n <span style=\"color: #ff6600;\">digitalWrite<\/span>( YELLOW_RIGHT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n <span style=\"color: #ff6600;\">delay<\/span>( 3000 ); \/\/ Left red, right yellow, 3 seconds<\/pre>\n<p>And just as earlier, we keep both channels on red for 2 seconds.<\/p>\n<pre><span style=\"color: #ff6600;\"> digitalWrite<\/span>( RED_RIGHT, <span style=\"color: #33cccc;\">HIGH<\/span> );\n <span style=\"color: #ff6600;\">digitalWrite<\/span>( YELLOW_RIGHT, <span style=\"color: #33cccc;\">LOW<\/span> );\n <span style=\"color: #ff6600;\">delay<\/span>( 2000 ); \/\/ Both red, 2 seconds\n}<\/pre>\n<p>And as we reach the end of loop(), the microcontroller will start from the top again. \u00a0Hence why we needed to set the left red LED to LOW in the first part of the cycle, otherwise it would stay on as the green LED comes on.<\/p>\n<p>That&#8217;s all. \u00a0Compile the sketch and upload it to the microcontroller.<\/p>\n<p><iframe loading=\"lazy\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/AT1WPqTlXIs?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>The full sketch can be downloaded from here:\u00a0<a href=\"https:\/\/www.bakke.online\/wp-content\/uploads\/2017\/04\/trafficlights.zip\">trafficlights.zip<\/a><\/p>\n<p>In the next part, I&#8217;ll show how to use a push button to control the cycling of the traffic lights and I&#8217;ll also introduce the concept of state tables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome back. In the previous part, I showed you how to connect the circuit for the traffic lights simulator. \u00a0This time, I&#8217;ll show you the sketch to control them. If you do not have the Arduino software installed already, it can be downloaded from the Arduino website. \u00a0You&#8217;ll need the Arduino IDE, I have not &hellip; <a href=\"https:\/\/www.bakke.online\/index.php\/2017\/04\/08\/arduino-traffic-lights-simulator-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Arduino traffic lights simulator, part 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,14,1],"tags":[8,13,15],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronics","category-uncategorized","tag-arduino","tag-beginners","tag-electronics"],"_links":{"self":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":0,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bakke.online\/index.php\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}