{"id":268,"date":"2008-09-24T16:15:07","date_gmt":"2008-09-24T21:15:07","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/?p=268"},"modified":"2008-09-24T16:15:07","modified_gmt":"2008-09-24T21:15:07","slug":"fading-an-led-from-a-switch","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/268\/","title":{"rendered":"Fading an LED from a switch"},"content":{"rendered":"<p>This example uses a digital input to control a fading LED. The LED turns on when the switch goes from off to on, then fades slowly to black.\u00c2\u00a0 It illustrates two principles:\u00c2\u00a0 the idea of edge detection or state change detection, and the idea of time delay without using <code>delay()<\/code>.<\/p>\n<p><!--more--><\/p>\n<pre>\r\n\r\nint lastState = 0;          \/\/ last state of the switch\r\nint currentState = 0;     \/\/ current state of the switch\r\nint ledLevel = 0;           \/\/ level of the LED analogWrite\r\nlong lastFadeTime = 0;  \/\/ last time you decremented the LED level\r\n\r\nvoid setup() {\r\n  pinMode (2, INPUT);       \/\/ the switch pin\r\n  pinMode(6, OUTPUT);     \/\/ the LED pin\r\n  Serial.begin(9600);        \/\/ initialize serial communication\r\n}\r\n\r\n\r\n\r\nvoid loop() {\r\n  \/\/ read the switch:\r\n  currentState = digitalRead(2);\r\n\r\n  \/\/ if the switch has changed, do something:\r\n  if (currentState != lastState) {\r\n    \/\/ if the switch is high:\r\n    if (currentState == 1) {\r\n      \/\/ set the LED level\r\n      ledLevel = 255;\r\n      Serial.println(\"turned on\");\r\n    } \r\n    \/\/ save the current state of the switch as the last state for next time through:\r\n    lastState = currentState;\r\n  }\r\n\r\n  if (millis() - lastFadeTime > 5) {\r\n    \/\/ fade the LED level:\r\n    if (ledLevel >0 ) {\r\n      \/\/ turn on or off the LED\r\n      analogWrite(6, ledLevel);\r\n      \/\/ decrement the LED level:\r\n      ledLevel--; \r\n    } \r\n    else {\r\n      \/\/ turn the LED off:\r\n      digitalWrite(6, LOW); \r\n    }\r\n    \/\/ note the last time you faded the LED:\r\n    lastFadeTime = millis();\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example uses a digital input to control a fading LED. The LED turns on when the switch goes from off to on, then fades slowly to black.\u00c2\u00a0 It illustrates two principles:\u00c2\u00a0 the idea of edge detection or state change detection, and the idea of time delay without using delay().<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":1,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/268\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}