Partage
  • Partager sur Facebook
  • Partager sur Twitter

Aide Robot Arduino

    23 mai 2019 à 18:28:56

    Bonsoir,

    Voici un programme qui consiste à contrôler un robot avec une télécommande via bluetooth (une application depuis un smartphone) :

    #include <AFMotor.h>  
    
    AF_DCMotor motor3 (3, MOTOR34_1KHZ); 
    AF_DCMotor motor4 (4, MOTOR34_1KHZ); 
    
    String readString;
    
    void setup() {
      //set all the motor control pins to output
    
      Serial.begin(9600);
    }
    
    void loop() 
    {
      while(Serial.available())
      {
        delay(50);
        char c = Serial.read();
        readString+=c;
      }
    
      if(readString.length() > 0) 
      {
    
        if(readString == "FORWARD")
        {
          Avancer();
        }
        
        if(readString == "BACK")
        {
          Reculer();
        }
    
        if(readString == "LEFT")
        {
          Gauche();
        }
    
        if(readString == "RIGHT")
        {
          Droite();
        }
    
        if(readString == "STOP")
        {
          Stop();
        }
    
        readString = "";
      }
    
    }
    
      
    
    void Avancer()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(700);
      motor4.run(FORWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Reculer()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(700);
      motor4.run(BACKWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Droite()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(190);
      motor4.run(BACKWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Gauche()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(190);
      motor4.run(FORWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
    
    void Stop()
    {
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }

    et voici un autre programme permettant de contrôler le robot par vocal via bluetooth (depuis une application aussi) :


    #include <AFMotor.h>  
    
    AF_DCMotor motor3 (3, MOTOR34_1KHZ); 
    AF_DCMotor motor4 (4, MOTOR34_1KHZ); 
    
    String readString;
    String voice;
    
    void setup() {
      //set all the motor control pins to output
    
      Serial.begin(9600);
    }
    
    void loop() 
    {
       while (Serial.available())
      { 
        delay(10); 
        char v = Serial.read(); 
        if (v == '#') 
        {
          break;
        } 
        voice += v; 
      }
      if (voice.length() > 0){
        if(voice == "*avancer"){
          Avancer();
          }
        else if(voice == "*reculer"){
          Reculer();
          }
        else if(voice == "*droite") {
          Droite();
        }
        else if(voice == "*gauche") {
          Gauche();
        }
        else if(voice == "*stop") {
          Stop();
        }
        
      voice=""; 
      }
      
    }
    
      
    
    void Avancer()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(700);
      motor4.run(FORWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Reculer()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(700);
      motor4.run(BACKWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Droite()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(190);
      motor4.run(BACKWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Gauche()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(190);
      motor4.run(FORWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
    
    void Stop()
    {
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }


    les deux marchent normalement (séparemment), mais quand j'essaie de combiner les deux programmes (pour que l'utilisateur aie le choix du contrôle) ça ne marche pas :

    #include <AFMotor.h>  
    
    AF_DCMotor motor3 (3, MOTOR34_1KHZ); 
    AF_DCMotor motor4 (4, MOTOR34_1KHZ); 
    
    String readString;
    String voice;
    
    void setup() {
      //set all the motor control pins to output
    
      Serial.begin(9600);
    }
    
    void loop() 
    {
      while(Serial.available())
      {
        delay(50);
        char c = Serial.read();
        readString+=c;
      }
    
      if(readString.length() > 0) 
      {
    
        if(readString == "FORWARD")
        {
          Avancer();
        }
        
        if(readString == "BACK")
        {
          Reculer();
        }
    
        if(readString == "LEFT")
        {
          Gauche();
        }
    
        if(readString == "RIGHT")
        {
          Droite();
        }
    
        if(readString == "STOP")
        {
          Stop();
        }
    
        readString = "";
      }
    
      while (Serial.available())
      { 
        delay(10); 
        char v = Serial.read(); 
        if (v == '#') 
        {
          break;
        } 
        voice += v; 
      }
      if (voice.length() > 0){
        if(voice == "*avancer"){
          Avancer();
          }
        else if(voice == "*reculer"){
          Reculer();
          }
        else if(voice == "*droite") {
          Droite();
        }
        else if(voice == "*gauche") {
          Gauche();
        }
        else if(voice == "*stop") {
          Stop();
        }
        
      voice=""; 
      }
      
    }
    
      
    
    void Avancer()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(700);
      motor4.run(FORWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Reculer()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(700);
      motor4.run(BACKWARD);
      motor4.setSpeed(700);
      delay(2000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Droite()
    {
      motor3.run(FORWARD);
      motor3.setSpeed(190);
      motor4.run(BACKWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
      
    void Gauche()
    {
      motor3.run(BACKWARD);
      motor3.setSpeed(190);
      motor4.run(FORWARD);
      motor4.setSpeed(190);
      delay(1000);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
    
    void Stop()
    {
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }

    Je ne comprends pas où est le problème.


    • Partager sur Facebook
    • Partager sur Twitter

    Aide Robot Arduino

    × 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