{"id":268,"date":"2013-08-04T05:44:00","date_gmt":"2013-08-04T05:44:00","guid":{"rendered":"http:\/\/dbbd.sg\/blog\/uncategorized\/simple-colour-changing-ambient-lamp-arduino-3-led\/"},"modified":"2021-05-14T14:18:10","modified_gmt":"2021-05-14T14:18:10","slug":"simple-colour-changing-ambient-lamp-arduino-3-led","status":"publish","type":"post","link":"https:\/\/dbbd.sg\/blog\/2013\/08\/simple-colour-changing-ambient-lamp-arduino-3-led\/","title":{"rendered":"Simple Colour-changing Ambient Lamp (Arduino + 3 LED)"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-2014\" src=\"https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431568785_b5f6d6f794_c.jpg\" width=\"800\" height=\"600\" alt=\"P8034922\" srcset=\"https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431568785_b5f6d6f794_c.jpg 800w, https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431568785_b5f6d6f794_c-300x225.jpg 300w, https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431568785_b5f6d6f794_c-768x576.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>In this continuation of posts on learning to do the most basic of things with the Arduino, the next experiment I made was a colour-changing lamp with 3 different LEDs. I followed <a href=\"http:\/\/www.instructables.com\/id\/How-to-Make-an-LED-Ambient-Mood-Light-A-Beginner-\/\">this simple instructable<\/a> but edited slightly. I noticed there were lots of &#8220;#define&#8221; in the instructable &#8211; it seems that <b>#define<\/b> is something from C that is for defining a constant. What is the difference between using #define and int to define which pins each LED is controlled via? Well in practice nothing much seems to be different except the conceptual difference is that #define is for defining a CONSTANT whereas technically speaking an int would be initialising a VARIABLE that can be changed later on in the program, although in this case we don&#8217;t really intend to change the pin number. But we could, if we wanted to.<\/p>\n<p>Things with # in front of them like <b>#include<\/b> and <b>#define<\/b> are preprocessed before the program compiles. Also, apparently #define does not take up any program space, whereas int is stored as variable in RAM. For more info see this wiki entry on <a href=\"http:\/\/en.wikipedia.org\/wiki\/C_preprocessor\">C Preprocessor<\/a>.<\/p>\n<h1>Colour Changing Lamp<\/h1>\n<p>For this you&#8217;ll need: an arduino uno, 4 jumper wires, 3 LEDs of different colour, and 3 x 330ohm resistors<\/p>\n<pre>int delayTime = 2;\r\nint maxBrightness = 255;\r\nint redPin = 9;\r\nint greenPin = 10;\r\nint bluePin = 11;\r\nint red = 0;\r\nint green = 170;\r\nint blue = 170;<\/pre>\n<p>\/\/ Indicates whether a color will be incrementing (1) or decrementing (0) in brightness<br \/>\nint incR = 1;<br \/>\nint incG = 1;<br \/>\nint incB = 0;<\/p>\n<p>void transition()<br \/>\n{<br \/>\nif (red &gt;= maxBrightness)<br \/>\nincR = 0;<br \/>\nelse if (red &lt;= 0)<br \/>\nincR = 1;<br \/>\nif (green &gt;= maxBrightness)<br \/>\nincG = 0;<br \/>\nelse if (green &lt;= 0)<br \/>\nincG = 1;<br \/>\nif (blue &gt;= maxBrightness)<br \/>\nincB = 0;<br \/>\nelse if (blue &lt;= 0)<br \/>\nincB = 1;<\/p>\n<p>if (incR)<br \/>\nred++;<br \/>\nelse<br \/>\nred&#8211;;<br \/>\nif(incG)<br \/>\ngreen++;<br \/>\nelse<br \/>\ngreen&#8211;;<br \/>\nif(incB)<br \/>\nblue++;<br \/>\nelse<br \/>\nblue&#8211;;<br \/>\n}<\/p>\n<p>void setColor()<br \/>\n{<br \/>\nanalogWrite(redPin, red);<br \/>\nanalogWrite(greenPin, green);<br \/>\nanalogWrite(bluePin, blue);<br \/>\n}<\/p>\n<p>void setup()<br \/>\n{<br \/>\n\/\/ do nothing<br \/>\n}<\/p>\n<p>void loop()<br \/>\n{<br \/>\ntransition();<br \/>\nsetColor();<br \/>\ndelay(delayTime);<br \/>\n}<\/p>\n<p><center><img loading=\"lazy\" class=\"alignnone size-full wp-image-2019\" src=\"https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431457105_592d9a1e64_c.jpg\" width=\"601\" height=\"799\" alt=\"ambientled_bb\" srcset=\"https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431457105_592d9a1e64_c.jpg 601w, https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431457105_592d9a1e64_c-226x300.jpg 226w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/> <iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/6dJmT3XqbQc\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/center><\/p>\n<h1>Stopping an Arduino Sketch<\/h1>\n<p>Now that you have this running, you might want to know how to make sketches stop. This baffled me for some time. My solution was eventually to create a blank sketch and to run it.<\/p>\n<pre>void setup(){}\r\nvoid loop(){}<\/pre>\n<hr \/>\n<p>See also: <a href=\"http:\/\/openurbanism.blogspot.sg\/2013\/08\/simple-light-meter-arduino-servo.html\">Simple Light Meter (Arduino, Photocell and Servo)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this continuation of posts on learning to do the most basic of things with the Arduino, the next experiment I made was a colour-changing lamp with 3 different LEDs. I followed this simple instructable but edited slightly. I noticed there were lots of &#8220;#define&#8221; in the instructable &#8211; it seems that #define is something [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[536,535,625,150],"jetpack_featured_media_url":"https:\/\/dbbd.sg\/blog\/wp-content\/uploads\/2021\/05\/9431568785_b5f6d6f794_c.jpg","_links":{"self":[{"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/posts\/268"}],"collection":[{"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":1,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":2022,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/posts\/268\/revisions\/2022"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/media\/2014"}],"wp:attachment":[{"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbbd.sg\/blog\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}