{"id":700,"date":"2020-10-09T20:51:46","date_gmt":"2020-10-10T00:51:46","guid":{"rendered":"http:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/?p=700"},"modified":"2020-10-09T22:31:32","modified_gmt":"2020-10-10T02:31:32","slug":"polish-present-4","status":"publish","type":"post","link":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/cctulig\/polish-present-4\/","title":{"rendered":"Polish &amp; Present"},"content":{"rendered":"\n<p>Last week I dumped a lot of time into finishing the majority of the project for last class as I knew this week (and next) I&#8217;d be swamped with work for other classes. So luckily, I didn&#8217;t have much work to do to finish up this week. All I really needed to do was some re-programming of the visualizer to mesh nicely with the infinity icosahedron shape. Since I now no longer have 9 pixel bands per edge and instead have a continuous loops in the top and bottom of the icosahedron, I could expand the amount of pixels per frequency channel up to 18 pixels. This gave me the resolution to do a cooler volume visualizer by expanding from either side of a center point instead of expanding left to right. Other major changes I made to the visualization was changing the color of all the pixels based on which frequency is currently the loudest, as well as scaling the brightness of each band depending on how loud its corresponding frequency is. Finally, I setup the infinity icosahedron so that no wires were showing and surrounded it with a white background to help amplify the effect the dancing lights have outside the icosahedron.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube aligncenter wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"jetpack-video-wrapper\"><iframe loading=\"lazy\" title=\"Light Art Infinity Icosahedron\" width=\"1000\" height=\"563\" src=\"https:\/\/www.youtube.com\/embed\/z5WyJaECQco?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div><figcaption>Song is Flight by Tristam &amp; Braken<\/figcaption><\/figure>\n\n\n\n<p>And in case you&#8217;re curious on the source code for this project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h&gt;\n#include &lt;FastLED.h&gt;\n#include &lt;math.h&gt;\n\n#define LED_PIN 7\n#define NUM_LEDS 117\n#define SPECTRUM_LENGTH 6\n\nint RED&#091;3] = {255, 0, 0};\nint GREEN&#091;3] = {0, 255, 0};\nint BLUE&#091;3] = {0, 0, 255};\nint ORANGE&#091;3] = {255, 100, 0};\nint PURPLE&#091;3] = {100, 0, 255};\nint YELLOW&#091;3] = {255, 255, 0};\n\nCRGB leds&#091;NUM_LEDS];\n\nbyte first = 1;\nint band&#091;18];\nint scale = 1;\nint color&#091;3] = {RED&#091;0], RED&#091;1], RED&#091;2]};\nint rnd&#091;18] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};\n\n\nvoid set_band (int b0, int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8, \n              int b9, int b10, int b11, int b12, int b13, int b14, int b15, int b16, int b17) {\n  band&#091;0] = b0;\n  band&#091;1] = b1;\n  band&#091;2] = b2;\n  band&#091;3] = b3;\n  band&#091;4] = b4;\n  band&#091;5] = b5;\n  band&#091;6] = b6;\n  band&#091;7] = b7;\n  band&#091;8] = b8;\n  band&#091;9] = b9;\n  band&#091;10] = b10;\n  band&#091;11] = b11;\n  band&#091;12] = b12;\n  band&#091;13] = b13;\n  band&#091;14] = b14;\n  band&#091;15] = b15;\n  band&#091;16] = b16;\n  band&#091;17] = b17;\n}\n\nvoid setupSpectrum() {\n  \/\/Setup pins to drive the spectrum analyzer. It needs RESET and STROBE pins.\n  pinMode(5, OUTPUT);\n  pinMode(4, OUTPUT);\n\n  \/\/Init spectrum analyzer\n  digitalWrite(4,LOW);  \/\/pin 4 is strobe on shield\n  digitalWrite(5,HIGH); \/\/pin 5 is RESET on the shield\n  digitalWrite(4,HIGH);\n  digitalWrite(4,LOW);\n  digitalWrite(5,LOW);\n}\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(\"Hello World!\");\n\n  setupSpectrum();\n\n  FastLED.addLeds&lt;WS2812, LED_PIN, GRB&gt;(leds, NUM_LEDS);\n}\n\nint getLoudest(int spectrum&#091;SPECTRUM_LENGTH]) {\n  byte loudestBand = 0;\n  int  loudestValue = -1;\n\n  for(byte band = 0; band &lt; SPECTRUM_LENGTH; band++) {\n    if (spectrum&#091;band] &gt; loudestValue) {\n      loudestBand = band;\n      loudestValue = spectrum&#091;band];\n    }\n  }\n  return loudestBand;\n}\n\nvoid assignColor(int c&#091;3]) {\n  color&#091;0] = c&#091;0];\n  color&#091;1] = c&#091;1];\n  color&#091;2] = c&#091;2];\n}\n\nvoid visualizeLoudest(int loudest) {\n  FastLED.clear();\n\n  switch (loudest)\n  {\n  case 0:\n    assignColor(ORANGE);\n    break;\n  case 1:\n    assignColor(BLUE);\n    break;\n  case 2:\n    assignColor(RED);\n    break;\n  case 3:\n    assignColor(GREEN);\n    break;\n  case 4:\n    assignColor(PURPLE);\n    break;\n  case 5:\n    assignColor(YELLOW);\n    break;\n  case 6:\n    assignColor(YELLOW);\n    break;\n  }\n}\n\nint r() {\n  return ((int)random(0, 2)) * 2;\n}\n\nvoid visualizeBand(float value, float max_val, float min_val) {\n\n  int diff = round((value - min_val) \/ (max_val - min_val) * 10);\n  if (diff &gt; 10) diff = 10;\n\n  scale = 13 - diff;\n\n  switch (diff)\n  {\n  case 0:\n    set_band(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);\n    break;\n  case 1:\n    set_band(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);\n    break;\n  case 2:\n    set_band(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0);\n    break;\n  case 3:\n    set_band(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0);\n    break;\n  case 4:\n    set_band(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0);\n    break;\n  case 5:\n    set_band(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0);\n    break;\n  case 6:\n    set_band(0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);\n    break;\n  case 7:\n    set_band(0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0);\n    break;\n  case 8:\n    set_band(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);\n    break;\n  case 9:\n    set_band(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);\n    break;\n  case 10:\n    if(millis() % 3 == 0) {\n      rnd&#091;0] = r(); rnd&#091;1] = r(); rnd&#091;2] = r(); rnd&#091;3] = r(); rnd&#091;4] = r(); rnd&#091;5] = r(); rnd&#091;6] = r(); rnd&#091;7] = r(); rnd&#091;8] = r(); rnd&#091;9] = r(); rnd&#091;10] = r(); rnd&#091;11] = r(); rnd&#091;12] = r(); rnd&#091;13] = r(); rnd&#091;14] = r(); rnd&#091;15] = r(); rnd&#091;16] = r(); rnd&#091;17] = r();\n    }\n    set_band(rnd&#091;0], rnd&#091;1], rnd&#091;2], rnd&#091;3], rnd&#091;4], rnd&#091;5], rnd&#091;6], rnd&#091;7], rnd&#091;8], rnd&#091;9], rnd&#091;10], rnd&#091;11], rnd&#091;12], rnd&#091;13], rnd&#091;14], rnd&#091;15], rnd&#091;16], rnd&#091;17]);\n    break;\n  default:\n    set_band(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);\n    break;\n  }\n  \n}\n\nvoid lightLED(int pos, int mode) {\n  switch (mode)\n  {\n  case 0:\n    leds&#091;pos] = CRGB(0, 0, 0);\n    break;\n  case 1:\n    leds&#091;pos] = CRGB(color&#091;0] \/ scale, color&#091;1] \/ scale, color&#091;2] \/ scale);\n    break;\n  case 2:\n    leds&#091;pos] = CRGB(color&#091;0], color&#091;1], color&#091;2]);\n    break;\n  default:\n    leds&#091;pos] = CRGB(0, 0, 0);\n    break;\n  }\n}\n\nvoid visualizeSpectrum(int spectrum&#091;SPECTRUM_LENGTH], int start) {\n  for (int i = 0; i &lt; SPECTRUM_LENGTH; i++) {\n    visualizeBand((float)spectrum&#091;i], (float)1000, (float)50);\n    for (int pos = 0; pos &lt; 18; pos ++) {\n      lightLED(start + pos + i * 18, band&#091;pos]);\n    }\n  }\n}\n\nvoid printSpectrum(int spectrum&#091;SPECTRUM_LENGTH]) {\n  for (int i = 0; i &lt; SPECTRUM_LENGTH; i++) {\n    Serial.print(spectrum&#091;i]);\n    Serial.print(\"\\t\");\n  }\n  Serial.println(\"\");\n}\n\n\/\/ Function to read 7 band equalizers \nvoid dance() {\n  \/\/ Spectrum analyzer read values will be kept here.\n  int middle&#091;7]; \/\/ Band 0 = Lowest Frequencies. \n  int right&#091;7];\n  int left&#091;7];\n\n  \/\/ Get the reading from each of 7 channels each (left and right)\n  byte Band;\n  for (Band=0;Band &lt;7; Band++) {\n    \/\/ left\n    left&#091;Band] = analogRead(0);\n\n    \/\/ right\n    right&#091;Band] = analogRead(1);\n\n    \/\/ middle\n    middle&#091;Band] = (left&#091;Band] + right&#091;Band]) \/ 2;\n\n    digitalWrite(4,HIGH);  \/\/Strobe pin on the shield\n    delay(2);\n    digitalWrite(4,LOW);\n  }\n\n  visualizeLoudest(getLoudest(middle));\n\n  visualizeSpectrum(middle, 0);\n\n  \/\/ printSpectrum(middle);\n\n  FastLED.show();\n}\n\nvoid loop() {\n  dance();\n  delay(25);\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week I dumped a lot of time into finishing the majority of the project for last class as I knew this week (and next) I&#8217;d be swamped with work for other classes. So luckily, I didn&#8217;t have much work to do to finish up this week. All I really needed to do was some&#8230; <\/p>\n<div class=\"link-more\"><a href=\"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/cctulig\/polish-present-4\/\">Read More<\/a><\/div>\n","protected":false},"author":165,"featured_media":660,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"cybocfi_hide_featured_image":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-700","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_featured_media_url":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-content\/uploads\/sites\/13\/2020\/10\/image-3.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/posts\/700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/users\/165"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/comments?post=700"}],"version-history":[{"count":5,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/posts\/700\/revisions"}],"predecessor-version":[{"id":709,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/posts\/700\/revisions\/709"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/media\/660"}],"wp:attachment":[{"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/media?parent=700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/categories?post=700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshuarosenstock.com\/teaching\/lightart-a20\/wp-json\/wp\/v2\/tags?post=700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}