Ligne 4 : |
Ligne 4 : |
| Le but est d'attendre que l'utilisateur entre un mot clé ("on" ou "off") sur le Moniteur Série d'Arduino et d'allumer la LED de la Pin 13 (visible sur la carte) en fonction de l'ordre reçu. | | Le but est d'attendre que l'utilisateur entre un mot clé ("on" ou "off") sur le Moniteur Série d'Arduino et d'allumer la LED de la Pin 13 (visible sur la carte) en fonction de l'ordre reçu. |
| | | |
− | Cet exemple vraiment rudimentaire sera par la suite utilisé comme fondation dans autre exemple du GPRS shield. | + | [[Fichier:SimpleLedPin13.png]] |
| + | |
| + | Cet exemple vraiment rudimentaire sera par la suite utilisé comme fondation dans autre exemple du GPRS shield (réception d'une commande par SMS ;-) ). |
| | | |
| == Principe de fonctionnement == | | == Principe de fonctionnement == |
Ligne 25 : |
Ligne 27 : |
| /* String Command In Serial Port using String | | /* String Command In Serial Port using String |
| | | |
− | The program waits for a command from the serial monitor. | + | Le programme attend qu'une commande arrive sur le moniteur série. |
− | Make sure than IDE Serial Monitor is configured on "Carriage Return" | + | Assurez vous que le moniteur série d'Arduino soit bien configuré sur "Carriage Return" |
− | Entering 'on' will turn the led connected to pin 13. | + | Entrer 'on' allume la LED connectée sur la pin 13. |
− | Entering 'off' will turn the led 13. | + | Entrer 'off' éteint la LED connectée sur la pin 13. |
− | This provides an alternative for Bitlash. | + | Cela fournit une alternative à Bitlash. |
− |
| |
− | This is not the best and only way to get a string command
| |
− | from the serial monitor. I just created this because I did
| |
− | not find any example. Any suggestions are welcome.
| |
| | | |
− | The circuit: | + | Le circuit: |
− | Pin 13 is connected to a 220-ohm resistor, which is then | + | La Pin 13 est connectée à une résistance de 220-ohm, qui est alors |
− | connected to a LED and then grounded. As an alternative, | + | connecté à une LED et ensuite à la masse (GND). Comme alternative, |
− | you may look at the arduino in-built led in pin 13. | + | vous pouvez regarder la LED "L" visible sur la carte Arduino et |
| + | déjà raccordée sur la broche 13. |
| | | |
− | created July 21, 2010, by Mark Aosawa. mark.aosawa@yahoo.com | + | Créé le July 21, 2010, par Mark Aosawa. mark.aosawa@yahoo.com |
− | Widely Modified April 7, 2012 by MC Hobby, info@mchobby.be | + | Modifié en profondeur April 7, 2012 par MC Hobby, info@mchobby.be |
| | | |
| */ | | */ |
| | | |
− | //String buffer | + | //String buffer/mémoire tampon |
| String cmd = String(""); | | String cmd = String(""); |
| | | |
− | // initialize the LED pin 2 as output | + | // Initialiser la LED de la pin 13 comme sortie (OUTPUT) |
− | // as an alternative, you may use int ledPin=13 if your
| |
− | // arduino has a built-in led (use the blink example to check
| |
| int ledPin = 13; | | int ledPin = 13; |
| | | |
| void setup() { | | void setup() { |
− | // initialize serial communication (and baud rate of 9600): | + | // Initialise la communication série (et baud rate à 9600): |
| Serial.begin(9600); | | Serial.begin(9600); |
| | | |
| pinMode(ledPin, OUTPUT); | | pinMode(ledPin, OUTPUT); |
− | //prints a message when the serial monitor is opened | + | // Affiche un message quand le port série est ouvert. |
− | //to check if the program is working | + | // Permet de vérifier que le programme est bien en court de fonctionnement |
| Serial.println("Serial communication is ready."); | | Serial.println("Serial communication is ready."); |
| } | | } |
Ligne 65 : |
Ligne 62 : |
| void loop() { | | void loop() { |
| | | |
− | //check if serial communication is available | + | // Vérifier si des caractères sont reçu sur le port série |
| if (Serial.available() > 0) { | | if (Serial.available() > 0) { |
| | | |
− | //temporary variable to hold the current character | + | // Variable temporaire pour copier le caractère lu |
| char SerialInByte; | | char SerialInByte; |
− | //read the data | + | // Lire un caractère (un Byte) |
| SerialInByte = Serial.read(); | | SerialInByte = Serial.read(); |
| | | |
| + | // Si c'est <CR> --> traiter la commande |
| if(SerialInByte==13){ | | if(SerialInByte==13){ |
| ProcessCmd(); | | ProcessCmd(); |
| }else{ | | }else{ |
− | //store the current character in the string buffer, cmd | + | // sinon, ajouter le caractère à la mémoire tampon (cmd) |
| cmd += String(SerialInByte); | | cmd += String(SerialInByte); |
| + | |
| + | // et afficher un message de déboggage |
| Serial.print( "SerialInByte: " ); | | Serial.print( "SerialInByte: " ); |
| Serial.print( SerialInByte, DEC ); | | Serial.print( SerialInByte, DEC ); |
Ligne 86 : |
Ligne 86 : |
| } | | } |
| | | |
− | //echoes the command typed | + | // Renvoyer la commande (contenu du buffer) sur le port série |
| void printCmd(){ | | void printCmd(){ |
| Serial.println(cmd); | | Serial.println(cmd); |
| } | | } |
| | | |
− | //resets the string buffer and the counter | + | // Réinitialiser le buffer (et vider le port série) |
| void clearCmd(){ | | void clearCmd(){ |
| cmd=""; | | cmd=""; |
Ligne 97 : |
Ligne 97 : |
| } | | } |
| | | |
− | //command interpreter | + | // Interpréter la commande reçue |
| + | // |
| void ProcessCmd() { | | void ProcessCmd() { |
| + | // Un petit message de débogage. La commande est entourée avec [] pour clairement |
| + | // identifier chacun des caractères qui la compose (même les espaces). |
| Serial.print( "ProcessCmd for [" ); | | Serial.print( "ProcessCmd for [" ); |
| Serial.print( cmd ); | | Serial.print( cmd ); |
Ligne 112 : |
Ligne 115 : |
| clearCmd(); | | clearCmd(); |
| }else{ | | }else{ |
− | //any other command is ignored | + | // toutes les autres commandes sont ignorées. |
| Serial.println("unknown command!"); | | Serial.println("unknown command!"); |
| clearCmd(); | | clearCmd(); |
Ligne 118 : |
Ligne 121 : |
| } | | } |
| </nowiki> | | </nowiki> |
| + | |
| + | == Résultat == |
| + | La LED 13 s'allume et s'éteint lorsque la commande "on" ou "off" est reçue. |
| + | |
| + | Outre cette modification de la Pin 13, les messages suivants sont également visible sur le moniteur série. |
| + | |
| + | La séquence de commandes (chacune terminée par la touche Entrée) utilisé dans cet exemple est: |
| + | # on |
| + | # off |
| + | # test |
| + | <nowiki> |
| + | Serial communication is ready. |
| + | SerialInByte: 111, Buffer o |
| + | SerialInByte: 110, Buffer on |
| + | ProcessCmd for [on] |
| + | on |
| + | SerialInByte: 111, Buffer o |
| + | SerialInByte: 102, Buffer of |
| + | SerialInByte: 102, Buffer off |
| + | ProcessCmd for [off] |
| + | off |
| + | SerialInByte: 116, Buffer t |
| + | SerialInByte: 101, Buffer te |
| + | SerialInByte: 115, Buffer tes |
| + | SerialInByte: 116, Buffer test |
| + | ProcessCmd for [test] |
| + | unknown command! |
| + | </nowiki> |
| + | |
| + | {{MCH-Accord}} |