{"id":815,"date":"2018-10-06T23:01:37","date_gmt":"2018-10-07T03:01:37","guid":{"rendered":"http:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/?p=815"},"modified":"2018-10-09T20:47:19","modified_gmt":"2018-10-10T00:47:19","slug":"polish-present-submit-assignment-william-schwartz","status":"publish","type":"post","link":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wrschwartz\/polish-present-submit-assignment-william-schwartz\/","title":{"rendered":"Polish &amp; Present Submit Assignment: William Schwartz"},"content":{"rendered":"<p>for this week I did not have a ton to do. I did all the assembly last week, so this week all I had to do was program the teensy, and do the audio analysis.<\/p>\n<p>using the teensy audio library made things pretty easy, I am still doing some minor adjustments, but for the most part the project is completed there is still some averaging and little tweaks to make but I am very pleased with the project<\/p>\n<p><a href=\"http:\/\/www.youtube.com\/watch?v=JU8OloREU7E&amp;feature=youtu.be\">https:\/\/youtu.be\/JU8OloREU7E<\/a><\/p>\n<p><a href=\"https:\/\/youtu.be\/N2PDXgPbCHg\">https:\/\/youtu.be\/N2PDXgPbCHg<\/a><\/p>\n<p>these are two videos of the head in operation \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>here is my current Arduino code:<\/p>\n<pre>#include &lt;Adafruit_NeoPixel.h&gt;\r\n#include &lt;Audio.h&gt;\r\n#include &lt;Wire.h&gt;\r\n#include &lt;SerialFlash.h&gt;\r\n#ifdef __AVR__\r\n#include &lt;avr\/power.h&gt;\r\n#endif\r\n\r\nvoid setLedColorHSV(byte h, byte s, byte v);\r\n\r\n\/\/ Create the Audio components. These should be created in the\r\n\/\/ order data flows, inputs\/sources -&gt; processing -&gt; outputs\r\n\/\/\r\nAudioInputAnalog adc1(A0); \/\/ ADC0 input\r\nAudioAnalyzeFFT1024 myFFT;\r\n\r\n\/\/ Connect either the live input or synthesized sine wave\r\nAudioConnection patchCord1(adc1, 0, myFFT, 0);\r\n\r\n\/\/ Which pin on the Arduino is connected to the NeoPixels?\r\n\/\/ On a Trinket or Gemma we suggest changing this to 1\r\n#define PIN 5\r\nconst int POWER_LED_PIN = 13; \/\/ Output pin for power LED (pin 13 to use Teensy 3.0's onboard LED).\r\nbyte h;\r\nbyte s;\r\nbyte v;\r\nbyte RedLight;\r\nbyte GreenLight;\r\nbyte BlueLight;\r\nint currentlight;\r\n\r\nfloat avg_fft[512]; \r\nint avg_fft_cout = 1;\r\n\r\n#define NUMPIXELS 41\r\n#define NUMCELLS 25\r\nint cells[NUMCELLS] = {3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1, 1, 3, 4, 3, 2};\r\n\r\n\/\/ ----------- ALL THE DIFFERENT MAPPINGS THAT I HAVE MADE!!! ---------------------------------------\r\n\/\/ even maping base at the neck\r\nint mapping[NUMCELLS] = {0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0};\r\n\/\/ random mapping\r\n\/\/int mapping[NUMCELLS] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0};\r\n\/\/ random mapping base at the left ear\r\n\/\/int mapping[NUMCELLS] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 5, 5, 5};\r\n\/\/ random mapping base in the neck\r\n\/\/int mapping[NUMCELLS] = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 0, 5, 5, 5, 5, 1, 2, 3};\r\n\r\n\/\/ --------------------------------------------------\r\n\r\n\r\n#define freq_bands 6\r\n\r\n\/\/ defines the frequency ranges for each band \r\nint freq_high[freq_bands] = {90, 40, 30, 20, 12, 6};\r\nint freq_low[freq_bands] = {40, 31, 21, 13, 6, 1};\r\n\r\n\/\/ stores the results of going through each band\r\nint freq_max_bin[freq_bands] = {};\r\nfloat freq_max_amp[freq_bands] = {};\r\nfloat prev_freq_max_amp[freq_bands] = {};\r\nbyte freq_hue[freq_bands] = {};\r\nbyte prev_freq_hue[freq_bands] = {};\r\n\r\nint top_freq = 0;\r\n\r\n\/\/ When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.\r\n\/\/ Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest\r\n\/\/ example for more information on possible values.\r\nAdafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);\r\n\r\nvoid setup() {\r\n\/\/ Turn on the power indicator LED.\r\npinMode(POWER_LED_PIN, OUTPUT);\r\ndigitalWrite(POWER_LED_PIN, HIGH);\r\nrandomSeed(10);\r\n\r\npixels.begin(); \/\/ This initializes the NeoPixel library.\r\n\/\/ Audio connections require memory to work. For more\r\n\/\/ detailed information, see the MemoryAndCpuUsage example\r\nAudioMemory(12);\r\n\r\n\/\/ Configure the window algorithm to use\r\nmyFFT.windowFunction(AudioWindowHanning1024);\r\n\/\/myFFT.windowFunction(NULL);\r\n}\r\n\r\nvoid loop() {\r\nfloat n;\r\nint i;\r\nint fft_count = 0;\r\n\r\ndelay(10);\r\n\r\n\/\/ ________________________________________________________ \r\n\/\/ copy initial sample\r\nif (myFFT.available()) {\r\nfor (i = 0; i &lt; 512; i++) {\r\navg_fft[i] = myFFT.read(i);\r\n}\r\n\/\/ averge other samples\r\nwhile (fft_count &lt; avg_fft_cout) {\r\nif (myFFT.available()) {\r\nfor (i = 0; i &lt; 512; i++) {\r\navg_fft[i] = (avg_fft[i] + myFFT.read(i))\/2.0;\r\n}\r\nfft_count++;\r\n}\r\n}\r\n}\r\n\/\/ ________________________________________________________\r\n\r\nint f; \/\/ used as intex for band we are in\r\nint bin = 0; \/\/ hold on to the current bin\r\n\r\nfor (f = 0; f &lt; freq_bands; f++) {\r\n\r\nfreq_max_amp[f] = 0;\r\nbin = 0;\r\n\r\nfor (i=freq_high[f]; i &gt; freq_low[f]; i--) {\r\nn = avg_fft[i];\r\nif ( n &gt; 0.01) {\r\nfreq_max_amp[f] = max(freq_max_amp[f], n);\r\n\/\/ recently added might make things worse? we will see...\r\n\/\/if (freq_max_amp[f] == n) {\r\nbin = i;\r\n\/\/}\r\n}\r\n}\r\n\r\nif (bin != 0 &amp;&amp; bin != freq_max_bin[f]){\r\nfreq_max_bin[f] = bin;\r\n\/\/ TODO: the bins are off!! bin 5 should be RED!! currently bin 0 is red\r\nfreq_hue[f] = 252 - ((f+1%6)*42 + random((freq_max_bin[f]-freq_low[f])*(42\/(freq_high[f]-freq_low[f]))));\r\n\r\n}\r\n\r\nif (freq_max_amp[f] == 0){\r\nfreq_max_bin[f] = 0;\r\n}\r\n\r\n\/\/ Serial.println();\r\n\/\/ Serial.print((255\/f));\r\n\/\/ Serial.println();\r\n\/\/ freq_hue[f] = (255\/f);\r\n\r\n\/\/ set the hue to the bin hue\r\nif (freq_max_bin[f] != 0){\r\nSerial.println();\r\nSerial.print(\"BAND \");\r\nSerial.print(f);\r\nSerial.print(\" amplitude is \");\r\nSerial.print(freq_max_amp[f], 20);\r\nSerial.print(\"the bin is \");\r\nSerial.print(freq_max_bin[f]);\r\n}\r\n}\r\n\r\n\/\/ PUSH ALL THE DATA TO THE LIGHTS!!!\r\n\/\/ ________________________________________________________ \r\ncurrentlight = 0;\r\nfor(int i=0;i&lt;NUMCELLS;i++) {\r\n\r\nh = freq_hue[mapping[i]];\r\nif (h != 0 &amp;&amp; prev_freq_hue[mapping[i]] != 0) {\r\nh = (h - prev_freq_hue[mapping[i]])\/3 + prev_freq_hue[mapping[i]];\r\n}\r\nprev_freq_hue[mapping[i]] = h;\r\n\r\ns = 255;\r\nif (freq_max_amp[mapping[i]] != 0.0){\r\n\/\/ v = (int)(freq_max_amp[mapping[i]]*1000.0);\r\nv = (int)(freq_max_amp[mapping[i]]*1000.0);\r\nv = (int) abs(v - prev_freq_max_amp[mapping[i]])\/3 + prev_freq_max_amp[mapping[i]];\r\ntop_freq = max(top_freq, v);\r\n\/\/ Serial.println();\r\n\/\/ Serial.print(top_freq);\r\n\/\/ Serial.println();\r\nprev_freq_max_amp[mapping[i]] = v\/1.1-1;\r\n} else {\r\n\/\/v = ((int)(prev_freq_max_amp[mapping[i]]*1000.0))\/2-1;\r\nv = prev_freq_max_amp[mapping[i]];\r\nprev_freq_max_amp[mapping[i]] = (int) prev_freq_max_amp[mapping[i]]\/1.2;\r\nif (v != 0) {\r\nSerial.println();\r\nSerial.print(prev_freq_max_amp[mapping[i]]);\r\nSerial.println();\r\n}\r\n\r\n}\r\nsetLedColorHSV(h,s,v);\r\nfor (int j=0;j&lt;cells[i];j++) {\r\n\/\/ pixels.Color takes RGB values, from 0,0,0 up to 255,255,255\r\npixels.setPixelColor(currentlight, pixels.Color(RedLight, GreenLight, BlueLight));\r\ncurrentlight = currentlight + 1;\r\n}\r\n}\r\npixels.show(); \/\/ This sends the updated pixel color to the hardware.\r\n\/\/ ________________________________________________________\r\n\r\n\r\n}\r\n\r\nvoid setLedColorHSV(byte h, byte s, byte v) {\r\n\/\/ this is the algorithm to convert from RGB to HSV\r\nh = (h * 192) \/ 256; \/\/ 0..191\r\nunsigned int i = h \/ 32; \/\/ We want a value of 0 thru 5\r\nunsigned int f = (h % 32) * 8; \/\/ 'fractional' part of 'i' 0..248 in jumps\r\n\r\nunsigned int sInv = 255 - s; \/\/ 0 -&gt; 0xff, 0xff -&gt; 0\r\nunsigned int fInv = 255 - f; \/\/ 0 -&gt; 0xff, 0xff -&gt; 0\r\nbyte pv = v * sInv \/ 256; \/\/ pv will be in range 0 - 255\r\nbyte qv = v * (256 - s * f \/ 256) \/ 256;\r\nbyte tv = v * (256 - s * fInv \/ 256) \/ 256;\r\n\r\nswitch (i) {\r\ncase 0:\r\nRedLight = v;\r\nGreenLight = tv;\r\nBlueLight = pv;\r\nbreak;\r\ncase 1:\r\nRedLight = qv;\r\nGreenLight = v;\r\nBlueLight = pv;\r\nbreak;\r\ncase 2:\r\nRedLight = pv;\r\nGreenLight = v;\r\nBlueLight = tv;\r\nbreak;\r\ncase 3:\r\nRedLight = pv;\r\nGreenLight = qv;\r\nBlueLight = v;\r\nbreak;\r\ncase 4:\r\nRedLight = tv;\r\nGreenLight = pv;\r\nBlueLight = v;\r\nbreak;\r\ncase 5:\r\nRedLight = v;\r\nGreenLight = pv;\r\nBlueLight = qv;\r\nbreak;\r\n}\r\n}\r\n\r\n\r\n\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>for this week I did not have a ton to do. I did all the assembly last week, so this week all I had to do was program the teensy, and do the audio analysis. using the teensy audio library made things pretty easy, I am still doing some minor adjustments, but for the most&#8230; <\/p>\n<div class=\"link-more\"><a href=\"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wrschwartz\/polish-present-submit-assignment-william-schwartz\/\">Read More<\/a><\/div>\n","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":2,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":817,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/posts\/815\/revisions\/817"}],"wp:attachment":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a18\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}