{"id":949,"date":"2016-08-23T11:38:19","date_gmt":"2016-08-23T10:38:19","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=949"},"modified":"2016-08-23T11:38:19","modified_gmt":"2016-08-23T10:38:19","slug":"lego-laser-harp-part-i","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2016\/lego-laser-harp-part-i","title":{"rendered":"LEGO laser harp &#8211; part I"},"content":{"rendered":"<div class=\"seriesmeta\">This post is part 1 of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-laser-harp\" class=\"series-275\" title=\"LEGO Laser Harp\">LEGO Laser Harp<\/a><\/div><p>This is an idea I&#8217;ve been postponing for several months but the time has finally come: an laser harp.<\/p>\n<p>After tinkering with lasers, fog, sound, color sensors and python I found myself wondering how to give a proper use to all that. Then I remembered <a href=\"https:\/\/en.wikipedia.org\/wiki\/Jean-Michel_Jarre\">Jean-Michel Jarre<\/a> and how his laser harp made such a big impression on me at late 80&#8217;s when I finally decided &#8220;hey, i wanna study Electronics!&#8221;<\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Jean-Michel_Jarre\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/9\/9a\/Jarre_helsinki_2009.jpg\/170px-Jarre_helsinki_2009.jpg\" \/><\/a>For a first version, let&#8217;s call it &#8220;a proof of concept&#8221;, I just want a simple 7-string harp that can play the basic 7 notes. Polyphony would be great but I doubt that the EV3 sound capabilities allow that (and I cannot afford the brute force solution of using 7 EV3 so that each one plays only a single note).<\/p>\n<p>So in the last months I&#8217;ve been buying EV3 color sensors and I finally have 7. Since the EV3 only has 4 input ports I need some kind of sensor multiplexer but thanks to <a href=\"http:\/\/www.mindsensors.com\/\">mindsensors.com<\/a> I already have one <a href=\"http:\/\/www.mindsensors.com\/ev3-and-nxt\/23-ev3-sensor-multiplexer-for-ev3-or-nxt\">EV3SensorMux<\/a> (and a second one is on the way, from an <a href=\"http:\/\/www.generationrobots.com\/en\/\">european distributor<\/a> &#8211; portuguese customs DO stink!)<\/p>\n<p>With 2 MUX it&#8217;s possible to connect up to 8 sensors to the EV3. Since I just want 7 &#8220;strings&#8221; I am considering using an 8th sensor to control the amplitude of the notes. I&#8217;ll try an ultrasonic sensor but I&#8217;m not sure if it has enough &#8220;wideness&#8221; to cover the whole harp, let&#8217;s see.<\/p>\n<p>So of course I&#8217;ll be using <a href=\"http:\/\/www.ev3dev.org\/\">ev3dev<\/a> and python.<\/p>\n<p>Using the EV3SensorMux is easy: just plug it to an input port and ev3dev immediately recognizes it:<\/p>\n<pre>lego-port port8: Registered 'in1:i2c80:mux1' on '3-0050'.\r\nlego-port port8: Added new device 'in1:i2c80:mux1:lego-ev3-color'\r\nlego-sensor sensor0: Registered 'ms-ev3-smux' on 'in1:i2c80'.\r\nlego-port port9: Registered 'in1:i2c81:mux2' on '3-0051'.\r\nlego-port port9: Added new device 'in1:i2c81:mux2:lego-ev3-color'\r\nlego-sensor sensor1: Registered 'ms-ev3-smux' on 'in1:i2c81'.\r\nlego-port port10: Registered 'in1:i2c82:mux3' on '3-0052'.\r\nlego-port port10: Added new device 'in1:i2c82:mux3:lego-ev3-color'\r\nlego-sensor sensor2: Registered 'ms-ev3-smux' on 'in1:i2c82'.\r\nlego-sensor sensor3: Registered 'lego-ev3-color' on 'in1:i2c80:mux1'.\r\nlego-sensor sensor4: Registered 'lego-ev3-color' on 'in1:i2c81:mux2'.\r\nlego-sensor sensor5: Registered 'lego-ev3-color' on 'in1:i2c82:mux3'.<\/pre>\n<p>Even better: by default all 3 mux ports are configured for the EV3 color sensor, just as I wanted!<\/p>\n<p>NOTE: as of today (kernel version &#8216;4.4.17-14-ev3dev-ev3&#8217;) my EV3 autodetection only works when booting with a non-default configuration:<\/p>\n<pre>sudo nano \/etc\/default\/flash-kernel\r\n\r\n LINUX_KERNEL_CMDLINE=\"console=ttyS1,115200\"\r\n\r\nsudo flash-kernel\r\nsudo reboot<\/pre>\n<p>this was suggested to me by David Lechner in <a href=\"https:\/\/github.com\/ev3dev\/ev3dev\/issues\/719\">another issue<\/a>, hope will be fixed soon.<\/p>\n<p>To use the color sensors in python I just need to know their ports. With the MUX in port &#8216;in1&#8217; and 6 color sensors connected, these are the ports to use:<\/p>\n<pre>in1:i2c80:mux1\r\nin1:i2c80:mux2\r\nin1:i2c80:mux3\r\nin2\r\nin3\r\nin4<\/pre>\n<p>And to play a note in python I just need to <a href=\"http:\/\/www.phy.mtu.edu\/~suits\/notefreqs.html\">know it&#8217;s frequency<\/a> to use with Sound.tone() function, so:<\/p>\n<pre>C3 = [(130.81, TONE_LENGHT)] \r\nD3 = [(146.83, TONE_LENGHT)] \r\nE3 = [(164.81, TONE_LENGHT)] \r\nF3 = [(174.61, TONE_LENGHT)] \r\nG3 = [(196.00, TONE_LENGHT)] \r\nA3 = [(220.00, TONE_LENGHT)] \r\nB3 = [(246.94, TONE_LENGHT)]<\/pre>\n<p>And so this was the first script for my harp:<\/p>\n<pre>#!\/usr\/bin\/env python\r\n\r\nfrom ev3dev.auto import *\r\n\r\nTONE_LENGHT = 150\r\n\r\nC4 = [(261.64, TONE_LENGHT)]\u00a0\u00a0 #Do4\r\nD4 = [(293.66, TONE_LENGHT)]\u00a0\u00a0 #Re4\r\nE4 = [(329.63, TONE_LENGHT)]\u00a0\u00a0 #Mi4\r\nF4 = [(349.23, TONE_LENGHT)]\u00a0\u00a0 #Fa4\r\nG4 = [(392.00, TONE_LENGHT)]\u00a0\u00a0 #Sol4\r\nA4 = [(440.00, TONE_LENGHT)]\u00a0\u00a0 #La4\r\nB4 = [(493.88, TONE_LENGHT)]\u00a0\u00a0 #Si4\r\n\r\nAMB_THRESHOLD = 9\r\n\r\nsensor1 = ColorSensor('in1:i2c80:mux1')\r\nsensor1.mode = 'COL-AMBIENT'\r\nsensor2 = ColorSensor('in1:i2c81:mux2')\r\nsensor2.mode = 'COL-AMBIENT'\r\nsensor3 = ColorSensor('in1:i2c82:mux3')\r\nsensor3.mode = 'COL-AMBIENT'\r\nsensor4 = ColorSensor('in2')\r\nsensor4.mode = 'COL-AMBIENT'\r\nsensor5 = ColorSensor('in3')\r\nsensor5.mode = 'COL-AMBIENT'\r\nsensor6 = ColorSensor('in4')\r\nsensor6.mode = 'COL-AMBIENT'\r\n\r\n# there is no sensor7 yet, I need another MUX\r\n\r\ns1 = 0\r\ns2 = 0\r\ns3 = 0\r\ns4 = 0\r\ns5 = 0\r\ns6 = 0\r\ns7 = 0\r\n\r\nwhile True:\r\n\u00a0\u00a0\u00a0 s1 = sensor1.value(0)\r\n\u00a0\u00a0\u00a0 s2 = sensor2.value(0)\r\n\u00a0\u00a0\u00a0 s3 = sensor3.value(0)\r\n\u00a0\u00a0\u00a0 s4 = sensor4.value(0)\r\n\u00a0\u00a0\u00a0 s5 = sensor5.value(0)\r\n\u00a0\u00a0\u00a0 s6 = sensor6.value(0)\r\n#\u00a0\u00a0\u00a0 s7 = sensor7.value(0)\r\n\u00a0 \r\n\u00a0\u00a0\u00a0 if s1 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(C4).wait()\r\n\u00a0\u00a0\u00a0 if s2 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(D4).wait()\r\n\u00a0\u00a0\u00a0 if s3 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(E4).wait()\r\n\u00a0\u00a0\u00a0 if s4 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(F4).wait()\r\n\u00a0\u00a0\u00a0 if s5 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(G4).wait()\r\n\u00a0\u00a0\u00a0 if s6 &lt; AMB_THRESHOLD:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(A4).wait()\r\n#\u00a0\u00a0\u00a0 if s7 &lt; AMB_THRESHOLD:\r\n#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Sound.tone(B4).wait()<\/pre>\n<p>So whenever the light level over one of the color sensor drops bellow AMB_THRESHOLD a note will play for TONE_LENGHT milliseconds.<\/p>\n<p>Unfortunately the sound is monophonic (just one note can be played at a time) and it doesn&#8217;t sound like an harp at all &#8211; it sounds more like my BASIC games on the ZX Spectrum in the 80&#8217;s.<\/p>\n<p>So I tried Sound.play(Wave File) instead. Found some harp samples, converted them to .wav files at 44100 Hz and it really sounds much better&#8230; but the length of the samples I found is to big so the &#8220;artist&#8221; have to wait for the note to stop playing before moving the hand to another &#8220;string&#8221;. Not good and also not polyphonic.<\/p>\n<p>Next post I&#8217;ll show a better approach for both quality and polyphony: MIDI.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"seriesmeta\">This post is part 1  of 2 of \u00a0<a href=\"https:\/\/ofalcao.pt\/blog\/series\/lego-laser-harp\" class=\"series-275\" title=\"LEGO Laser Harp\">LEGO Laser Harp<\/a><\/div><p>This is an idea I&#8217;ve been postponing for several months but the time has finally come: an laser harp. After tinkering with lasers, fog, sound, color sensors and python I found myself wondering how to give a proper use to all that. Then I remembered Jean-Michel Jarre and how his laser harp made such a &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2016\/lego-laser-harp-part-i\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;LEGO laser harp &#8211; part I&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,157,154,18,158,19,20,148,98],"tags":[277,276],"series":[275],"class_list":["post-949","post","type-post","status-publish","format-standard","hentry","category-ev3dev","category-ev3dev-en","category-lego-en","category-lego","category-lego-mindstorms-en","category-lego-mindstorms","category-linux","category-linux-en","category-python","tag-laser-harp","tag-music","series-lego-laser-harp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-fj","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=949"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/949\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=949"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}