{"id":1068,"date":"2025-01-19T16:12:18","date_gmt":"2025-01-19T21:12:18","guid":{"rendered":"http:\/\/ve2cuy.com\/420-1c4\/?page_id=1068"},"modified":"2025-02-05T17:02:42","modified_gmt":"2025-02-05T22:02:42","slug":"arduino-serial-printlnval-et-digitalread","status":"publish","type":"page","link":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/arduino-serial-printlnval-et-digitalread\/","title":{"rendered":"Arduino &#8211; Serial.println() et digitalRead()"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"696\" height=\"464\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/digital-age-696x464-1.jpg\" alt=\"\" class=\"wp-image-1106\" style=\"width:522px;height:348px\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/digital-age-696x464-1.jpg 696w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/digital-age-696x464-1-300x200.jpg 300w\" sizes=\"auto, (max-width: 696px) 100vw, 696px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Contenu:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Afficher dans la console s\u00e9rie<\/mark>\n<ul class=\"wp-block-list\">\n<li>Serial.begin()<\/li>\n\n\n\n<li>Serial.print()<\/li>\n\n\n\n<li>Afficher un nombre en DEC, OCT, BIN et HEX<\/li>\n\n\n\n<li>La MACRO <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">AFFICHER<\/mark><\/li>\n\n\n\n<li>Utilisation de la librairie Streaming de Mikal Hart<\/li>\n\n\n\n<li>Retour sur l&rsquo;incr\u00e9mentation d&rsquo;une variable (++, +=, &#8230;)<\/li>\n\n\n\n<li>Retour sur l&rsquo;instruction &lsquo;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>if<\/strong><\/mark>&lsquo;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Utilisation d&rsquo;une broche en mode d&rsquo;entr\u00e9e (lecture)<\/mark>\n<ul class=\"wp-block-list\">\n<li>pinMode(noBroche, INPUT)<\/li>\n\n\n\n<li>digitalRead()<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">Utilisation d&rsquo;un pushButton dans Wokwi<\/mark>\n<ul class=\"wp-block-list\">\n<li>Utilisation d&rsquo;une r\u00e9sistance pour forcer l&rsquo;\u00e9tat \u00e0 0.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Gestion du rel\u00e2chement d&rsquo;un pushButton <\/mark>\n<ul class=\"wp-block-list\">\n<li>L&rsquo;instruction &lsquo;while()&rsquo;<\/li>\n\n\n\n<li>Gestion du bruit d&rsquo;un interrupteur (debouce)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">Gestion d&rsquo;une permutation d&rsquo;\u00e9tat d&rsquo;un pushButton<\/mark><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 &#8211; <strong>La fonction <em>Serial<\/em>.println(val)<\/strong><\/h2>\n\n\n\n<p>L&rsquo;IDE Arduino nous propose une console d&rsquo;affichage qu&rsquo;il est possible d&rsquo;utiliser, via le protocole UART (COM[1..9] sous windows) pour afficher des messages et des \u00e9tats des \u00e9l\u00e9ments d&rsquo;un projet.<\/p>\n\n\n\n<p>Comme par exemple, afficher l&rsquo;\u00e9tat ON\/OFF d&rsquo;un interrupteur.<\/p>\n\n\n\n<p>Ces messages seront disponibles pendant l&rsquo;\u00e9tape de prototypage du projet.<\/p>\n\n\n\n<p>La plaquette Arduino doit-\u00eatre connect\u00e9e sur le port USB pour que cette fonction soit disponible. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">Voici la marche \u00e0 suivre pour afficher des messages dans la console de &lsquo;d\u00e9boggage&rsquo;<\/h2>\n\n\n\n<p>1.2 &#8211; Dans un projet, initialiser la vitesse du port de communication s\u00e9rie (UART\/COM) et afficher:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> void setup() {\n  \/\/ 1 - Ouvrir le port s\u00e9rie \u00e0 9600 bps (bits par seconde):\n  \/\/ Note: Sous PlatformIO, la vitesse du terminal est de 9600 Baud.\n  \/\/ Il est possible de la changer, dans le fichier platformio.ini,  avec la propri\u00e9t\u00e9 monitor_speed = 115200. \n  Serial.begin(9600);\n\n  \/\/ 2 - Afficher un message dans la console de d\u00e9bogage\n  Serial.println(\"D\u00e9but du programme\");\n  Serial.println(\"------------------\\n\");\n  Serial.println(\"Voici un message ;-) !\");\n  delay(2000);\n} \/\/ setup()<\/pre>\n\n\n\n<p>1.3 &#8211; T\u00e9l\u00e9verser le projet vers l&rsquo;Arduino puis afficher la console s\u00e9rie:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"392\" height=\"92\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.33.47.png\" alt=\"\" class=\"wp-image-1069\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.33.47.png 392w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.33.47-300x70.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"115\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-1024x115.png\" alt=\"\" class=\"wp-image-1070\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-1024x115.png 1024w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-300x34.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-768x86.png 768w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-1536x173.png 1536w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.34.34-2048x230.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Sous <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">platformIO<\/mark><\/strong>, utiliser les touches <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">CTRL+ALT+S<\/mark><\/strong> pour ouvrir la console s\u00e9rie.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.4 &#8211; <strong><span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span><\/strong><\/h2>\n\n\n\n<p>Programmer un projet qui affiche, dans la console s\u00e9rie, le message suivant en boucle:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"538\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33-1024x538.png\" alt=\"\" class=\"wp-image-1073\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33-1024x538.png 1024w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33-300x158.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33-768x404.png 768w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33-1536x807.png 1536w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-12.38.33.png 1556w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.5 &#8211; Afficher le contenu d&rsquo;une variable <\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int compteur = 1;  \/\/ Je suis une variable de type 'entier sign\u00e9' de port\u00e9e globale.\n\/\/ ------------------\nvoid setup() {\n\n  \/\/ 1 - Ouvrir le port s\u00e9rie \u00e0 9600 bps:\n  Serial.begin(9600);\n\n  \/\/ 2 - Afficher un message sans changer de ligne\n  Serial.print(\"La variable compteur vaut \");\n\n  \/\/ 3 - Afficher la valeur de la variable et changer de ligne.\n  Serial.println(compteur);                    \n\n} \/\/ setup()<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1.6 &#8211; <strong><span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span><\/strong><\/h2>\n\n\n\n<p>Incr\u00e9menter et afficher la valeur de la variable &lsquo;compteur&rsquo; en boucle.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.6.2 &#8211; Utilisation d&rsquo;une Macro vide<\/h2>\n\n\n\n<p>Utilisation d&rsquo;une Macro vide pour activer\/d\u00e9sactiver les messages de la console.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define afficher(x)  Serial.println(x)\n\/\/ #define afficher(x)\n\n...\n\nafficher(\"Afficher un message dans la console ...\");\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.7 &#8211; Afficher un nombre dans une autre base que 10:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> Serial.println(88, BIN); \n\n\/\/ Affiche: 1011000<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.8 &#8211; Voici un exemple complet d&rsquo;affichage:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Fichier:      afficher-entier.ino\n  Auteur:       Alain Boudreault\n  Date:         2021.09.07\n  Description:  Afficher un entier en base 2, 8, 10 et 16.\n  --------------------------------------------------------\n  M-A-J:        A.B.2022.06.14 - Ajout de l'op\u00e9rateur de r\u00e9solution de port\u00e9e ::\n*\/\n\n\/\/ D\u00e9finition des constantes\n#define UN_ENTIER     0b01010101    \/\/ le nombre '85', repr\u00e9sent\u00e9 en format binaire.\n#define UNE_SECONDE   1000          \/\/ 1000 milli-secondes = 1 seconde\n#define VITESSE_UART  9600\n\n\/\/ D\u00e9finition des variables globales\nint compteur = 1;                 \/\/ la variable compteur est d\u00e9finie de fa\u00e7on globale\n\n\/\/ ------------------\nvoid setup() {\n  \/\/ 1 - Ouvrir le port s\u00e9rie \u00e0 9600 bps:\n  Serial.begin(VITESSE_UART);\n  \/\/ 2 - Afficher un message dans la console de d\u00e9boggage\n  Serial.print(\"La variable compteur vaut \");\n  Serial.println(compteur);\n  Serial.println(\"============================\\n\\n\");\n} \/\/ setup()\n\n\/\/ ------------------\nvoid loop() {\n  \/\/ D\u00e9finition d'une variable locale \u00e0 la fonction loop()\n  int compteur = ::compteur++;  \/\/ Initialisation de la variable locale 'compteur' \n                                \/\/ \u00e0 partir de la valeur de la variable globale 'compteur'\n\n  \/\/ Afficher un entier en utilisant tous les formats possibles:\n  Serial.print(compteur);  \n  Serial.print(\" - UN_ENTIER = \");  \n  Serial.print(UN_ENTIER);       \/\/ Afficher en notation d\u00e9cimale\n  Serial.print(\" , DEC: \");  \n  Serial.print(UN_ENTIER, DEC);  \/\/ Afficher en notation d\u00e9cimale\n  Serial.print(\" , HEX: \");   \n  Serial.print(UN_ENTIER, HEX);  \/\/ Afficher en notation hexad\u00e9cimale\n  Serial.print(\" , OCT: \"); \n  Serial.print(UN_ENTIER, OCT);  \/\/ Afficher en notation octale\n  Serial.print(\" , BIN: \"); \n  Serial.println(UN_ENTIER, BIN);  \/\/ Afficher en notation binaire\n  delay(UNE_SECONDE);\n\n} \/\/ loop()\n\n\/\/ FIN du fichier <\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.9 &#8211; <span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span><\/h2>\n\n\n\n<p>Modifier le projet pr\u00e9c\u00e9dent pour qu&rsquo;il affiche, <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">en utilisant la librairie &lt;Streaming.h><\/mark><\/strong> la valeur (dans toutes les bases) de la variable &lsquo;compteur&rsquo;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"387\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.20.22-1024x387.png\" alt=\"\" class=\"wp-image-1081\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.20.22-1024x387.png 1024w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.20.22-300x113.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.20.22-768x290.png 768w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.20.22.png 1388w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color\">Rappel &#8211; Incr\u00e9menter une variable i<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>i = i + 1;      \/\/ Augmenter la valeur de i de 1\ni++;            \/\/ Augmenter la valeur de i de 1\ni+=5;           \/\/ Augmenter la valeur de i de 5\n\ni = i - 1;      \/\/ Diminuer la valeur de i de 1\ni--;            \/\/ Diminuer la valeur de i de 1\ni-=5;           \/\/ Diminuer la valeur de i de 5\n\nif ( i++ == 2 ) \/\/ Tester si i = 2 puis augmenter la valeur de i de 1\nif ( ++i == 2 ) \/\/ Augmenter la valeur de i de 1 puis tester si i = 2 \n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.10 &#8211; Streaming.h<\/h2>\n\n\n\n<p>Voici un exemple d&rsquo;utilisation de la librairie &lt;Streaming.h&gt;  de Mikal Hart pour l&rsquo;affichage:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;Arduino.h>\n#include &lt;Streaming.h>\n#define VITESSE_SERIAL  9600\n\nvoid setup() {\n    Serial.begin(VITESSE_SERIAL);\n    Serial &lt;&lt; F(\"D\u00e9but du programme ...\\n\");\n    Serial &lt;&lt; F(\"PI vaut : \") &lt;&lt; PI &lt;&lt; endl;\n} \/\/ setup()\n\nvoid loop() {\n    static unsigned long i = 0;\n    Serial &lt;&lt; \"i = \" &lt;&lt; ++i &lt;&lt; endl; \n} \/\/ loop()\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ D\u00e9pendance sous PlatformIO\nlib_deps = mikalhart\/Streaming @ ^1.0.0<\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/mark><\/strong>: Cette technique fonctionne aussi pour les \u00e9crans LCD.<\/p>\n\n\n\n<p>R\u00e9f\u00e9rence sur <a href=\"https:\/\/github.com\/janelia-arduino\/Streaming\">GitHub<\/a> et <a href=\"https:\/\/docs.arduino.cc\/libraries\/streaming\/\">Arduino.cc<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">2 &#8211; Lecture de l&rsquo;\u00e9tat d&rsquo;une boche digitale, la fonction <strong><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">digitalRead(pin)<\/span><\/em><\/strong><\/h2>\n\n\n\n<p>2.1 &#8211; La fonction <strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">digitalRead(pin)<\/mark><\/em><\/strong> permet de lire l&rsquo;\u00e9tat d&rsquo;une GPIO digitale qui est programm\u00e9e en mode lecture.  Voici comment l&rsquo;utiliser:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Fichier:      lire-switch.ino\n  Auteur:       Alain Boudreault\n  Date:         2021.09.07\n  Description:  Allumer la LED sur GPIO-03 \u00e0 partir de l'\u00e9tat de la switch sur GPIO-02.\n\n  Note:         ON = 1, OFF = 0\n*\/\n\n\/\/ Les MACROs\n#define LED       3 \n#define SWITCH    2    \n\n\/\/ Les variables globales\nint etatBouton      = 0;\n\n\/\/ Renseigner les GPIO\nvoid setup() {\n  pinMode(LED, OUTPUT);     \/\/ OUTPUT et INPUT sont des macros de l'IDE Arduino\n  pinMode(SWITCH, INPUT);   \/\/ Renseigner la broche 'SWITCH' en mode d'entr\u00e9e (INPUT)\n} \/\/ setup()\n\n\nvoid loop() {\n  etatBouton = digitalRead(SWITCH);  \/\/ Lire l'\u00e9tat du bouton.  \n                                     \/\/ digitalRead() retourne '1' si bouton appuy\u00e9, sinon '0'\n  digitalWrite(LED, etatBouton);     \/\/ Utiliser l'\u00e9tat du bouton (1|0) pour Allumer\/\u00c9teindre la DEL\n} \/\/ loop()<\/pre>\n\n\n\n<p>2.2 &#8211; Tester le projet<\/p>\n\n\n\n<p>2.3 &#8211; Remplacer la fonction loop() par le code suivant:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void loop() {\n  etatBouton = digitalRead(SWITCH);\n  if (etatBouton == 1) { \/\/ Ou bien etatBouton == HIGH,  HIGH est une macro de l'IDE (HIGH|LOW)\n    Serial.println(\"Interrupteur ON!\");\n  } \n  digitalWrite(LED, etatBouton); \n  delay(200);\n} \/\/ loop()<\/pre>\n\n\n\n<p>2.4 &#8211; Tester le projet<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"281\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.56.28-1024x281.png\" alt=\"\" class=\"wp-image-1086\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.56.28-1024x281.png 1024w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.56.28-300x82.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.56.28-768x211.png 768w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-13.56.28.png 1522w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-red-color has-text-color\">2.4.5 &#8211; Rappel de l&rsquo;instruction &lsquo;if&rsquo;<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if ( condition ) {\n instructions si vrai\n}\n\nif ( condition ) {\n instructions si vrai\n} else\n{\n  instructions si faux\n}\n\nif ( i == 2 )      \/\/ Attention au double == sinon la variable sera r\u00e9affect\u00e9e par 2\nif ( variable )    \/\/ Si variable = 0 alors faux sinon vrai\nif ( fonction() )  \/\/ Si fonction = 0 alors faux sinon vrai\nif ( 1 )           \/\/ Toujours vrai<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.5 &#8211; <span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span><\/h2>\n\n\n\n<p>Modifier le projet pr\u00e9c\u00e9dent pour qu&rsquo;il affiche le nombre de fois que l&rsquo;interrupteur a \u00e9t\u00e9 appuy\u00e9.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"592\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.01.12.png\" alt=\"\" class=\"wp-image-1088\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.01.12.png 940w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.01.12-300x189.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.01.12-768x484.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-vivid-cyan-blue-color has-text-color\">2.6 &#8211; Voici un exemple complet et document\u00e9<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/* \n   -----------------------------------------------------------------------\n   Projet:         Introduction \u00e0 Arduino\n   Nom du fichier: allumer LED avec bouton (momentary) V1.ino\n   Auteur:         Alain Boudreault, Robert Turenne\n   Date:           2021.08.13\n   -----------------------------------------------------------------------\n   Description:    Petit programme qui allume la DEL int\u00e9gr\u00e9e au\n                   module Arduino Uno sur une broche (pin). Un bouton est plac\u00e9 \n                   sur une autre broche\n   -----------------------------------------------------------------------\n   M-A-J: \n   AB2021.07.26 - Modification du d\u00e9lai ON\/OFF\n   RT-21.8.13   - Version avec bouton\n   -----------------------------------------------------------------------\n*\/\n\n\n\/\/ Les MACROs sont d\u00e9finies au d\u00e9but du programme, avant toutes fonctions!\n\/\/ La convention dicte de les nommer avec des caract\u00e8res MAJUSCULES\n#define DEL_ROUGE 3       \/\/ La broche de la DEL du projet\n\/\/#define BOUTON_PIN    2 \/\/ La broche pour le bouton (voir plus bas pour la mise en commentaire)\n#define ALLUMER   1\n#define ETEINDRE  0\n#define DELAI    100     \/\/ Le d\u00e9lai de transition entre ON\/OFF de la DEL\n\n\/*\n  faire attention au branchement des pin 2 et 3. Les connecteurs D ont acc\u00e8s \u00e0 2 pins en g\u00e9n\u00e9ral.\n  Celle qui est \u00e0 l'ext\u00e9rieure est souvent celle qui sera connect\u00e9e dans notre montagne. Ainsi, \n  le connecteur D2 pr\u00e9sente les pin 2 et 3, mais la pin 2 est \u00e0 l'ext\u00e9rieur.\n*\/\n\n\/\/ Les variables et dans certains cas, les constantes, sont d\u00e9finies au d\u00e9but du programme, \n\/\/ apr\u00e8s les macros, avant toute fonction.\n\/\/ La convention est de commencer en minuscules et mettre un majuscule au d\u00e9but de chaque nouveau mot\n\/\/ Notez qu'on peut aussi utiliser des constantes, similaires au #define, par exemple:\nconst int boutonPin = 2;\n\/\/ On pourrait \u00eatre confus quant \u00e0 l'utilisation de la m\u00e9moire, limit\u00e9e dans le Arduino, entre #define et \"const int\".\n\/\/ mais dans la plupart des cas, l'utilisation de la m\u00e9moire est identique. \n\/\/ voir https:\/\/forum.arduino.cc\/t\/the-difference-between-define-and-const\/65169\/16 \u00e0 partir de l'intervention #18\n\/\/ ou https:\/\/www.baldengineer.com\/const-vs-define-when-do-you-them-and-why.html\n\/\/ Mais on doit absolument utiliser la notation Arduino pour les variables, par exemple:\nint boutonEnfonce = 0; \/\/indicateur pour  le bouton. = 1 si enfonc\u00e9\n\n\/\/ D\u00c9BUT du code -->\n\n\/\/ Cette fonction sera ex\u00e9cut\u00e9e une seule fois, au d\u00e9marrage du programme.\n\/\/ Elle sert \u00e0 renseigner, entre autre, la fonction des diff\u00e9rentes\n\/\/ broches du MCU.\nvoid setup()\n{\n  \/\/ TOUJOURS initialiser TOUTES LES PINS UTILIS\u00c9ES\n  \/\/ Note:  Elles conserveront leur \u00e9tat durant toute la dur\u00e9e de l'application, \n  \/\/        incluant dans les autres fonctions.\n  pinMode(DEL_ROUGE, OUTPUT); \/\/ LED Externe\n  pinMode(boutonPin, INPUT);  \/\/ bouton\n\n\/\/ Initialiser (pr\u00e9parer) le port s\u00e9rie (avec le USB) \u00e0 une certaine vitesse\n\/\/ L'instruction indiquera au compilateur d'int\u00e9grer le code n\u00e9cessaire (invisible pour nous)\n\/\/ La vitesse (ici 9600 bps - bits par secondes) doit \u00eatre la m\u00eame sur la console\n  Serial.begin(9600);\n  Serial.println(\"Clignoter led externe avec bouton et console V1\"); \/\/ Imprimer sur la console\n}\n\n\/\/ Cette section est ex\u00e9cut\u00e9e en boucle pour toujours\nvoid loop()\n{\n  \/\/lecture de la pin digitale: les valeurs peuvent seulement \u00eatre 0 ou 1\n  boutonEnfonce = digitalRead(boutonPin); \n\n  if (boutonEnfonce == 1) \/\/ Ne pas oublier le double ==\n  \/\/ Il est aussi possible d'\u00e9crire: \n  \/\/  if (boutonEnfonce == HIGH), if (boutonEnfonce == TRUE), if (boutonEnfonce)\n  {\n    Serial.println(\"Bouton Enfonc\u00e9 : ON !\");\n    delay(100); \/\/d\u00e9lais avant d'allumer la LED\n    digitalWrite(DEL_ROUGE, HIGH); \n    delay(100);\n    digitalWrite(DEL_ROUGE, LOW);\n  } else \n  {\n    \/\/Serial.print(boutonPese);\n    digitalWrite(DEL_ROUGE, LOW);\n  }\n}\n\ufeff<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3 &#8211; <span class=\"has-inline-color has-vivid-red-color\">Laboratoire de synth\u00e8se<\/span> (dur\u00e9e 30 minutes) <\/h2>\n\n\n\n<p>\u00c9crire un programme Arduino qui allume une LED connect\u00e9e sur GPIO D7, une seconde, lorsque l&rsquo;interrupteur connect\u00e9 sur GPIO D4 est appuy\u00e9.<\/p>\n\n\n\n<p>La LED doit s&rsquo;allumer 1\/10 de seconde de moins \u00e0 chaque fois que l&rsquo;interrupteur est appuy\u00e9.<\/p>\n\n\n\n<p>La dur\u00e9e de l&rsquo;\u00e9tat ON de la LED doit \u00eatre affich\u00e9e dans la console s\u00e9rie.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"938\" height=\"686\" src=\"http:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.21.23.png\" alt=\"\" class=\"wp-image-1093\" srcset=\"https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.21.23.png 938w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.21.23-300x219.png 300w, https:\/\/ve2cuy.com\/420-1c4\/wp-content\/uploads\/2021\/09\/Capture-decran-le-2021-09-07-a-14.21.23-768x562.png 768w\" sizes=\"auto, (max-width: 938px) 100vw, 938px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-red-color has-text-color\">3.0.2 &#8211; NOTE: D\u00e9monstration de l&rsquo;utilisation d&rsquo;un pushButton dans Wokwi.<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">3.1 &#8211; D\u00e9tection du rel\u00e2chement du bouton poussoir (pushButton)<\/h2>\n\n\n\n<p>Dans les exemples pr\u00e9c\u00e9dents, nous avons utilis\u00e9 un delai (delay(ms)) pour offrir \u00e0 l&rsquo;utilisateur une p\u00e9riode de temps suffisante pour qu&rsquo;il puisse rel\u00e2cher le bouton et reprendre le programme.<\/p>\n\n\n\n<p>Cette m\u00e9thode ins\u00e8re des d\u00e9lais inutiles dans le programme et propose des temps d&rsquo;attente arbitraires qui ne pourraient pas convenir \u00e0 toutes les situations.<\/p>\n\n\n\n<p>Il serait pr\u00e9f\u00e9rable de bloquer le programme jusqu&rsquo;au rel\u00e2chement de l&rsquo;interrupteur.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading has-luminous-vivid-orange-color has-text-color\"> 3.1.1 &#8211; L&rsquo;instruction &lsquo;while(test d&rsquo;une condition){}&rsquo;<\/h3>\n\n\n\n<p>L&rsquo;instruction &lsquo;while&rsquo; du langage C permet d&rsquo;ex\u00e9cuter un bloc d&rsquo;instruction tant que la condition test\u00e9e est vrai.<\/p>\n\n\n\n<p>Par exemple, le programme suivant va afficher le message &lsquo;<strong>Objet connect\u00e9<\/strong>&lsquo; dans la console \u00e0 l&rsquo;infini car la condition ( 1 == 1 ) sera toujours vrai.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n    Cours:    Objets connect\u00e9s\n    Module:   L'instruction 'while( condition )'\n    Auteur:   Alain Boudreault\n    Date:     2022.09.12\n    ------------------------------------------------------\n    Description:  Pr\u00e9senter des exemples d'utilisation\n                  de l'instruction while() du langage C\n\n    Note:         Lorsque qu'une condition est \u00e9valu\u00e9,\n                  le langage C consid\u00e8re que vrai = 1 et\n                                             faux = 0.\n\n                  Donc, si nous \u00e9crivons if(1){}, le 'if'\n                  sera toujours vrai.                                         \n    ------------------------------------------------------\n*\/\n\nvoid setup() {\n  Serial.begin(9600);\n  Serial.println(\"D\u00e9monstration de l'instruction while(condition)\");\n\n  \/\/ Exemple 01 - une boucle sans fin\n\n  while ( 1 == 1 ) {\n    Serial.println(\"Objet connect\u00e9\");\n    delay(250);\n  } \/\/ while ( 1 == 1 )\n  \/\/ Note:  1 == 1 sera toujours VRAI.  Vrai est \u00e9gal \u00e0 1.\n  \/\/ Il est alors possible d'\u00e9crire: while ( 1 ) et obtenir \n  \/\/ le m\u00eame r\u00e9sultat.  Faire la modification et tester.\n  \/\/ C propose aussi les mots cl\u00e9 'true' pour 1 et 'false' pour z\u00e9ro. \n\n  \/\/ Exemple 02 - une boucle tant que i &lt; 5:\n  int i = 0;\n  while ( i &lt; 5 ) {\n    Serial.print(i);\n    Serial.println(\" est plus petit que 5\");\n    i++;\n    delay(250);\n  } \/\/ while ( 1 == 1 )\n  \/\/ Note: Est-il possible de raccourcir le code pr\u00e9c\u00e9dent?\n\n} \/\/setup()\n\nvoid loop() {\n} \/\/ loop()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-green-cyan-color has-text-color\">Ce projet est disponible sur <a href=\"https:\/\/wokwi.com\/projects\/342626183392789074\">Wokwi<\/a><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left has-vivid-purple-color has-text-color\">3.1.2 &#8211; Attente jusqu&rsquo;au changement d&rsquo;\u00e9tat d&rsquo;un bouton<\/h2>\n\n\n\n<p>Nous allons maintenant utiliser l&rsquo;instruction while() pour attendre jusqu&rsquo;\u00e0 ce que le bouton soit pass\u00e9 de l&rsquo;\u00e9tat appuy\u00e9 (vaut 1) \u00e0 rel\u00e2ch\u00e9 (vaut 0).<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">3.2 &#8211; Laboratoire:  \u00c9crire un programme qui compte le nombre de fois que le bouton sur D2 a \u00e9t\u00e9 appuy\u00e9:<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Note<\/mark><\/strong>: Il faut faire le labo dans Wokwi.  D\u00e9monstration de l&rsquo;enseignant.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading has-vivid-red-color has-text-color\">Solution:<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define BOUTON 2  \/\/ Le pushButton est sur D2\n\nvoid setup() {\n  \/\/ Un commentaire\n  \/\/ G\u00e9rer un bouton de type push\n  Serial.begin(9600);\n  Serial.println(\"Tout va bien ;-)\");\n  pinMode(BOUTON, INPUT);\n} \/\/ setup()\n\nvoid loop() {\n  static int nbFois = 0;                   \/\/ Ex\u00e9cuter cette ligne une seule fois\n  byte etatBouton = digitalRead(BOUTON);   \/\/ Lecture de l'\u00e9tat du bouton (0 ou 1)\n\n  if ( etatBouton == 1 ) {\n      \/\/ Si vrai, afficher nbFois dans la console\n      nbFois++;\n      Serial.print(\"Bouton ON : \");\n      Serial.println(nbFois);\n      delay(50);                      \/\/ \u00c9limination du bruit du bouton\n      while ( digitalRead(BOUTON) );  \/\/ Attendre que le bouton soit relach\u00e9.\n      delay(50);                      \/\/ \u00c9limination du bruit du bouton\n  } \/\/ if etatBouton == 1\n  \n} \/\/ loop()\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-green-cyan-color has-text-color\">Ce projet est disponible sur <a href=\"https:\/\/wokwi.com\/projects\/342607551952585300\">Wokwi<\/a><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">4 &#8211; Inversion de l&rsquo;\u00e9tat de lecture d&rsquo;une GPIO digitale (<span class=\"has-inline-color has-vivid-cyan-blue-color\">pullUp<\/span>)<\/h2>\n\n\n\n<p>Lorsque nous utilisons une GPIO digitale en mode INPUT, il est essentiel de forcer un \u00e9tat stable, soit 0 ou soit 1 au niveau \u00e9lectrique.<\/p>\n\n\n\n<p>Pour la plaquette de prototypage Arduino Uno, les \u00e9tats 0 et 1 correspondent \u00e0 une tension \u00e9lectrique de 0 ou 5 volts.<\/p>\n\n\n\n<p>Dans l&rsquo;\u00e9tat ouvert de la GPIO digitale en mode INPUT, les \u00e9tats 0 et 1 ne sont pas garanties.<\/p>\n\n\n\n<p>Pour les garantir, il faut forcer l\u00e9g\u00e8rement la GPIO soit vers le n\u00e9gatif (0) de l&rsquo;alimentation soit vers le positif (1).<\/p>\n\n\n\n<p>Le module Hat &lsquo;push Button&rsquo; tire l\u00e9g\u00e8rement (gr\u00e2ce \u00e0 une r\u00e9sistance) l&rsquo;\u00e9tat de l&rsquo;interrupteur, en mode ouvert, vers le cot\u00e9 n\u00e9gatif de l&rsquo;alimentation, ce qui garantie que lorsque le bouton n&rsquo;est pas appuy\u00e9, un signal 0 est envoy\u00e9 vers la GPIO.<\/p>\n\n\n\n<p>L&rsquo;atmega 128P propose une fonction qui permet de tirer l\u00e9g\u00e8rement le signal d&rsquo;ent\u00e9e d&rsquo;une GPIO en mode digitale vers le 1.<\/p>\n\n\n\n<p>Avec l&rsquo;activation de cette fonction, qui s&rsquo;appelle pull up, un digitalRead() sur une GPIO ou rien n&rsquo;est connect\u00e9 retournera la valeur 1 (HIGH).<\/p>\n\n\n\n<p>Note, il n&rsquo;y a pas de fonction inverse disponible (pulldown) sur cette puce.  Il faudra alors utiliser une r\u00e9sistance externe, comme dans le cas du push Button de notre kit de laboratoire.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-luminous-vivid-amber-color has-text-color\">4.1 &#8211; Voici l&rsquo;exemple d&rsquo;un projet qui utilise la fonction pullup<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Programme:  Exemple d'une lecture GPIO avec PullUP\n  Auteur:     Alain Boudreault\n  Date:       2021.09.13\n  Cible:      420-1C4 - objets connect\u00e9s\n  ------------------------------------------------------------\n  Note: Ne pas connecter le push button pour tester le projet.\n\n        Il faut l'ins\u00e9rer seulement une fois que l'application roule.\n*\/\n\n\nvoid setup() {\n  \/\/ Configurer le port s\u00e9rie\n  Serial.begin(9600);\n  Serial.println(\"Debut du programme\\n=========================\\n\");\n  \/\/ Configurer la pin BOUTON en entr\u00e9e et activer la r\u00e9sistance interne de type pull-up.\n  pinMode(2, INPUT_PULLUP);\n  \/\/ Configurer la pin DEL en sortie\n  pinMode(3, 1);\n}\n\nvoid loop() {\n  \/\/ Lire la valeur de l'interrupteur de type poussoir \n  int sensorVal = digitalRead(2);\n  \/\/ Afficher son \u00e9tat\n  Serial.println(sensorVal);\n\n  \/\/ Note: Les valeurs des \u00e9tats de l'interrupteur seront invers\u00e9es.\n  \/\/ HIGH si ouvert, et LOW lorsqu'appuy\u00e9. \n  if (sensorVal == 1) {\n    digitalWrite(3, 0);\n  } else {\n    digitalWrite(3, 1);\n  }\n  delay(200);\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.2 &#8211; <span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span><\/h2>\n\n\n\n<p>Remplacer les valeurs cod\u00e9es \u00e0 la dure par des macros et tester l&rsquo;application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Voici le nom des macros:\nBOUTON        \nDEL           \nON            \nOFF           \nVITESSE_UART\nDELAI\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5 &#8211; D\u00e9tection d&rsquo;un changement d&rsquo;\u00e9tat<\/h2>\n\n\n\n<p>Parfois, il peut \u00eatre utile de d\u00e9terminer si nous passons d&rsquo;un \u00e9tat de lecture vers un autre.<\/p>\n\n\n\n<p>Par exemple, un &lsquo;push&rsquo; sur un bouton pour allumer une DEL et un autre &lsquo;push&rsquo; pour l&rsquo;\u00e9teindre.<\/p>\n\n\n\n<p>Voici justement un exemple de code qui fait exactement cela:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"permutationbouton\">5.1 &#8211; Exemple d&rsquo;une permutation bouton<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n    Fichier:  bouton-permutation.ino\n    Auteur:   Alain Boudreault\n    Date:     2021.09.17\n    --------------------------------------------------------------------------\n    Description: Exemple d'un projet qui tient compte de l'\u00e9tat pr\u00e9c\u00e9dent\n                 du bouton momentan\u00e9.\n\n                 L'application allume la DEL \u00e0 la premi\u00e8re utilisation \n                 du bouton puis l'\u00e9teint \u00e0 la suivante ...\n\n    --------------------------------------------------------------------------\n    M-A-J:    2022.09.12 - Ajout de code pour le rel\u00e2chement du bouton.\n                           Gestion du bruit du bouton\n    --------------------------------------------------------------------------    \n*\/\n\n\/\/ -----------------------------\n\/\/ Les Macros\n#define ON              HIGH\n#define OFF             LOW\n#define BROCHE_BOUTON   4\n#define BROCHE_DEL      3\n#define OUI             true\n#define NON             false\n\n\/\/ -----------------------------\n\/\/ Les variables globales\nbool etatDuBoutonAChange  = NON;\nbool etatDeLaDEL          = OFF;         \/\/ \u00c9tat courant de la DEL (ON\/OFF)\n\n\n\/\/ -----------------------------\nvoid setup()\n{\n  pinMode(BROCHE_BOUTON, INPUT);    \n  pinMode(BROCHE_DEL, OUTPUT);\n}\n\n\/\/ -----------------------------\nvoid loop()\n{\n  if (digitalRead(BROCHE_BOUTON))       \/\/ Lire le bouton\n  {\n    if (etatDuBoutonAChange == OUI)     \/\/ Si l'\u00e9tat du bouton a chang\u00e9 alors\n    {\n      etatDuBoutonAChange = NON;        \/\/ R\u00e9initialiser l'\u00e9tat du bouton\n      etatDeLaDEL = !etatDeLaDEL;       \/\/ Inverser l'\u00e9tat de la DEL (ON\/OFF)\n    }\n    while(digitalRead(BROCHE_BOUTON));  \/\/ Attendre le rel\u00e2chement du bouton\n    delay(50);                          \/\/ \u00c9liminer le bruit du bouton.\n  \n  }                                  \n  else etatDuBoutonAChange = OUI;       \/\/ Sinon, indiquer que l'\u00e9tat du bouton a chang\u00e9\n\n  \/\/ Allumer ou \u00e9teindre la DEL\n  digitalWrite(BROCHE_DEL, etatDeLaDEL); \n \n} \/\/ loop()<\/pre>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">Note<\/span><\/strong>: L&rsquo;explication du code pr\u00e9c\u00e9dent sera donn\u00e9e en classe.<\/p>\n\n\n\n<h3 class=\"wp-block-heading has-vivid-green-cyan-color has-text-color\">Ce projet est disponible sur <a href=\"https:\/\/wokwi.com\/projects\/342629470301061714\">Wokwi<\/a><\/h3>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5.2 &#8211; <span class=\"has-inline-color has-vivid-red-color\">Laboratoire<\/span> (dur\u00e9e 15 minutes)<\/h2>\n\n\n\n<p>\u00c9crire un programme qui d\u00e9bute le clignotement (2 fois\/sec) d&rsquo;une DEL connect\u00e9e \u00e0 D7 lorsque le bouton connect\u00e9 \u00e0 D5 est appuy\u00e9 une fois.<\/p>\n\n\n\n<p>La DEL arr\u00eate de clignoter \u00e0 la prochaine utilisation du bouton.<\/p>\n\n\n\n<p>Le tout en boucle infinie.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-text-align-left\">6 &#8211; D\u00e9tection des bonds d&rsquo;\u00e9tat d&rsquo;un interrupteur (<span class=\"has-inline-color has-vivid-cyan-blue-color\">debounce<\/span>)<\/h2>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Note<\/mark><\/strong>: Ce projet vient des exemples disponibles dans l&rsquo;IDE Arduino. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Debounce\n\n  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button\n  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a\n  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).\n\n  The circuit:\n  - LED attached from pin 13 to ground\n  - pushbutton attached from pin 2 to +5V\n  - 10 kilohm resistor attached from pin 2 to ground\n\n  - Note: On most Arduino boards, there is already an LED on the board connected\n    to pin 13, so you don't need any extra components for this example.\n\n  created 21 Nov 2006\n  by David A. Mellis\n  modified 30 Aug 2011\n  by Limor Fried\n  modified 28 Dec 2012\n  by Mike Walters\n  modified 30 Aug 2016\n  by Arturo Guadalupi\n\n  This example code is in the public domain.\n\n  http:\/\/www.arduino.cc\/en\/Tutorial\/Debounce\n*\/\n\n\/\/ constants won't change. They're used here to set pin numbers:\nconst int buttonPin = 2;    \/\/ the number of the pushbutton pin\nconst int ledPin = 3;      \/\/ the number of the LED pin\n\n\/\/ Variables will change:\nint ledState = HIGH;         \/\/ the current state of the output pin\nint buttonState;             \/\/ the current reading from the input pin\nint lastButtonState = LOW;   \/\/ the previous reading from the input pin\n\n\/\/ the following variables are unsigned longs because the time, measured in\n\/\/ milliseconds, will quickly become a bigger number than can be stored in an int.\nunsigned long lastDebounceTime = 0;  \/\/ the last time the output pin was toggled\nunsigned long debounceDelay = 50;    \/\/ the debounce time; increase if the output flickers\n\nvoid setup() {\n  pinMode(buttonPin, INPUT);\n  pinMode(ledPin, OUTPUT);\n\n  \/\/ set initial LED state\n  digitalWrite(ledPin, ledState);\n}\n\nvoid loop() {\n  \/\/ read the state of the switch into a local variable:\n  int reading = digitalRead(buttonPin);\n\n  \/\/ check to see if you just pressed the button\n  \/\/ (i.e. the input went from LOW to HIGH), and you've waited long enough\n  \/\/ since the last press to ignore any noise:\n\n  \/\/ If the switch changed, due to noise or pressing:\n  if (reading != lastButtonState) {\n    \/\/ reset the debouncing timer\n    lastDebounceTime = millis();\n  }\n\n  if ((millis() - lastDebounceTime) > debounceDelay) {\n    \/\/ whatever the reading is at, it's been there for longer than the debounce\n    \/\/ delay, so take it as the actual current state:\n\n    \/\/ if the button state has changed:\n    if (reading != buttonState) {\n      buttonState = reading;\n\n      \/\/ only toggle the LED if the new button state is HIGH\n      if (buttonState == HIGH) {\n        ledState = !ledState;\n      }\n    }\n  }\n\n  \/\/ set the LED:\n  digitalWrite(ledPin, ledState);\n\n  \/\/ save the reading. Next time through the loop, it'll be the lastButtonState:\n  lastButtonState = reading;\n}\n\ufeff<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Solution du laboratoire 4.2<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n  Programme:  Exemple d'une lecture GPIO avec PullUP\n  Auteur:     Alain Boudreault\n  Date:       2021.09.13\n  Cible:      420-1C4 - objets connect\u00e9s\n  ------------------------------------------------------------\n  Note: Ne pas connecter le push button pour tester le projet.\n\n        Il faut l'ins\u00e9rer seulement une fois que l'application roule.\n*\/\n\n#define BOUTON        2\n#define DEL           3\n#define ON            1\n#define OFF           0\n#define VITESSE_UART  9600\n#define DELAI         200\n\nvoid setup() {\n  \/\/ Configurer le port s\u00e9rie\n  Serial.begin(VITESSE_UART);\n  Serial.println(\"Debut du programme\\n=========================\\n\");\n  \/\/ Configurer la pin BOUTON en entr\u00e9e et activer la r\u00e9sistance interne de type pull-up.\n  pinMode(BOUTON, INPUT_PULLUP);\n  \/\/ Configurer la pin DEL en sortie\n  pinMode(DEL, OUTPUT);\n}\n\nvoid loop() {\n  \/\/ Lire la valeur de l'interrupteur de type poussoir \n  int sensorVal = digitalRead(BOUTON);\n  \/\/ Afficher son \u00e9tat\n  Serial.println(sensorVal);\n\n  \/\/ Note: Les valeurs des \u00e9tats de l'interrupteur seront invers\u00e9es.\n  \/\/ HIGH si ouvert, et LOW lorsqu'appuy\u00e9. \n  if (sensorVal == HIGH) {\n    digitalWrite(DEL, OFF);\n  } else {\n    digitalWrite(DEL, ON);\n  }\n  delay(DELAI);\n}<\/pre>\n\n\n\n<h6 class=\"wp-block-heading has-text-align-right\">Document r\u00e9dig\u00e9 par Alain Boudreault &#8211; r\u00e9vision 2025.01.19.01<\/h6>\n","protected":false},"excerpt":{"rendered":"<p>Contenu: 1.1 &#8211; La fonction Serial.println(val) L&rsquo;IDE Arduino nous propose une console d&rsquo;affichage qu&rsquo;il est possible d&rsquo;utiliser, via le protocole UART (COM[1..9] sous windows) pour afficher des messages et des \u00e9tats des \u00e9l\u00e9ments d&rsquo;un projet. Comme par exemple, afficher l&rsquo;\u00e9tat ON\/OFF d&rsquo;un interrupteur. Ces messages seront disponibles pendant l&rsquo;\u00e9tape de prototypage du projet. La plaquette [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"ub_ctt_via":"","footnotes":""},"class_list":["post-1068","page","type-page","status-publish","hentry"],"featured_image_src":null,"_links":{"self":[{"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/pages\/1068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/comments?post=1068"}],"version-history":[{"count":74,"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/pages\/1068\/revisions"}],"predecessor-version":[{"id":2743,"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/pages\/1068\/revisions\/2743"}],"wp:attachment":[{"href":"https:\/\/ve2cuy.com\/420-1c4\/index.php\/wp-json\/wp\/v2\/media?parent=1068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}