Partage
  • Partager sur Facebook
  • Partager sur Twitter

Arduino et Port Serie

Serial.read()

    16 août 2019 à 10:11:21

    Salut les amis, 

    dans le cadre d'un projet je suis en train de faire arduino et rfid, le but est de commander un servo moteur, une et un buzzer une fois que carte est lue, cette carte lue doit etre enregistrer dans une base de données

    pendant les essai tout va bien voici le code

    #include <SPI.h>
    #include <MFRC522.h>
    #include <Servo.h>
    #include <LiquidCrystal.h>
    int data;
    
    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_PIN          10         // Configurable, see typical pin layout above
    #define LED_VERT         4          // Configurable, see typical pin layout above
    #define LED_ROUGE        5         // Configurable, see typical pin layout above
    #define BUZZER           2          // Configurable, see typical pin layout above
    LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
    MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
    Servo myServo;//define // define serov name
    
    void setup()
    {
    
      lcd.begin(16,2);
      lcd.clear();
      lcd.setCursor (0,0);
      lcd.print("SCANNER LA CARTE");
    	Serial.begin(9600);		// Initialize serial communications with the PC  
    	SPI.begin();			// Init SPI bus
    	mfrc522.PCD_Init();		// Init MFRC522
      myServo.attach(3); // servo pin
      myServo.write(0);//servo start position
      pinMode(LED_VERT,OUTPUT);
      pinMode(LED_ROUGE,OUTPUT);
      pinMode(BUZZER,OUTPUT);
      noTone(BUZZER);
     Serial.println("Approcher votre Carte pour le scan");
     Serial.println();
    }
    
    void loop() 
    {
    	// Look for new cards
    	if ( ! mfrc522.PICC_IsNewCardPresent())
    	{
    		return;
    	}
    	// Select one of the cards
    	if ( ! mfrc522.PICC_ReadCardSerial()) {
    		return;
    	}
      // SHOW ID SERIAL
      //Serial.println("UID tag : ");
      String content="";  
      byte letter;
      for(byte i=0; i<mfrc522.uid.size;i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
        content.toUpperCase();
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("SCANNER LA CARTE");
        lcd.setCursor(0,1);
        lcd.print(content);
      }
      Serial.println();
      Serial.println("Message : ");
    
       
      if(content.substring(1)=="5B 7B 96 0B")
      {
        Serial.println(content.substring(1));
      
        delay(500);
        digitalWrite(LED_VERT,HIGH);
        
        tone(BUZZER,1500);
        delay(300);
        noTone(BUZZER);
        myServo.write(180);
        delay(5000);
        myServo.write(0);
        digitalWrite(LED_VERT,LOW);
      }
      else
      {
          Serial.println("Acces Refuse");
          digitalWrite(LED_ROUGE,HIGH);
          tone(BUZZER,5000);
          delay(1000);
          digitalWrite(LED_ROUGE,LOW);
          noTone(BUZZER);
          
      }
    }
      


    j'aimerai ajouter cette instruction  pour la lecture via le port serie:

    data = Serial.read();
            
      if (Serial.available())
      {  
         data = Serial.read();  
         if (data == 'A') 
         {  
            Serial.print("OK");  
         } else {  
            digitalWrite(13, LOW);  
         }  
      }

    pour avoir un code qui ressemble à ça :

    #include <SPI.h>
    #include <MFRC522.h>
    #include <Servo.h>
    #include <LiquidCrystal.h>
    int data;
    
    #define RST_PIN         9          // Configurable, see typical pin layout above
    #define SS_PIN          10         // Configurable, see typical pin layout above
    #define LED_VERT         4          // Configurable, see typical pin layout above
    #define LED_ROUGE        5         // Configurable, see typical pin layout above
    #define BUZZER           2          // Configurable, see typical pin layout above
    LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
    MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
    Servo myServo;//define // define serov name
    
    void setup()
    {
    
      lcd.begin(16,2);
      lcd.clear();
      lcd.setCursor (0,0);
      lcd.print("SCANNER LA CARTE");
    	Serial.begin(9600);		// Initialize serial communications with the PC  
    	SPI.begin();			// Init SPI bus
    	mfrc522.PCD_Init();		// Init MFRC522
      myServo.attach(3); // servo pin
      myServo.write(0);//servo start position
      pinMode(LED_VERT,OUTPUT);
      pinMode(LED_ROUGE,OUTPUT);
      pinMode(BUZZER,OUTPUT);
      noTone(BUZZER);
     Serial.println("Approcher votre Carte pour le scan");
     Serial.println();
    }
    
    void loop() 
    {
    	// Look for new cards
    	if ( ! mfrc522.PICC_IsNewCardPresent())
    	{
    		return;
    	}
    	// Select one of the cards
    	if ( ! mfrc522.PICC_ReadCardSerial()) {
    		return;
    	}
      // SHOW ID SERIAL
      //Serial.println("UID tag : ");
      String content="";  
      byte letter;
      for(byte i=0; i<mfrc522.uid.size;i++)
      {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
        content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
        content.concat(String(mfrc522.uid.uidByte[i], HEX));
        content.toUpperCase();
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("SCANNER LA CARTE");
        lcd.setCursor(0,1);
        lcd.print(content);
      }
      Serial.println();
      Serial.println("Message : ");
    
       data = Serial.read();
            
      if (Serial.available())
      {
       if(data == 'A')
        {
        Serial.println(content.substring(1));
      
        delay(500);
        digitalWrite(LED_VERT,HIGH);
        
        tone(BUZZER,1500);
        delay(300);
        noTone(BUZZER);
        myServo.write(180);
        delay(5000);
        myServo.write(0);
        digitalWrite(LED_VERT,LOW);
      }
      else
      {
          Serial.println("Acces Refuse");
          digitalWrite(LED_ROUGE,HIGH);
          tone(BUZZER,5000);
          delay(1000);
          digitalWrite(LED_ROUGE,LOW);
          noTone(BUZZER);
          
      }
     }
    }
      

    En compilant aucun probleme mais juste si j'essai d'envoyer un caractere via le moniteur serie, rien ne se produit sur les differentes led et le buzzer ne produit aucun sont. si quelqu'un peut avoir une idée pour m'aidé.

    Merci d'avance

    • Partager sur Facebook
    • Partager sur Twitter
      16 août 2019 à 14:13:04

      Hello,

      Normalement ça se fait dans l'autre sens :

      if (Serial.available() > 0) {
         incomingByte = Serial.read();
      }
      Dans ton cas, si tu envoie juste un 'A', tu le lis d'abord, donc il n'y a plus rien à lire dans le buffer du serial, et donc Serial.available() devient faux.

      • Partager sur Facebook
      • Partager sur Twitter
        17 août 2019 à 8:18:58

        Salut Arkturus, 

        je viens de changer la comme tu l'as dis, mais rien toujours, j'ai d'ailleurs ajouté un Serialprint(data) juste apres la lecture pour voir si la lecture Serie est faite mais rien toujours, Voici le code :

        #include <SPI.h>
        #include <MFRC522.h>
        #include <Servo.h>
        #include <LiquidCrystal.h>
        int data;
         
        #define RST_PIN         9          // Configurable, see typical pin layout above
        #define SS_PIN          10         // Configurable, see typical pin layout above
        #define LED_VERT         4          // Configurable, see typical pin layout above
        #define LED_ROUGE        5         // Configurable, see typical pin layout above
        #define BUZZER           2          // Configurable, see typical pin layout above
        LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
        MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
        Servo myServo;//define // define serov name
         
        void setup()
        {
         
          lcd.begin(16,2);
          lcd.clear();
          lcd.setCursor (0,0);
          lcd.print("SCANNER LA CARTE");
            Serial.begin(9600);     // Initialize serial communications with the PC 
            SPI.begin();            // Init SPI bus
            mfrc522.PCD_Init();     // Init MFRC522
          myServo.attach(3); // servo pin
          myServo.write(0);//servo start position
          pinMode(LED_VERT,OUTPUT);
          pinMode(LED_ROUGE,OUTPUT);
          pinMode(BUZZER,OUTPUT);
          noTone(BUZZER);
         Serial.println("Approcher votre Carte pour le scan");
         Serial.println();
        }
         
        void loop()
        {
            // Look for new cards
            if ( ! mfrc522.PICC_IsNewCardPresent())
            {
                return;
            }
            // Select one of the cards
            if ( ! mfrc522.PICC_ReadCardSerial()) {
                return;
            }
          // SHOW ID SERIAL
          //Serial.println("UID tag : ");
          String content=""; 
          byte letter;
          for(byte i=0; i<mfrc522.uid.size;i++)
          {
            Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
            Serial.print(mfrc522.uid.uidByte[i], HEX);
            content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
            content.concat(String(mfrc522.uid.uidByte[i], HEX));
            content.toUpperCase();
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("SCANNER LA CARTE");
            lcd.setCursor(0,1);
            lcd.print(content);
          }
          Serial.println();
          Serial.println("Message : ");
         
          
               
          if (Serial.available())
          {
           data = Serial.read();
           Serial.print(data);
           
           if(data == 'A')
            {
            Serial.println(content.substring(1));
           
            delay(500);
            digitalWrite(LED_VERT,HIGH);
             
            tone(BUZZER,1500);
            delay(300);
            noTone(BUZZER);
            myServo.write(180);
            delay(5000);
            myServo.write(0);
            digitalWrite(LED_VERT,LOW);
          }
          else
          {
              Serial.println("Acces Refuse");
              digitalWrite(LED_ROUGE,HIGH);
              tone(BUZZER,5000);
              delay(1000);
              digitalWrite(LED_ROUGE,LOW);
              noTone(BUZZER);
               
          }
         }
        }
        



        • Partager sur Facebook
        • Partager sur Twitter

        Arduino et Port Serie

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown