{"id":2068,"date":"2018-02-05T22:25:59","date_gmt":"2018-02-06T02:25:59","guid":{"rendered":"http:\/\/nagashur.com\/blog\/?p=2068"},"modified":"2018-02-06T22:50:05","modified_gmt":"2018-02-07T02:50:05","slug":"code-de-la-rampe-a-led-intelligente-version-arduino","status":"publish","type":"post","link":"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/","title":{"rendered":"Code de la rampe \u00e0 LED intelligente version Arduino"},"content":{"rendered":"<p>Bonjour \u00e0 tous! Dans des billets ant\u00e9rieurs, je vous pr\u00e9sentais <a href=\"http:\/\/nagashur.com\/blog\/2017\/07\/03\/cablage-de-rampe-a-led-intelligente-version-arduino\/\">un montage simple<\/a> de <a href=\"http:\/\/nagashur.com\/blog\/2017\/06\/30\/rampe-a-led-intelligente-cuisine-via-arduino-capteur-pir\/\">rampe\u00a0 \u00e0 LED intelligente<\/a>, bas\u00e9e sur un Arduino. Suite aux nombreuses requ\u00eates, voici le billet fournissant le code de la lampe \u00e0 LED intelligente.<\/p>\n<p><!--more--><\/p>\n<p>Je modifierai ce billet ult\u00e9rieurement pour expliquer le code de la lampe \u00e0 LED intelligente, mais il est comment\u00e9 (au moins les variables, que vous pouvez ajuster), et fonctionne. Donc vu la demande, je poste d\u00e9j\u00e0 le code, que voici ci apr\u00e8s (je le mettrai sur un d\u00e9pot github) :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/**\r\n * Licence : GNU GPL V3\r\n * by Audrey robinel\r\n * this simple code for Arduino controls an automatic light, that activates\r\n * and deactivate depending on the output of a PIR sensor. There is also a progressive\r\n * fading in and fading out. fade speed can be set in the code.\r\n * v0.4\r\n *\/\r\n\r\n\/\/ constants won't change. They're used here to\r\n\/\/ set pin numbers:\r\nconst int buttonPin = 4;   \/\/ the number of the pushbutton pin\r\nconst int ledPin = 3;      \/\/ the number of the LED pin\r\nint ir_sensor_pin=7;       \/\/ where the PIR sensor output is plugged\r\n\r\nint buttonLedPin=6;\r\nint brightness = 0;    \/\/ how bright the LED is\r\nint fadeAmount = 5;    \/\/ how many points to fade the LED by\r\n\r\n\r\n\r\n\/\/int lightsState = LOW;              \/\/LOW = no change, HIGH = changing.\r\nint lightFadeDir=0;                   \/\/0= no change, 1= fading in, -1 = fading out.\r\nint lightBrightness=0;                \/\/light current brightness, from 0 to 255. starts at 0 by default\r\nint lightFadeAmount=1;                \/\/by how much we change the light level at each iteration\r\nint lightFadeInDelay=5;               \/\/how long do we wait between two light adjustment (ms) for fade in\r\nint lightFadeOutDelay=30;             \/\/how long do we wait between two light adjustment (ms) for fade out\r\nlong lastLightBrightnessChangeTime=0; \/\/last time when light was adjusted\r\n\r\nint ledState = LOW;           \/\/ the current state of the output pin\r\nint buttonState;              \/\/ the current reading from the input pin\r\nint lastButtonState = LOW;    \/\/ the previous reading from the input pin\r\n\r\nint bl_brightness = 0;        \/\/ how bright the LED indicator is\r\nint bl_fadeAmount = 1;        \/\/ how much the LED changes at each step\r\nlong last_bl_change_time = 0; \/\/ when did the led indicator change last\r\nint bl_fade_time=5;           \/\/ how many time between to LED indicator adjustments\r\n\r\nint mode=2;               \/\/mode 1 is manual mode\r\n                          \/\/mode 2 is auto mode\r\n\r\n\r\nlong buttonPressStartTime=0;      \/\/when the button started beeing pressed\r\nconst long longPressDuration=1000;  \/\/the duration of a long press (ms)\r\n\r\n\r\n\r\nlong last_ir_detection_time=0;    \/\/ last time when an IR event was detected.\r\nlong auto_off_delay=120000;       \/\/how long to wait for lights shutdown after the last IR event\r\n\r\nlong lastDebounceTime = 0;  \/\/ the last time the output pin was toggled\r\nlong debounceDelay = 50;    \/\/ the debounce time; increase if the output flickers\r\nint i=0;\r\n\r\nvoid setup() \r\n{\r\n  pinMode(buttonPin, INPUT_PULLUP);\r\n  pinMode(ir_sensor_pin, INPUT);\r\n  pinMode(ledPin, OUTPUT);\r\n  pinMode(buttonLedPin, OUTPUT);\r\n  Serial.begin(9600);\r\n\r\n  \/\/ set initial LED state\r\n  digitalWrite(ledPin, ledState);\r\n}\r\n\r\nvoid loop() \r\n{\r\n  \/\/ read the state of the switch into a local variable:\r\n  int reading = digitalRead(buttonPin);\r\n\r\n  if(mode==2)\/\/if auto mode\r\n  {\r\n    \/\/ set the brightness of the button's LED:\r\n    analogWrite(buttonLedPin, bl_brightness);\r\n    if (millis() &gt; (last_bl_change_time + bl_fade_time))\r\n    {\r\n      bl_brightness=bl_brightness+bl_fadeAmount;\r\n      last_bl_change_time=millis();\r\n      if (bl_brightness &lt;= 0 || bl_brightness &gt;= 255) \r\n      {\r\n        bl_fadeAmount = -bl_fadeAmount ;\r\n      }\r\n      if(bl_brightness&gt;255)\r\n          {bl_brightness=255;}\r\n      else if(bl_brightness&lt;0) {bl_brightness=0;} } } else if(mode==1) \/\/if manual mode { digitalWrite(buttonLedPin, HIGH); } \/\/debouncing the button if (reading != lastButtonState) { \/\/ reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) &gt; debounceDelay) \r\n  {\r\n    if (reading != buttonState) \r\n    {\r\n      buttonState = reading;\r\n      if (buttonState == LOW)\/\/if the button was pushed\r\n      {\r\n\r\n        buttonPressStartTime=millis();\r\n\r\n        while(buttonState==LOW)\r\n        {\r\n          buttonState=digitalRead(buttonPin);\r\n          delay(1);\r\n        }\r\n\r\n        \/\/long press detected, changing mode\r\n        if(millis()&gt;buttonPressStartTime+longPressDuration)\r\n        {\r\n\r\n          if(mode==1)\r\n            {mode=2;}\r\n          else if(mode==2)\r\n            {mode=1;}\r\n        }\r\n        \r\n        \/\/short press detected\r\n        else\r\n        {\r\n          if(lightFadeDir==0)\r\n          {\r\n            if(lightBrightness==0)\r\n            {\r\n              lightFadeDir=1;\r\n            }\r\n            else\r\n            {\r\n              lightFadeDir=-1;\r\n            }\r\n          }\r\n          else\r\n          {\r\n            if(lightFadeDir==1)\r\n            {\r\n              lightFadeDir=-1;\r\n            }\r\n            else if(lightFadeDir==-1)\r\n            {\r\n              lightFadeDir=1;\r\n            }\r\n          }\r\n        }\r\n        \r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/change the light brightness\r\n  if(lightFadeDir!=0)\r\n  {\r\n    int fadeWaitTime=0;\r\n    if(lightFadeDir==1)\r\n    {\r\n      fadeWaitTime=lightFadeInDelay;\r\n    }\r\n    else\r\n    {\r\n      fadeWaitTime=lightFadeOutDelay;\r\n    }\r\n    if (millis() &gt; (lastLightBrightnessChangeTime + fadeWaitTime))\r\n    {\r\n      lightBrightness=lightBrightness+lightFadeAmount*lightFadeDir;\r\n      lastLightBrightnessChangeTime=millis();\r\n      if(lightBrightness&lt;=0) { lightBrightness=0; lightFadeDir=0; } else if(lightBrightness&gt;=255)\r\n      {\r\n        lightBrightness=255;\r\n        lightFadeDir=0;\r\n      }\r\n      analogWrite(ledPin,lightBrightness);\r\n     }\r\n  }\r\n  \r\n  int ir_status=digitalRead(ir_sensor_pin);\r\n  if(ir_status==1)\r\n  {\r\n    last_ir_detection_time=millis();\r\n    if(lightFadeDir==0 &amp;&amp; mode==2)\r\n    {\r\n      if(lightBrightness&lt;255) { lightFadeDir=1; } } } else { if(millis()&gt;last_ir_detection_time+auto_off_delay &amp;&amp; mode==2)\r\n    {\r\n      if(lightBrightness&gt;0)\r\n      {\r\n        lightFadeDir=-1;\r\n      }\r\n    }\r\n  }\r\n \r\n  lastButtonState = reading;\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bonjour \u00e0 tous! Dans des billets ant\u00e9rieurs, je vous pr\u00e9sentais un montage simple de rampe\u00a0 \u00e0 LED intelligente, bas\u00e9e sur un Arduino. Suite aux nombreuses requ\u00eates, voici le billet fournissant le code de la lampe \u00e0 LED intelligente.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[222,653,208],"tags":[174,655,111,7,656,657,629,307,477,654],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Code de la rampe \u00e0 LED intelligente version Arduino - Nagashur<\/title>\n<meta name=\"description\" content=\"Le code de la lampe \u00e0 LED intelligente bas\u00e9e sur un Arduino, un capteur PIR et un MOSFET, permettant d&#039;allumer et \u00e9teindre progressivement une bande de LED ou un \u00e9clairage DC, avec un contr\u00f4le manuel ou automatique, assujetti \u00e0 un capteur de pr\u00e9sence (PIR).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Code de la rampe \u00e0 LED intelligente version Arduino - Nagashur\" \/>\n<meta property=\"og:description\" content=\"Le code de la lampe \u00e0 LED intelligente bas\u00e9e sur un Arduino, un capteur PIR et un MOSFET, permettant d&#039;allumer et \u00e9teindre progressivement une bande de LED ou un \u00e9clairage DC, avec un contr\u00f4le manuel ou automatique, assujetti \u00e0 un capteur de pr\u00e9sence (PIR).\" \/>\n<meta property=\"og:url\" content=\"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"Nagashur\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-06T02:25:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-07T02:50:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@audreyrobinel\" \/>\n<meta name=\"twitter:site\" content=\"@audreyrobinel\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/nagashur.com\/blog\/#website\",\"url\":\"http:\/\/nagashur.com\/blog\/\",\"name\":\"Nagashur\",\"description\":\"raspi et arduino FTW :)\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/nagashur.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/#webpage\",\"url\":\"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/\",\"name\":\"Code de la rampe \\u00e0 LED intelligente version Arduino - Nagashur\",\"isPartOf\":{\"@id\":\"http:\/\/nagashur.com\/blog\/#website\"},\"datePublished\":\"2018-02-06T02:25:59+00:00\",\"dateModified\":\"2018-02-07T02:50:05+00:00\",\"author\":{\"@id\":\"http:\/\/nagashur.com\/blog\/#\/schema\/person\/11a329fa33df4e94444716624beb8186\"},\"description\":\"Le code de la lampe \\u00e0 LED intelligente bas\\u00e9e sur un Arduino, un capteur PIR et un MOSFET, permettant d'allumer et \\u00e9teindre progressivement une bande de LED ou un \\u00e9clairage DC, avec un contr\\u00f4le manuel ou automatique, assujetti \\u00e0 un capteur de pr\\u00e9sence (PIR).\",\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/nagashur.com\/blog\/2018\/02\/05\/code-de-la-rampe-a-led-intelligente-version-arduino\/\"]}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/nagashur.com\/blog\/#\/schema\/person\/11a329fa33df4e94444716624beb8186\",\"name\":\"sky99\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6yqc2-xm","jetpack-related-posts":[{"id":1974,"url":"http:\/\/nagashur.com\/blog\/2017\/06\/30\/rampe-a-led-intelligente-cuisine-via-arduino-capteur-pir\/","url_meta":{"origin":2068,"position":0},"title":"Une rampe \u00e0 LED intelligente pour la cuisine via Arduino avec capteur PIR","date":"30 juin 2017","format":false,"excerpt":"Dans ma cuisine, au dessus de l'\u00e9vier, j'ai rajout\u00e9 deux lampes basses consommation. Cependant cette solution est loin d'\u00eatre id\u00e9ale : il est facile d'oublier d'\u00e9teindre ces lumi\u00e8res, l'\u00e9clairage est soit \u00e9teint, soit \u00e0 fond (\u00e9blouissant en pleine nuit), et je pr\u00e9f\u00e8re \u00e9viter d'avoir du 220V au dessus de l'\u00e9vier.\u2026","rel":"","context":"Dans &quot;Arduino&quot;","img":{"alt_text":"rampe \u00e0 LED intelligente : la carte de commande","src":"https:\/\/i2.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2017\/06\/light_board_7502_1024.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2007,"url":"http:\/\/nagashur.com\/blog\/2017\/07\/03\/cablage-de-rampe-a-led-intelligente-version-arduino\/","url_meta":{"origin":2068,"position":1},"title":"C\u00e2blage de la rampe \u00e0 LED intelligente version Arduino","date":"3 juillet 2017","format":false,"excerpt":"Dans un pr\u00e9c\u00e9dent billet, nous avons d\u00e9crit une rampe \u00e0 LED intelligente utilisable dans la maison ou dehors. Aujourd'hui, nous nous pencherons sur la construction et le c\u00e2blage de la rampe \u00e0 LED intelligente, en version Arduino, sur une breadboard (donc sans soudures). Mat\u00e9riel n\u00e9cessaire Voyons maintenant le mat\u00e9riel n\u00e9cessaire\u2026","rel":"","context":"Dans &quot;Arduino&quot;","img":{"alt_text":"c\u00e2blage de la rampe \u00e0 LED intelligente, miniature d'article","src":"https:\/\/i2.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2017\/06\/thumbnail_PIR_detector_arduino_oil_painting_7530.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1125,"url":"http:\/\/nagashur.com\/blog\/2015\/08\/10\/controleur-de-led-simple-utilisant-un-tip120\/","url_meta":{"origin":2068,"position":2},"title":"Contr\u00f4leur de LED simple utilisant un TIP120","date":"10 ao\u00fbt 2015","format":false,"excerpt":"Dans le cadre du projet Domochevsky, nous cherchons \u00e0 impl\u00e9menter un syst\u00e8me d'\u00e9clairage intelligent, tel que nous en avons discut\u00e9 dans ce pr\u00e9c\u00e9dent billet. Nous n'impl\u00e9menterons pas toutes les fonctionnalit\u00e9s d\u00e9crites imm\u00e9diatement, mais nous allons cr\u00e9er une base simple et extensible sur laquelle nous pourrons construire la suite du projet.\u2026","rel":"","context":"Dans &quot;Arduino&quot;","img":{"alt_text":"contr\u00f4leur de LED basique sur breadboard","src":"https:\/\/i1.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2015\/08\/basic_lightController_beta_4611_800px.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":819,"url":"http:\/\/nagashur.com\/blog\/2013\/07\/13\/interfacer-un-ecran-lcd-texte-avec-un-atmega-ou-un-arduino\/","url_meta":{"origin":2068,"position":3},"title":"Interfacer un \u00e9cran LCD texte avec un ATmega ou un Arduino","date":"13 juillet 2013","format":false,"excerpt":"Dans l'optique de mon projet Milapli, Il faudra bien, \u00e0 un moment donn\u00e9, afficher les donn\u00e9es quelquepart. Dans l'absolu, on pourrait se contenter de stocker ces donn\u00e9es, puis d'y acc\u00e9der par le r\u00e9seau. Cependant, on peut trouver pour une dizaine d'euros des \u00e9crans LCD 2*16 caract\u00e8res RGB, ou divers autres\u2026","rel":"","context":"Dans &quot;Arduino&quot;","img":{"alt_text":"LCD Sparkfun 5V White on black","src":"https:\/\/i1.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2013\/07\/img_2136_crop-e1438906257917.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":543,"url":"http:\/\/nagashur.com\/blog\/2013\/01\/01\/controler-une-led-depuis-les-ports-gpio-du-raspberry-pi\/","url_meta":{"origin":2068,"position":4},"title":"Contr\u00f4ler une LED depuis les GPIO du Raspberry PI","date":"1 janvier 2013","format":false,"excerpt":"Un des grands int\u00e9r\u00eats du Raspberry pi est qu'il dispose de GPIO utilisables comme entr\u00e9e ou sortie afin de lire des capteurs ou commandes des syst\u00e8mes. Nous nous verrons ici comment contr\u00f4ler les GPIO du Raspberry pi configur\u00e9s en mode sorte, \u00e0 travers un exemple concret ou nous commanderons une\u2026","rel":"","context":"Dans &quot;\u00e9lectronique&quot;","img":{"alt_text":"LED de 5mm : rouge, jaune, verte, orange, bleue, blanche et infrarouge","src":"https:\/\/i2.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2013\/01\/LEDs_5mm_4755_800px.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":870,"url":"http:\/\/nagashur.com\/blog\/2014\/03\/24\/controleur-daquarium-circuit-de-base\/","url_meta":{"origin":2068,"position":5},"title":"Contr\u00f4leur d'aquarium : circuit de base","date":"24 mars 2014","format":false,"excerpt":"Aujourd'hui, je vais vous pr\u00e9senter rapidement le circuit de base de mon contr\u00f4leur d'aquarium. Pour l'instant, il y a juste un \u00e9cran LCD, une sonde waterproof immerg\u00e9e dans l'aquarium, et une sonde prenant la temp\u00e9rature de l'air. Cette base est amen\u00e9e \u00e0 \u00e9voluer, mais je poste d\u00e9j\u00e0 les sch\u00e9mas simples\u2026","rel":"","context":"Dans &quot;aquariophilie&quot;","img":{"alt_text":"Aquascape dans un aquarium de 50l","src":"https:\/\/i1.wp.com\/nagashur.com\/blog\/wp-content\/uploads\/2014\/03\/aquarium_rlieh292363_800px.jpg?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/posts\/2068"}],"collection":[{"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/comments?post=2068"}],"version-history":[{"count":0,"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/posts\/2068\/revisions"}],"wp:attachment":[{"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/media?parent=2068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/categories?post=2068"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nagashur.com\/blog\/wp-json\/wp\/v2\/tags?post=2068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}