{"id":5811,"date":"2010-09-12T22:01:15","date_gmt":"2010-09-13T02:01:15","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=5811"},"modified":"2013-10-23T14:57:11","modified_gmt":"2013-10-23T18:57:11","slug":"hsv-colour-cycling-led-on-arduino","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2010\/09\/12\/hsv-colour-cycling-led-on-arduino\/","title":{"rendered":"HSV colour cycling LED on Arduino"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"HSV colour cycling LED on Arduino\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/77zn_wO1osM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Pretty much everyone tries the RGB colour cycler when they get their first Arduino. This variant cycles through the HSV colour wheel, though at fixed saturations and values.<\/p>\n<p>Code:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/\/ HSV fade\/bounce for Arduino - scruss.com - 2010\/09\/12\r\n\/\/ Note that there's some legacy code left in here which seems to do nothing\r\n\/\/ but should do no harm ...\r\n\r\n\/\/ don't futz with these, illicit sums later\r\n#define RED       9 \/\/ pin for red LED\r\n#define GREEN    10 \/\/ pin for green - never explicitly referenced\r\n#define BLUE     11 \/\/ pin for blue - never explicitly referenced\r\n#define SIZE    255\r\n#define DELAY    10\r\n#define HUE_MAX  6.0\r\n#define HUE_DELTA 0.01\r\n\r\nlong deltas&#x5B;3] = {\r\n  5, 6, 7 };\r\nlong rgb&#x5B;3];\r\nlong rgbval;\r\n\/\/ for reasons unknown, if value !=0, the LED doesn't light. Hmm ...\r\n\/\/ and saturation seems to be inverted\r\nfloat hue=0.0, saturation=1.0, value=1.0;\r\n\r\n\/*\r\nchosen LED SparkFun sku: COM-09264\r\n has Max Luminosity (RGB): (2800, 6500, 1200)mcd\r\n so we normalize them all to 1200 mcd -\r\n R  1200\/2800  =  0.428571428571429   =   109\/256\r\n G  1200\/6500  =  0.184615384615385   =    47\/256\r\n B  1200\/1200  =  1.0                 =   256\/256\r\n *\/\r\nlong bright&#x5B;3] = {\r\n  109, 47, 256};\r\n\r\nlong k, temp_value;\r\n\r\nvoid setup () {\r\n  randomSeed(analogRead(4));\r\n  for (k=0; k&lt;3; k++) {\r\n    pinMode(RED + k, OUTPUT);\r\n    rgb&#x5B;k]=0;\r\n    analogWrite(RED + k, rgb&#x5B;k] * bright&#x5B;k]\/256);\r\n    if (random(100) &gt; 50) {\r\n      deltas&#x5B;k] = -1 * deltas&#x5B;k]; \/\/ randomize direction\r\n    }\r\n  }\r\n}\r\n\r\nvoid loop() {\r\n  hue += HUE_DELTA;\r\n  if (hue &gt; HUE_MAX) {\r\n    hue=0.0;\r\n  }\r\n  rgbval=HSV_to_RGB(hue, saturation, value);\r\n  rgb&#x5B;0] = (rgbval &amp; 0x00FF0000) &gt;&gt; 16; \/\/ there must be better ways\r\n  rgb&#x5B;1] = (rgbval &amp; 0x0000FF00) &gt;&gt; 8;\r\n  rgb&#x5B;2] = rgbval &amp; 0x000000FF;\r\n  for (k=0; k&lt;3; k++) { \/\/ for all three colours\r\n    analogWrite(RED + k, rgb&#x5B;k] * bright&#x5B;k]\/256);\r\n  }\r\n  delay(DELAY);\r\n}\r\n\r\nlong HSV_to_RGB( float h, float s, float v ) {\r\n  \/* modified from Alvy Ray Smith's site: http:\/\/www.alvyray.com\/Papers\/hsv2rgb.htm *\/\r\n  \/\/ H is given on &#x5B;0, 6]. S and V are given on &#x5B;0, 1].\r\n  \/\/ RGB is returned as a 24-bit long #rrggbb\r\n  int i;\r\n  float m, n, f;\r\n\r\n  \/\/ not very elegant way of dealing with out of range: return black\r\n  if ((s&lt;0.0) || (s&gt;1.0) || (v&lt;0.0) || (v&gt;1.0)) {\r\n    return 0L;\r\n  }\r\n\r\n  if ((h &lt; 0.0) || (h &gt; 6.0)) {\r\n    return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) * 65536;\r\n  }\r\n  i = floor(h);\r\n  f = h - i;\r\n  if ( !(i&amp;1) ) {\r\n    f = 1 - f; \/\/ if i is even\r\n  }\r\n  m = v * (1 - s);\r\n  n = v * (1 - s * f);\r\n  switch (i) {\r\n  case 6:\r\n  case 0:\r\n    return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);\r\n  case 1:\r\n    return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);\r\n  case 2:\r\n    return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);\r\n  case 3:\r\n    return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);\r\n  case 4:\r\n    return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);\r\n  case 5:\r\n    return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);\r\n  }\r\n}\r\n<\/pre>\n<p>The circuit is very simple:<br \/>\n<a href=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2010\/09\/HSV-colour-cycling-LED-on-Arduino_pola2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-5814\" title=\"HSV colour cycling LED on Arduino Pretty much everyone tries the RGB colour cycler when they get their first Arduino. This variant cycles through the HSV colour wheel, though at fixed saturations and values.\" alt=\"\" src=\"http:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2010\/09\/HSV-colour-cycling-LED-on-Arduino_pola2.jpg\" width=\"640\" height=\"778\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2010\/09\/HSV-colour-cycling-LED-on-Arduino_pola2.jpg 640w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2010\/09\/HSV-colour-cycling-LED-on-Arduino_pola2-131x160.jpg 131w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2010\/09\/HSV-colour-cycling-LED-on-Arduino_pola2-263x320.jpg 263w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<ul>\n<li>Digital pin 9 \u00e2\u2020\u2019 165\u00ce\u00a9 resistor \u00e2\u2020\u2019 LED Red pin<\/li>\n<li>Digital pin 10 \u00e2\u2020\u2019 100\u00ce\u00a9 resistor \u00e2\u2020\u2019 LED Green pin<\/li>\n<li>Digital pin 11 \u00e2\u2020\u2019 100\u00ce\u00a9 resistor \u00e2\u2020\u2019 LED Blue pin<\/li>\n<li>GND \u00e2\u2020\u2019 LED common cathode.<\/li>\n<\/ul>\n<p>The different resistor values are to provide a limited current to the <a title=\"desc\" href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=9264\">Triple Output LED RGB &#8211; Diffused<\/a>, as each channel has different requirements. The 165\u00ce\u00a9 resistor is actually two 330\u00ce\u00a9 in parallel; I didn&#8217;t have the right value, and this was the closest I could make with what I had.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pretty much everyone tries the RGB colour cycler when they get their first Arduino. This variant cycles through the HSV colour wheel, though at fixed saturations and values. Code: \/\/ HSV fade\/bounce for Arduino &#8211; scruss.com &#8211; 2010\/09\/12 \/\/ Note that there&#8217;s some legacy code left in here which seems to do nothing \/\/ but [&hellip;]<\/p>\n","protected":false},"author":2,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[2207,2208,150],"class_list":["post-5811","post","type-post","status-publish","format-standard","hentry","category-computers-suck","tag-arduino","tag-electronics","tag-led"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-1vJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/5811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=5811"}],"version-history":[{"count":6,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/5811\/revisions"}],"predecessor-version":[{"id":5813,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/5811\/revisions\/5813"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=5811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=5811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=5811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}