Bruiteur moteur avec Arduino Uno

Publié le par le Moujo

Principe

La manette des gaz envoie un signal ; le récepteur décode les signaux et la UNO, connectée à la pin2 en parallèle avec le régulateur moteur, lit ce signal de la radio commande (les gaz) et lit les sons de la carte SD en Wav. (bruitage moteur)

En pratique

L’idée est d’avoir plusieurs sons WAV qui représentent le bruit du moteur à différentes vitesses de rotation.
L’arduino lit les valeurs de la radiocommande entre 0 et 3000
Exemple :

-allumage : wav0 d’allumage des moteurs
-entre 0-50 : wav01: démarrage moteur à vide (ralenti)
-entre 51 et 100 : fichier wav2
-entre 101 et 150 : fichier wav3
-entre 151 et 200 : fichier wav4
-entre 201 et 250 : fichier wav5
Etc… jusqu’à 3000

Shéma

Pour l'IDE Arduino il faut télécharger les 3 bibliothèques suivantes : TMRpcm   SD.h  SPI.h et les installer  en cliquant sur Croquis/inclure une bibliothèque/Ajouter à la bibliothèque.zip

Le sketch : SoundGenv2. A revoir, il y a un souci, (et on sait ce que les soucis sont comme dirait mon boucher), Nano ne lit que les 8 premiers sons sur 16 présents.
A copier dans l'IDE Arduino : si j'arrive à corriger l'erreur !

//**SoundGenV2**lecture de wav avec signal RC**********

  //********** LIBRAIRIES ****************
  #include <SD.h>                      
  #define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins
  #include <TMRpcm.h>       
  #include <SPI.h>
  volatile uint8_t prev; // remembers state of input bits from previous interrupt
  volatile uint32_t risingEdge[6]; // time of last rising edge for each channel
  volatile uint32_t uSec[6]; // the latest measured pulse width for each channel
  int volatile channel[5];
  TMRpcm audio;   // crée un objet audio pour l'utiliser dans ce sketch
 
