{"id":9713,"date":"2014-11-27T21:07:21","date_gmt":"2014-11-28T02:07:21","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=9713"},"modified":"2014-11-28T18:36:33","modified_gmt":"2014-11-28T23:36:33","slug":"simple-running-led-dice","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/simple-running-led-dice\/","title":{"rendered":"Running LED dice"},"content":{"rendered":"<p>Tons of LED dice projects with different output forms have been published online. The most common output configuration in those projects is a 3-1-3 setup (two rows of three LEDs and one LED at in the middle) of seven LEDs, which simulates the actual patterns of dots found on the six faces of a traditional dice. When it is rolled, one or more LEDs are selectively turned on to display a random number between 1 to 6. This project is about a similar LED dice but with a slightly different output form. It uses 6 LEDs which are arranged in a circular pattern and are labeled 1 through 6. They create a chasing effect when the dice is rolled. The chasing effect slows down gradually, and eventually stops at one of the six LEDs. The rolling is done by a gentle shaking of the dice horizontally. The LED dice is powered with a 3V coin cell battery and uses PIC12LF1822 microcontroller to generate a random number and drive the output LEDs.<\/p>\n<div id=\"attachment_9720\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=9720\" rel=\"attachment wp-att-9720\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9720\" class=\"size-full wp-image-9720\" alt=\"Running LED dice\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDicetitle.jpg\" width=\"585\" height=\"482\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDicetitle.jpg 585w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDicetitle-550x453.jpg 550w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><p id=\"caption-attachment-9720\" class=\"wp-caption-text\">Running LED dice<\/p><\/div>\n<p><strong><!--more-->Circuit diagram<\/strong><\/p>\n<p>The six LEDs are driven through 3 I\/O pins of PIC12LF1822 microcontroller using the Charlieplexing technique. Three 100 Ohm resistors are placed in series with the I\/O lines to limit the LED currents. A horizontally placed tilt switch (also known as vibration switch) is used to sense the shaking of the dice. A tilt switch is simply a\u00a0tube with a tiny conductive metal ball that rolls inside it, and has\u00a0two electrical contacts that are accessible from external leads.\u00a0When its tilted upright, the ball rolls onto the two metallic contacts, thus short-circuiting them together. The tilt sensor along with a pull up resistor generates an interrupt signal on GP2\/INT pin of the PIC12LF1822 microcontroller and the dice is rolled. A power on\/off slide switch is also included in the project. In case you forgot to turn it off, the PIC microcontroller will be programmed to automatically go to sleep mode to save the power consumption. A circular shape PCB is designed for this project. The CR2032 battery holder is placed on the bottom side of the PCB, whereas rest of the electronics and LEDs go on the top side. The design files can be downloaded from the link provided at the end of this section.<\/p>\n<div id=\"attachment_9717\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=9717\" rel=\"attachment wp-att-9717\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9717\" class=\"size-medium wp-image-9717\" alt=\"LED dice circuit (click to enlarge)\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/LED_dice_circuit-550x440.png\" width=\"550\" height=\"440\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/LED_dice_circuit-550x440.png 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/LED_dice_circuit.png 831w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><p id=\"caption-attachment-9717\" class=\"wp-caption-text\">LED dice circuit (click to enlarge)<\/p><\/div>\n<div id=\"attachment_9721\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=9721\" rel=\"attachment wp-att-9721\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9721\" class=\"size-full wp-image-9721\" alt=\"Dice is made on a circular PCB\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice.jpg\" width=\"585\" height=\"528\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice.jpg 585w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice-550x496.jpg 550w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><p id=\"caption-attachment-9721\" class=\"wp-caption-text\">Completed dice top view<\/p><\/div>\n<div id=\"attachment_9722\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=9722\" rel=\"attachment wp-att-9722\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9722\" class=\"size-full wp-image-9722\" alt=\"Battery goes to the bottom side of PCB\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice2.jpg\" width=\"585\" height=\"536\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice2.jpg 585w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/CompleteDice2-550x503.jpg 550w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><p id=\"caption-attachment-9722\" class=\"wp-caption-text\">Battery goes to the bottom side of PCB<\/p><\/div>\n<p><strong><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/LED_DICE.zip\">Download Eagle CAD Design Files<\/a><\/strong><\/p>\n<p><strong>Firmware<\/strong><\/p>\n<p>The LED dice firmware was developed using <a href=\"http:\/\/www.mikroe.com\/mikroc\/pic\/\" target=\"_blank\">mikroC Pro for PIC Compiler<\/a>. For simplicity, I used the built-in <strong>rand()<\/strong> function to generate a pseudo-random number between 1 and 6. The <strong>rand()<\/strong> function in mikroC gives a pseudo-random number between 0 and 32767. It is then scaled to between 1 and 6 through a modulus division by 6 (which gives a remainder output between 0 and 5) and adding 1 to the remainder. I ran<span style=\"font-size: 0.95em; line-height: 1.6em;\">\u00a0the same algorithm on a higher end PIC microcontroller (PIC18F44K22 at 16MHz) and recorded the frequency of the dice output for 100000 rolls just to see how the output distribution looks like. The mean value and standard deviation of the distribution were 16667 and 3207 (19% of mean), respectively. In 100000 rolls, number 3 exhibits the highest frequency, while 4 has the least number of occurrence. The distribution of outcome was not very impressive (that&#8217;s why it is called pseudo-random), but it should work for this basic dice project.\u00a0Here is the output for 100000 rolls.<\/span><\/p>\n<div id=\"attachment_9725\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=9725\" rel=\"attachment wp-att-9725\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-9725\" class=\"size-full wp-image-9725\" alt=\"Frequency of dice outputs for 100000 rolls\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/RandomNess.jpg\" width=\"585\" height=\"463\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/RandomNess.jpg 585w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/RandomNess-550x435.jpg 550w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/a><p id=\"caption-attachment-9725\" class=\"wp-caption-text\">Frequency of dice outputs for 100000 rolls<\/p><\/div>\n<p>External Interrupt is enabled on RA2\/INT pin. When the dice is rolled (shaken in this case), the tilt switch outputs a pulse signal on this pin and generates a hardware interrupt. The PIC microcontroller then displays a chasing effect with the circular pattern of LEDs, always starting from LED 1. The effect is slowed down gradually and finally stopped at the pseudo-number generated using the <strong>rand()<\/strong> function with some additional manipulation. The output LED is programmed to stay on for 2.5 seconds and then the PIC is put to sleep. The PIC wakes up when the dice is rolled again and an interrupt signal is issued on the RA2\/INT pin.<\/p>\n<pre lang=\"C#\">\/\/ Define Charlieplexing lines\r\n#define Row_A RA4_bit\r\n#define Row_B RA5_bit\r\n#define Row_C RA0_bit\r\n\r\n\/\/ Define port direction and output states for each LED\r\nconst unsigned short TRISA_Data[]= \\\r\n               {0xCF, 0xCF, 0xDE, 0xDE, 0xEE, 0xEE};\r\nconst unsigned short PORT_Data[] = \\\r\n               {0x10, 0x20, 0x20, 0x01, 0x10, 0x01};\r\nunsigned int j, i,k, random_num;\r\n\r\nvoid ALL_LEDs_OFF(){\r\n  PORTA = 0x00;\r\n  TRISA = 0xFF;\r\n}\r\n\r\nvoid LED_ON(unsigned short num){\r\n  TRISA = TRISA_Data[num];\r\n  PORTA = PORT_Data[num];\r\n}\r\n\r\n\/\/ Interrupt service routine \r\nvoid interrupt(){ \r\n  if(INTCON.INTF == 1) asm nop;\r\n  INTCON.INTF = 0;\r\n}\r\n\r\nvoid main() {\r\n ANSELA = 0b00000000;  \/\/ Disable analog inputs\r\n TRISA  = 0b00001000;  \/\/ RA3 is input only\r\n PORTA = 0;\r\n OSCCON = 0b01101000;  \/\/ Set internal clock to 4MHz\r\n INTCON = 0b10010000;  \/\/ Enable external hardware interrupt\r\n OPTION_REG.INTEDG = 0; \/\/ External interrupt on falling edge of RA2\/INT pin\r\n Delay_ms(100);\r\n\r\ndo {\r\n  random_num = rand()%6 ;\r\n  for (j=0; j&lt;=random_num+36; j++){\r\n      i = j%6;\r\n      LED_ON(i);\r\n      for (k=0; k&lt;=j\/3; k++){\r\n        Delay_ms(15);\r\n      }\r\n  }\r\n  Delay_ms(2500);\r\n  ALL_LEDs_OFF();\r\n  asm sleep;\r\n  asm nop;\r\n } while(1);\r\n}<\/pre>\n<p><strong><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/11\/LED_Dice.zip\">Download mikroC Project files<\/a><\/strong><\/p>\n<p><span style=\"font-size: 0.95em; line-height: 1.6em;\">Here is a video clip of the dice in action.<\/span><\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/PPTYJnGeFpE\" height=\"315\" width=\"420\" allowfullscreen=\"\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tons of LED dice projects with different output forms have been published online. The most common output configuration in those projects is a 3-1-3 setup (two rows of three LEDs and one LED at in the middle) of seven LEDs, which simulates the actual patterns of dots found on the six faces of a traditional dice. When it is rolled, one or more LEDs are selectively turned on to display a random number between 1 to 6. This project is about a similar LED dice but with a slightly different output form. It uses 6 LEDs which are arranged in<\/p>\n","protected":false},"author":1,"featured_media":9717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1041,1042,363],"class_list":["post-9713","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pic-projects","tag-led-dice","tag-pic-led-projects","tag-pic12f1822"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=9713"}],"version-history":[{"count":20,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9713\/revisions"}],"predecessor-version":[{"id":9744,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/9713\/revisions\/9744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/9717"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=9713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=9713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=9713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}