Étape 4: Contrôleur (code de l’Arduino)
Pour utiliser l’accéléromètre, vous devez la bibliothèque HMC5883L
Pour la communication infrarouge, vous devez utiliser cette bibliothèque
J’ai conçu mon code pour quelques fonctions :
flotteur getRotation (char x) :
Cette fonction get la rotation de l’accéléromètre et la variable X définissent axe de sorcière de revenir.
float getRotation(char x) {float heading; sensors_event_t event; mag.getEvent(&event); //read the data from the accelerometer if (x=='x'){heading = atan2(event.magnetic.x, event.magnetic.y);} else if (x=='y'){heading = atan2(event.magnetic.y, event.magnetic.z);} else if (x=='z'){heading = atan2(event.magnetic.z, event.magnetic.x);} return heading * 180/M_PI; }
bool Verify (flotteur courant [3], origine de flotteur [4] [3], octets par ligne) :
Cette fonction vérifier si l’accéléromètre est dans une position spécifique en comparant les données lues stockées dans le tableau « courant [3]"aux données du tableau « l’origine » [4] [3] ce tableau possède 4 rangs à chaque point (dans nos dessins nous avons seulement 4 points) et la variable « row » définissez la ligne sorcière à utiliser.
bool Verify(float current[3], float origin_[4][3],int row){ byte toler=37;//this variable set the tolerance to respect //return True if the next conditions are true return origin_[row][0]-toler<=current[0] && current[0]<origin_[row][0]+toler && origin_[row][1]-toler<=current[1] && current[1]<origin_[row][1]+toler && origin_[row][2]-toler<=current[2] && current[2]<origin_[row][2]+toler ;}
getPoint octet (flotteur origine [4] [3]) :
Cette fonctions combinent les deux dernières fonctions et retournent la position de l’accéléromètre :
(Droit (retour 2), Down (retour 3), Up(return 1), Left(return 4))
byte getPoint (float origin__[4][3] ){ byte return_=0; float current[3]={getRotation('x'), getRotation('y'), getRotation('z')}; if(Verify(current, origin__,0)){return_=1;} else if(Verify(current, origin__,1)){return_=2;} else if(Verify(current, origin__,2)){return_=3;} else if(Verify(current, origin__,3)){return_=4;} return return_; }
Boolean Compare (byte Mdrawing [4] [4], octets par ligne) :
Cette fonction obtenir les points successifs et voir si elles sont dans l’ordre des points stockés dans 'Mdrawing[4] [4]' il y a 4 raws dans ce tableau qui dépend de la quantité de dessins vous avez déclaré, en cela, nous avons juste 4 dessins
bool Compare (byte Mdrawing[4][4],byte row){ byte Cpoint=getPoint( origin); //read the current point of the accelerometer(origin is a global variable ,it's the rotations of each point ) byte Ppoint=0;// this variable will stock previous points byte y=0; byte x=0; while(Mdrawing[row][x]!=0 && x<4){x++;}//get the number of columns that the value is different than 0 while(y<x){ Cpoint = getPoint( origin);//read the current point if (Cpoint==Mdrawing[row][y] && Cpoint!=0){Ppoint=Cpoint;//if the current point is in the same order of the point in 'Mdrawing' while (Ppoint==Cpoint && y!=x-1){Cpoint=getPoint(origin);}// wait that the current point change y++;}//pass to the next point else if(Cpoint!=Mdrawing[row][y] && Cpoint!=0){return 0;}}//if the current point is different than the point in 'Mdrawing' return 1;}
origine de flotteur [4] [3] :
ce tableau contient la rotation de chaque points, vous pouvez la définir avec le fichier de calibration. Il ressemble à :
float origin[4][3]= {{71.93,149.56,-29.04},//up point {5.97,126.75,-82.03},//right point {-72.14,162.31,-134.71},//down point {97.64,-174.11,-52.44}};//left point
Byte Mdrawing [4] [4] :
ce tableau contient l’ordre que les points devraient faire un dessin précis, par exemple:(picture 1)