Étape 13 : Code Arduino, partie 1
Définir certaines broches sur l’Arduino :
broches numériques
const int LEDPIN=13; // light up when audio detected const int PIN_STROBE=4; // spectrum shield const int PIN_RESET=5; // spectrum shield const int PIN_MOTOR_L=3; //PWM to motor open mouth const int PIN_MOTOR_R=6; //PWM to motor close mouth const int PIN_MOTOR_SLEEP=7; //sleep fxn on motor board const int PIN_MOTOR_STALL=8; //stall warning on motor board
//analog pins const int PIN_LEFT=0; // L analog from spectrum shield const int PIN_RIGHT=1; // R analog from spectrum shield const int PIN_MOTOR_POT=3; //analog potentiometer on motor
Il s’avère que vous n’avez vraiment pas besoin de « fermer » la bouche avec le moteur-la boîte de vitesses apparaît pour se détendre à la position fermée par lui-même. Cela rend contrôle des mouvements encore plus facile puisque nous avons seulement besoin de « ouvrir » la bouche, c'est-à-dire, laisser tourner le moteur dans un sens. Pourtant, pourrait aussi bien fil it up et programme pour cela juste au cas où.
Nous avons besoin de mettre en place deux tableaux s’accrocher le signal de chacun des sept bandes canaux L et R
// spectrum shield band arrays int left[7]; int right[7];
Définissez le min et max pour le mouvement de la mâchoire :
int minRotation = 400; //approximate reading when mouth closed int maxRotation = 600; //approximate reading when mouth open
Initialiser :
void setup() {<br> pinMode(LEDPIN, OUTPUT); // LED //initialize spectrum board related pins pinMode(PIN_RESET, OUTPUT); // reset pinMode(PIN_STROBE, OUTPUT); // strobe digitalWrite(PIN_RESET,LOW); // reset low digitalWrite(PIN_STROBE,HIGH); //pin 5 is RESET on the shield }
L’analyseur de spectre, utilisation appel lire ce qui suit :
void readMSGEQ7()<br>{ //reset the data digitalWrite(PIN_RESET, HIGH); digitalWrite(PIN_RESET, LOW); //loop thru all 7 bands int sumRight = 0; int sumLeft = 0; for(int band=0; band < 7; band++) { digitalWrite(PIN_STROBE,LOW); // go to the next band delayMicroseconds(20); //gather some data left[band] = analogRead(PIN_LEFT); // store left band reading //right[band] = left[band]; //use this only for MONO! right[band] = analogRead(PIN_RIGHT); // store right band reading digitalWrite(PIN_STROBE,HIGH); // reset the strobe pin sumRight = sumRight+right[band]; // get the sum from all bands sumLeft = sumLeft+left[band]; // get the sum from all bands if(left[band] > 100 || right[band] > 100) { digitalWrite(LEDPIN,HIGH); // sound detected } else { digitalWrite(LEDPIN,LOW); // reset low } } }
Cette routine crée un retard de 7 x 20 msec (140 millisecondes). Il suffit de dire. Lorsque vous exécutez des moteurs, il est toujours bon à retenir tout retard dur (puisque vous n’aurez aucun contrôle du moteur tout en retard actif, qu'il pourrait être en cours d’exécution à pleine vitesse).
Après avoir appelé readMSGEQ7, nous pouvons fixer le moteur à une position fondée sur le signal audio. Dans cet exemple, j’utilise le signal total de 7 pour toutes les bandes, mais seul le canal gauche (donc le mouvement de la bouche ne sera pas affecté par audio sur le canal de droite). Le signal max de chaque canal est 1024, donc la somme max théorique est de 7 chaînes x 1024 = 7168. Pratiquement le max n’obtiendra pas plus de 5000-6000. Vous pourriez obtenir un signal d’entrée moyens et la valeur min/max de celle, mais je préfère quelque chose de plus simple. Tout d’abord j’ai obligera la somme de « sumLeft »
if (sumLeft > 1024) {sumLeft = 1024}; //constrain to 1024 max
Et puis j’ai carte sumLeft pour le min et max de la position de la mâchoire. Simple.
int mouthPosition = map(sumLeft, 0, 1024, minRotation, maxRotation);
Mettre quelques limites pour la position de la bouche comme cible haute et cible faible (cela empêche le moteur de tenter d’atteindre la position exacte et permet au contraire une gamme (+/-"posError").
targetH = target_mouthPosition + posError;<br>targetL = target_mouthPosition - posError;