{"id":2758,"date":"2014-10-13T12:17:29","date_gmt":"2014-10-13T19:17:29","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=2758"},"modified":"2021-06-17T08:41:46","modified_gmt":"2021-06-17T15:41:46","slug":"simon-says-with-leds","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/simon-says-with-leds\/","title":{"rendered":"Simon Says&#8230; with LEDs!"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>At one point or another, we have all played the &#8220;Simon Says&#8221; game. In this game, one person, Simon (or Susan, Chad, or whoever happens to be the leader), will say &#8220;Simon says&#8221; and tell all of the other players to do something, such as raise their left hand. The catch in this game is that if the leader tells the other players to do something without saying the words &#8220;Simon says&#8221; and the other players do it anyway, they&#8217;re out. I personally really enjoy playing Simon Says, but I thought it could even be more fun if you could play Simon Says with a bunch of LEDs&#8230;<\/p>\n<figure id=\"attachment_2759\" aria-describedby=\"caption-attachment-2759\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2759\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298.jpg\" alt=\"Simon Says Project\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298.jpg 4000w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298-600x450.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298-1024x768.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298-225x168.jpg 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1298-800x600.jpg 800w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2759\" class=\"wp-caption-text\">Simon Says project.<\/figcaption><\/figure>\n<p>So that&#8217;s exactly what I did. Using a set of nine\u00a0LEDs, two <a title=\"PmodBTN\" href=\"https:\/\/digilent.com\/blog\/Products\/Detail.cfm?NavPath=2,401,507&amp;Prod=PMOD-BTN\" target=\"_blank\" rel=\"noopener\">PmodBTN<\/a>s (four user buttons on each), a single additional button, and a shift register, I was able to create Simon Says with it being just as fun as I was hoping it would be. Granted, the <a title=\"chipKIT uC32\" href=\"https:\/\/digilent.com\/blog\/Products\/Detail.cfm?NavPath=2,892,1035&amp;Prod=CHIPKIT-UC32\" target=\"_blank\" rel=\"noopener\">chipKIT uC32<\/a> I designed the game on always gets to be Simon and pressing buttons to make LEDs light up in the same pattern that the uC32 made them isn&#8217;t a super intensive cardio workout, but I&#8217;m okay with that.<\/p>\n<p>&nbsp;<\/p>\n<p>What some of you may be interested in is how I made the game work&#8211;how the game compared the user guess to the original pattern, how the person playing the game knew if they got the answer right or wrong, and how the microcontroller came up with the random patterns in the first place.<\/p>\n<p>&nbsp;<\/p>\n<p>The comparison between the user guess and the original pattern of LEDs that was provided was done through two <a title=\"Arrays \u2014 They\u2019re Not So Scary\" href=\"https:\/\/digilent.com\/blog\/index.php\/arrays-theyre-not-so-scary\/\">arrays<\/a>. One array kept track of what numbers the uC32 had generated, which corresponded to which LEDs were lit up in the pattern. A second array similarly kept track of which LED was lit up through the player pressing the corresponding button. Once the player was done lighting up the LEDs in the (hopefully) correct order, they press the isolated button off to the far left to light up the single yellow LED, thus submitting their &#8220;final answer&#8221;.<\/p>\n<figure id=\"attachment_2760\" aria-describedby=\"caption-attachment-2760\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2760\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332.jpg\" alt=\"The &quot;final answer&quot; button\" width=\"600\" height=\"449\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332.jpg 2001w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332-600x448.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332-1024x766.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1332-225x168.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2760\" class=\"wp-caption-text\">The &#8220;final answer&#8221; button.<\/figcaption><\/figure>\n<p>The uC32 then compares the two arrays, number by number, to see if they match. If the arrays do match, the four green LEDs are flashed a few times to show the player that they got the pattern right and the length of the pattern that they have to match increases by 1.<\/p>\n<figure id=\"attachment_2761\" aria-describedby=\"caption-attachment-2761\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2761\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344.jpg\" alt=\"Correct!\" width=\"602\" height=\"451\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344.jpg 3001w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344-600x449.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344-1024x767.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1344-225x168.jpg 225w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-2761\" class=\"wp-caption-text\">Correct!<\/figcaption><\/figure>\n<p>However, if the two arrays do not match, the red LEDs will flash and the length of the pattern goes back down to just one LED. To make sure that the two arrays aren&#8217;t accidentally different somehow, the values of the arrays are both internally reset to all zeros before a new pattern of LEDs (of any length) is displayed.<\/p>\n<figure id=\"attachment_2762\" aria-describedby=\"caption-attachment-2762\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2762\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336.jpg\" alt=\"Not the correct answer\" width=\"601\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336.jpg 3001w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336-600x449.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336-1024x767.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1336-225x168.jpg 225w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption id=\"caption-attachment-2762\" class=\"wp-caption-text\">Not the correct answer.<\/figcaption><\/figure>\n<p>But how did the microcontroller come up with the &#8220;random&#8221; patterns? Electronic chips, such as the PIC32 chip from <a title=\"Microchip website\" href=\"http:\/\/www.microchip.com\/\" target=\"_blank\" rel=\"noopener\">Microchip<\/a>, do not have any way to arbitrarily come up with a number of some kind, but what the chip on the uC32 does have is a very long sequence of numbers. This sequence of numbers is accessed through the random() function, which chooses a number based on the bounds that you give it. For example, with the eight LEDs that could be lit up, the random() function is written as random(1,9) to choose a number ranging from 1 to 8. The upper number given in the random() function is presumed to be 1 greater than the desired output range of the random() function.<\/p>\n<p>&nbsp;<\/p>\n<p>While this is nice, we would find that every time we play Simon Says that the pattern of LEDs that light up will always be the same because the number sequence which random() pulls from is always the same. To get around this and make the game more &#8220;random&#8221; we can call the randomSeed() function before using the random() function. RandomSeed() will tell the microcontroller where to start pulling numbers from in the number sequence based on what value we give it. Thus, we could tell randomSeed() to get a value based off the analog noise of one of the I\/O pins on the microcontroller, getting us a &#8220;random&#8221; place to start in our number sequence with the random() function.<\/p>\n<figure id=\"attachment_2771\" aria-describedby=\"caption-attachment-2771\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-2771\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual.png\" alt=\"The randomSeed chooses a spot somewhere in the number sequence\" width=\"600\" height=\"450\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual.png 2000w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual-600x450.png 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual-1024x768.png 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/randomSeed-visual-225x168.png 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2771\" class=\"wp-caption-text\">The randomSeed chooses a spot somewhere in the number sequence.<\/figcaption><\/figure>\n<p>However, it has been my experience that the electrical noise on a pin tends to be restricted to a very small range of numbers, such as 0 to 5, which is not quite as arbitrary as I would like. So, I instead have the randomSeed() collect its number from the function millis(), which reports the value on the continually running time counter that lasts for nearly 50 days. By using millis(), where the random() function pulls its values from is inherently dependent on how long the player takes to submit their answer. Presuming the player is human instead of another microcontroller, this method guarantees that a different place in the number sequence will be chosen every time the game is played.<\/p>\n<p>&nbsp;<\/p>\n<p>You can get the code I used for the game in the text file below (right click on it to open it in a new tab). If you want a little more instruction on how to create the circuit itself, feel free to check out my Instructable for it here. I&#8217;ve also included a link below to my code for the project.<\/p>\n<p>&nbsp;<\/p>\n<p><a title=\"Right click on this and choose &quot;Open link in a new tab&quot;\" href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/Simon-Says-Code.txt\" target=\"_blank\" rel=\"noopener\">Simon Says Code<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Keep coming back to the Digilent Blog to see the other cool things that are always happening here at Digilent!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-2758 jlk' data-task='like' data-post_id='2758' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-2758 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-2758 jlk' data-task='unlike' data-post_id='2758' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-2758 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-2758 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>At one point or another, we have all played the &#8220;Simon Says&#8221; game. In this game, one person, Simon (or Susan, Chad, or whoever happens to be the leader), will say &#8220;Simon says&#8221; and tell all of the other players to do something, such as raise their left hand. The catch in this game is that if the leader tells the other players to do something without saying the words &#8220;Simon says&#8221; and the other players do it anyway, they&#8217;re out. I personally really enjoy playing Simon Says, but I thought it could even be more fun if you could play Simon Says with a bunch of LEDs&#8230;<\/p>\n","protected":false},"author":17,"featured_media":2838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4327],"tags":[104],"ppma_author":[4469],"class_list":["post-2758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","tag-project-2"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/09\/IMG_1330.jpg","authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/509febded809c5de0423909f6ae6587f?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Colvin","last_name_2":"","first_name":"James","first_name_2":"","job_title":"Applications Engineer \/ Technical Support Engineer \/ Product Support Engineer \/ Technical Writer \/ the person to bother about T&M and JTAG when the senior design engineer is busy","description":"A local Digilent employee who is sometimes tricked into making other content besides documentation and supporting customers on the Digilent Forum, but then I get to write a little more informally so that's a plus. \r\n\r\nA sassy engineer, lover of puns and dad jokes, father and husband. \r\n\r\nI know both way too much and simultaneously almost nothing about a number of nerdy topics. If you want to hear me rant, ask me what data rate USB C operates at."}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=2758"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/2758\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/2838"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=2758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=2758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=2758"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}