//********* DEFINITIONS *************************
    typedef struct {
    char * soundfile;
    int  threshold_up;
    int  threshold_down;
    }
    step_type;
    step_type mystep[32];
    char * soundstart;
    char * soundstop;
 //********* INITIALISATION *****************
    //unsigned long time = 0;
    int numLoop = 0;
    //int throttle = 0;
    int prevThrottle = 0;
    int currThrottle = 0;
    //int playingSound = 0;
 
    //RC scale
    int rc_pin = 2;
    unsigned long throttle_level;
    //*********** Niveau de manette des gaz long non signé ***************

    ISR(PCINT2_vect) { // one or more of pins 2~7 have changed state
    uint32_t now = micros();
    uint8_t curr = PIND; // current state of the 6 input bits
    uint8_t changed = curr ^ prev;
    int index = 0;
    for (uint8_t mask = 0x04; mask; mask <<= 1)
    {
    if (changed & mask) { // Si la broche a changé d'état
    if (curr & mask) {
    risingEdge[index] = now;
    }
    else { // stocker la largeur des impulsions
    uSec[index] = now - risingEdge[index];
    channel[index+1]=now - risingEdge[index];
    }
    }
    index++;
    }
    prev = curr;
    }
    //**********SETUP ************
    void setup(){
    pinMode(rc_pin, INPUT);
    PCMSK2 |= 0xFC; // sélectionner le cache pour allouer les 6 pins à générer des interruptions
    PCICR |= 0x04;  // enable interupt for port D
    prevThrottle=0; // manette des gaz au point mort
    audio.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
    soundstart="00start.wav";

     mystep[0].soundfile="00start.wav";
     mystep[0].threshold_down=20;
     mystep[0].threshold_up=50.;
     
     mystep[1].soundfile="0.wav";
     mystep[1].threshold_down=50;
     mystep[1].threshold_up=100;
     
     mystep[2].soundfile="01.wav";
     mystep[2].threshold_down=100;
     mystep[2].threshold_up=150;
     
     mystep[3].soundfile="02.wav";
     mystep[3].threshold_down=150;
     mystep[3].threshold_up=200;
     
     mystep[4].soundfile="03.wav";
     mystep[4].threshold_down=200;
     mystep[4].threshold_up=250;
     
     mystep[5].soundfile="04.wav";
     mystep[5].threshold_down=250;
     mystep[5].threshold_up=300;
     
     mystep[6].soundfile="05.wav";
     mystep[6].threshold_down=300;
     mystep[6].threshold_up=350;
     
     mystep[7].soundfile="06.wav";
     mystep[7].threshold_down=350;
     mystep[7].threshold_up=400;

     mystep[8].soundfile="07.wav";
     mystep[8].threshold_down=400;
     mystep[8].threshold_up=450;

     mystep[9].soundfile="08.wav";
     mystep[9].threshold_down=450;
     mystep[9].threshold_up=500;

     mystep[10].soundfile="09.wav";
     mystep[10].threshold_down=500;
     mystep[10].threshold_up=550;

     mystep[11].soundfile="10.wav";
     mystep[11].threshold_down=550;
     mystep[11].threshold_up=600;

     mystep[12].soundfile="11.wav";
     mystep[12].threshold_down=600;
     mystep[12].threshold_up=650;

     mystep[13].soundfile="12.wav";
     mystep[13].threshold_down=650;
     mystep[13].threshold_up=1000;

     mystep[14].soundfile="13.wav";
     mystep[14].threshold_down=100;
     mystep[14].threshold_up=2500;

     mystep[15].soundfile="14.wav";
     mystep[15].threshold_down=2500;
     mystep[15].threshold_up=3000;
     
     mystep[16].soundfile="15stop.wav";
     mystep[16].threshold_down=0;
     mystep[16].threshold_up=20;
        
    Serial.begin(9600);
    //pinMode(10, OUTPUT);
    // see if the card is present and can be initialized:
    if (!SD.begin(SD_ChipSelectPin))
    {  
    Serial.println("SD fail");  
    return;   // don't do anything more if not
    }
    else
    {   
    Serial.println("SD ok");   
    }
    //PLay init file to tell sound version
    Serial.println(soundstart);
    audio.play(soundstart);
    while(audio.isPlaying()){
    delay(200);
    Serial.println("setup end");
    }}
    void loop(){  
    int i;
  //********** Lire la valeur de la manette des gaz ************
 
  for (i=0;i<=9;i++)
     {
      Serial.print("play ");
      Serial.println(mystep[i].soundfile);
      audio.play(mystep[i].soundfile);
      while(audio.isPlaying())
      {
      delay(200);
      }
      audio.stopPlayback();    
      }   
      throttle_level=channel[1];
      Serial.print("input  ");
      Serial.println(throttle_level);
      //*********** choisir la nouvelle valeur de la manette des gaz ***************
      for (i=0;i<=16;i++)
      {
      if ((throttle_level>mystep[i].threshold_down) && (throttle_level <=mystep[i].threshold_up))
      {
        currThrottle=i;
           Serial.print("play");
      Serial.println(mystep[i].soundfile);
      }
      }
      if (audio.isPlaying() && (prevThrottle!=currThrottle))
      {        
      Serial.println(mystep[currThrottle].soundfile);
      audio.play(mystep[currThrottle].soundfile);
      prevThrottle=currThrottle;
      }  
      if (audio.isPlaying()==0)
      {        
      Serial.println(mystep[currThrottle].soundfile);
      audio.play(mystep[currThrottle].soundfile);
      prevThrottle=currThrottle;
      }
      }    
   ******************* FIN DU PROGRAMME ************************************

Les explications sont ici : https://github.com/TMRh20/TMRpcm/wiki

Le Moujo

Publicité
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article