{"id":404,"date":"2014-07-17T21:12:57","date_gmt":"2014-07-17T21:12:57","guid":{"rendered":"http:\/\/interactingobjects.com\/?p=404"},"modified":"2014-12-02T21:41:53","modified_gmt":"2014-12-02T21:41:53","slug":"sparkfuns-4x4-button-led-matrix-part-3-playing-with-the-pad","status":"publish","type":"post","link":"https:\/\/interactingobjects.com\/?p=404","title":{"rendered":"Sparkfun\u2019s 4\u00d74 Button &#038; LED Matrix \u2013 Part 3 \u2013 Playing with the pad"},"content":{"rendered":"<p>In previous posts, we saw <a title=\"Sparkfun\u2019s 4\u00d74 Button &amp; LED Matrix \u2013 Part 1 \u2013 Soldering\" href=\"http:\/\/interactingobjects.com\/sparkfuns-4x4-button-led-matrix-part-1-soldering\/\">how to assemble the pad<\/a> and <a title=\"Sparkfun\u2019s 4\u00d74 Button &amp; LED Matrix \u2013 Part 2 \u2013 Connecting to an Arduino Mega\" href=\"http:\/\/interactingobjects.com\/sparkfuns-4x4-button-led-matrix-part-2-connecting-to-an-arduino-mega\/\">how to connect it to an arduino MEGA<\/a> and use a very simple program to switch led on and of using the keypad. Now, this is getting a bit more complex as we will :<\/p>\n<ul>\n<li>detect simultaneous key press (up to 10)<\/li>\n<li>use key status (PRESSED, HOLD, RELEASED and IDLE) to do stuff<\/li>\n<li>lights up multiple leds with different colors<\/li>\n<\/ul>\n<h1>What the program will do<\/h1>\n<p>On startup, it will lights up every led in red, green, blue and white successively. It is just a test to verify that the 3 RGB pins of all leds are properly wired to the arduino.<\/p>\n<p>Then, it will lights up all leds at once with random colors.<\/p>\n<p>Then, when using a key :<\/p>\n<ul>\n<li>If the led off\n<ul>\n<li>When key is pressed, led will be switched on with a random color<\/li>\n<li>if the key is not held long enough, the led will be switched off when the key is released<\/li>\n<li>if the key is held long enough, when it is released, led will stay on<\/li>\n<\/ul>\n<\/li>\n<li>If the led is on\n<ul>\n<li>When key is pressed, led color will change<\/li>\n<li>if\u00a0the key is not held long enough, the led will be switched off when the key is released<\/li>\n<li>if the key is held long enough, when it is released, led will stay on with the new color<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>[youtube width=&#8221;640&#8243; height=&#8221;360&#8243;]http:\/\/youtu.be\/4QhD3rGYXbE[\/youtube]<\/p>\n<p>Not really usefull&#8230; but it should show how all this works and get you started the the pad !<\/p>\n<h1>Detecting multiple key press<\/h1>\n<p>No magic here : I simply used <a href=\"http:\/\/playground.arduino.cc\/code\/Keypad\">Keypad library<\/a> from Arduino&#8217;s playground. I used the MultiKey example provided with the library to get my code working.<\/p>\n<h1>How to get all leds switched on with different colors<\/h1>\n<p>4&#215;4\u00a0led matrix (more specifically Sparkfun&#8217;s 4&#215;4 led matrix) requires :<\/p>\n<ul>\n<li>3 x 4 PMW pins that is 3 pins per row of 4 leds (against\u00a03&#215;16=48 PWM pins if each led was to be controlled individually)<\/li>\n<li>4 ground pins (against 16 pins&#8230;)<\/li>\n<\/ul>\n<p>(See <a title=\"Sparkfun\u2019s 4\u00d74 Button &amp; LED Matrix \u2013 Part 2 \u2013 Connecting to an Arduino Mega\" href=\"http:\/\/interactingobjects.com\/sparkfuns-4x4-button-led-matrix-part-2-connecting-to-an-arduino-mega\/\">here <\/a>for some details on RGB leds and have a look <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/PWM\">here<\/a>\u00a0for PWM pins.)<\/p>\n<p>Let say PWM pins are rows and GND pins are columns<\/p>\n<p><a href=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-with-pins.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-409\" src=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-with-pins.png\" alt=\"4x4 button pad sample with pins\" width=\"400\" height=\"395\" srcset=\"https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-with-pins.png 400w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-with-pins-300x296.png 300w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-with-pins-303x300.png 303w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>If you want to make the B2 led red, then you just set D5 to 255 and D32 to LOW.<\/p>\n<p><a href=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-1-red1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-411\" src=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-1-red1.png\" alt=\"4x4 button pad sample 1 red\" width=\"400\" height=\"395\" srcset=\"https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-1-red1.png 400w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-1-red1-300x296.png 300w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-1-red1-303x300.png 303w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>But, although requiring much less pins,\u00a0matrix have\u00a0some drawbacks :\u00a0 what if you want to ligh up B2 and C3 and also B2 to be red and D2 to be green ? All at the same time ?<\/p>\n<p><a href=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-3-red-green-blue.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-412\" src=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-3-red-green-blue.png\" alt=\"4x4 button pad sample 3 red green blue\" width=\"400\" height=\"395\" srcset=\"https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-3-red-green-blue.png 400w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-3-red-green-blue-300x296.png 300w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-3-red-green-blue-303x300.png 303w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>All 4 leds in a row share the very same 3 &#8220;colors&#8221; pins and all leds in a column share the very same ground. It is not directly possible to have 2 differents colors for 2 leds on the same row. And it is not possible to switch on only 2 leds which are not on the same columns.<\/p>\n<p>One\u00a0way is to switch all leds on and off\u00a0successively. Or better (more efficient),<\/p>\n<ol>\n<li>set colors on\u00a0all the rows for the 1st column<\/li>\n<li>switch column on<\/li>\n<li>wait &#8220;leds on&#8221; for a little while (else leds will look quite dimmed)<\/li>\n<li>switch column off<\/li>\n<li>go to the next column (back to 1)<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-413\" src=\"http:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-4-process.png\" alt=\"4x4 button pad sample 4 process\" width=\"1578\" height=\"948\" srcset=\"https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-4-process.png 1578w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-4-process-300x180.png 300w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-4-process-1024x615.png 1024w, https:\/\/interactingobjects.com\/wp-content\/uploads\/2014\/07\/4x4-button-pad-sample-4-process-500x300.png 500w\" sizes=\"auto, (max-width: 1578px) 100vw, 1578px\" \/><\/p>\n<p>I noticed that waiting for\u00a04ms was giving some fickering. I needed 3ms or less to get a &#8220;flicker free&#8221; display.\u00a0 I finally get down to 300\u03bcs &#8220;led on&#8221; period. This is sufficient to get the maximum brightness and definitively get rid of flickering.<\/p>\n<p>Here is a small video showing the same led pattern as above, starting with a 500ms period down to 2ms.<\/p>\n<p>[youtube width=&#8221;640&#8243; height=&#8221;360&#8243;]http:\/\/youtu.be\/E1xLP7nZ4Cw[\/youtube]<\/p>\n<h1>PWM trick<\/h1>\n<p>I did connect the leds a bit differently than in the previous article. That is, I swapped\u00a0 pins<br \/>\n4 &amp;\u00a013 respectively with pin 44 &amp; 45.\u00a0Why didn&#8217;t I used pin 4 &amp; 13 and 44 &amp; 45 instead ?<\/p>\n<p>Because I needed to increase PWM frequency to make the matrix works. To do this, you need to change timers config. Pins 4 and 13 use timer 0.\u00a0But&#8230; as timer 0\u00a0is used by Arduino&#8217;s core timing functions, you don&#8217;t want to mess with it. Do you ?<\/p>\n<p>But why the heck did I want to change PWM frequency ! Because it too &#8220;slow&#8221; by default. On an Arduino MEGA, default frequencies are :<\/p>\n<ul>\n<li>976.56 Hz for pin 4 &amp; 13<\/li>\n<li>490.20 Hz for all other PWM pins<\/li>\n<\/ul>\n<p>As stated above, I won&#8217;t be using pins 4 &amp; 13. So frequency fo pins I use is a bit less that 500 Hz, that is, a period is a bit higher that 2ms. This is veeeeeeeery loooong&#8230; waaaaaay too long because I wait for 300\u03bcs and can only &#8220;wait&#8221; up for 3ms max anyway (not to get flickering). So, even for 3ms, that is around 1 and a half PWM period. This is not enough to get a stable color.<\/p>\n<p>So I increased PWM frequency to the maximum, i.e.\u00a031372.55 Hz. That makes a PWM period of ~32\u03bcs which is much smaller of even the smallest 300\u03bcs &#8220;led on&#8221; period.<\/p>\n<p>More on PWM :<\/p>\n<ul>\n<li>http:\/\/arduino.cc\/en\/Tutorial\/SecretsOfArduinoPWM<\/li>\n<li>http:\/\/playground.arduino.cc\/Main\/TimerPWMCheatsheet<\/li>\n<li>http:\/\/playground.arduino.cc\/Code\/PwmFrequency<\/li>\n<li>http:\/\/letsmakerobots.com\/node\/40136<\/li>\n<li>http:\/\/letsmakerobots.com\/content\/arduino-101-timers-and-interrupts<\/li>\n<\/ul>\n<h1>And now the code<\/h1>\n<p>I finally built up my very first library based on some OO code. It is to be used with an Arduino Mega and the Sparkfun&#8217;s 4&#215;4 button pad.<\/p>\n<p>As this is my first library, it may (does) require improvements&#8230; I know. But I hope it will at least get you started !<\/p>\n<p>All the code is <a href=\"https:\/\/github.com\/jsiobj\/InteractingObjects_ButtonPad\">here<\/a>\u00a0on GitHub.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous posts, we saw how to assemble the pad and how to connect it to an arduino MEGA and use a very simple program to switch led on and of using the keypad. Now, this is getting a bit &hellip; <a href=\"https:\/\/interactingobjects.com\/?p=404\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":[111,86],"tags":[88,94,95,93,97,91,96],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-4x4-button-pad","category-arduino-2","tag-arduino-mega","tag-com-07835","tag-com-08033","tag-led-matrix","tag-pwm","tag-rgb-led","tag-sparkfuns-button-pad"],"_links":{"self":[{"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":16,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/interactingobjects.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/interactingobjects.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}