Ligne 25 : |
Ligne 25 : |
| /* 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 | + | Le circuit: |
− | from the serial monitor. I just created this because I did | + | La Pin 13 est connectée à une résistance de 220-ohm, qui est alors |
− | not find any example. Any suggestions are welcome. | + | connecté à une LED et ensuite à la masse (GND). Comme alternative, |
| + | vous pouvez regarder la LED "L" visible sur la carte Arduino et |
| + | déjà raccordée sur la broche 13. |
| | | |
− | The circuit: | + | Créé le July 21, 2010, par Mark Aosawa. mark.aosawa@yahoo.com |
− | Pin 13 is connected to a 220-ohm resistor, which is then
| + | Modifié en profondeur April 7, 2012 par MC Hobby, info@mchobby.be |
− | connected to a LED and then grounded. As an alternative,
| |
− | you may look at the arduino in-built led in pin 13.
| |
− |
| |
− | created July 21, 2010, by Mark Aosawa. mark.aosawa@yahoo.com
| |
− | Widely Modified April 7, 2012 by 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 60 : |
| 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 84 : |
| } | | } |
| | | |
− | //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 95 : |
| } | | } |
| | | |
− | //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 113 : |
| 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(); |