Advertisement
Guest User

Mouvement detection arduino accelerometres

a guest
Feb 22nd, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. /*
  2. * Accelerometer input
  3. * Takes the input from a lilypad accelerometer on analog pins 0=x, 1=y, 2=z
  4. * Detect the movment of each accelerometer
  5. */
  6.  
  7. int buttonpin = 2;
  8. // pied droit
  9. int xpin = 3; // select the input pin for the sensor
  10. int ypin = 4; // select the input pin for the sensor
  11. int zpin = 5; // select the input pin for the sensor
  12.  
  13. int yval = 0; // variable to store the value coming from the x axis
  14. int yval_prec = 0;
  15. int xval = 0; // variable to store the value coming from the x axis
  16. int xval_prec = 0; // variable to store the value coming from the x axis
  17. int zval = 0; // variable to store the value coming from the x axis
  18. int zval_prec = 0;
  19.  
  20. int zdir=90;  //initial z orientation is "up"
  21.  
  22. //pied gauche
  23. int xpinG = 2;
  24. int ypinG = 1;
  25. int zpinG = 0;
  26.  
  27. int yvalG = 0; // variable to store the value coming from the x axis
  28. int yvalG_prec = 0;
  29. int xvalG = 0; // variable to store the value coming from the x axis
  30. int xvalG_prec = 0;
  31. int zvalG = 0; // variable to store the value coming from the x axis
  32. int zvalG_prec = 0;
  33.  
  34. int initialise = 1;
  35. int delta =20;
  36. int isolement_mouvement_init = 3;
  37. int isolement_mouvement = 3;
  38.  
  39. int marche=0;
  40.  
  41. int normeG=0;
  42. int norme=0;
  43. int normeG_init=0;
  44. int norme_init=0;
  45.  
  46. int delai = 0;
  47. int temps_echantillonage = 80;
  48. int temps_init = 0;
  49. int up=390; int down=290; int mid=(up+down)/2;
  50.  
  51. void setup() {
  52.   Serial.begin(9600);
  53.   pinMode(buttonpin, INPUT);
  54.  
  55.   //Serial.print("normeG_init  =");Serial.println(normeG_init);
  56.  
  57.   isolement_mouvement = isolement_mouvement_init;
  58. }
  59.  
  60. void loop() {
  61.  
  62.   if(marche>=1){
  63.     //Serial.print("marche");
  64.     xval = analogRead(xpin); // read the value from the x axis, 0-1023
  65.     yval = analogRead(ypin); // read the value from the y axis, 0-1023
  66.     zval = analogRead(zpin); // read the value from the z axis, 0-1023
  67.     xvalG = analogRead(xpinG); // read the value from the x axis, 0-1023
  68.     yvalG = analogRead(ypinG); // read the value from the y axis, 0-1023
  69.     zvalG = analogRead(zpinG); // read the value from the z axis, 0-1023
  70.    
  71.     displayvalues();
  72.     delay(temps_echantillonage); // rest a little...
  73.     if(digitalRead(buttonpin)==0){ // si on appuye sur le bouton -> arret
  74.       marche++;
  75.       if (marche>3){
  76.         Serial.print("stop");
  77.         marche=-1;
  78.       }
  79.     }
  80.    
  81.   } else if(marche==0){
  82.     if(digitalRead(buttonpin)==0){// si on appuye sur le bouton -> marche
  83.       marche=1;
  84.       initialisationVariables();
  85.     }
  86.   }
  87.  
  88. }
  89.  
  90. void displayvalues(){
  91. int zorientation; int yorientation; int xorientation;
  92. int zorientationG; int yorientationG; int xorientationG;
  93.  
  94. isolement_mouvement--;
  95. //Serial.print(" : x="); Serial.print(xval);
  96. if (isolement_mouvement <= 0) {
  97.  
  98.   normeG = sqrt(((xvalG_prec-xvalG)*(xvalG_prec-xvalG))+((yvalG_prec-yvalG)*(yvalG_prec-yvalG))+((zvalG_prec-zvalG)*(zvalG_prec-zvalG)));
  99.   norme = sqrt(((xval_prec-xval)*(xval_prec-xval))+((yval_prec-yval)*(yval_prec-yval))+((zval_prec-zval)*(zval_prec-zval)));
  100.   /*Serial.print("norme :");Serial.println(norme);
  101.   Serial.print("normeG :");Serial.println(normeG);*/
  102.  
  103.   if(((normeG>(normeG_init+delta)) || (normeG<(normeG_init-delta)))&&(initialise==1)){
  104.     Serial.print(millis()/1000.0);
  105.     /*Serial.print("Deplacement G (");
  106.     Serial.print(normeG);
  107.     Serial.println(")");*/
  108.     Serial.print(",");
  109.     initialise=0;
  110.   } else if(((norme>(norme_init+delta)) || (norme<(norme_init-delta)))&&(initialise==1)){
  111.     Serial.print(millis()/1000.0);
  112.     /*Serial.print("Deplacement (");
  113.     Serial.print(norme);
  114.     Serial.println(")");*/
  115.     Serial.print(",");
  116.     initialise=0;
  117.   } else {//pour eviter de prendre un déplacement pour plusieurs
  118.     initialise = 1;
  119.   }
  120.  
  121.   isolement_mouvement = isolement_mouvement_init;
  122.  
  123. }
  124.  
  125.  
  126. }
  127.  
  128.  
  129. void initialisationVariables(){
  130.   xval_prec = analogRead(xpin);
  131.   yval_prec = analogRead(ypin);
  132.   zval_prec = analogRead(zpin);
  133.   xvalG_prec = analogRead(xpinG);
  134.   yvalG_prec = analogRead(ypinG);
  135.   zvalG_prec = analogRead(zpinG);
  136.   xval = analogRead(xpin); // read the value from the x axis, 0-1023
  137.   yval = analogRead(ypin); // read the value from the y axis, 0-1023
  138.   zval = analogRead(zpin); // read the value from the z axis, 0-1023
  139.   xvalG = analogRead(xpinG); // read the value from the x axis, 0-1023
  140.   yvalG = analogRead(ypinG); // read the value from the y axis, 0-1023
  141.   zvalG = analogRead(zpinG); // read the value from the z axis, 0-1023
  142.   normeG_init = sqrt(((xvalG_prec-xvalG)*(xvalG_prec-xvalG))+((yvalG_prec-yvalG)*(yvalG_prec-yvalG))+((zvalG_prec-zvalG)*(zvalG_prec-zvalG)));
  143.   norme_init = sqrt(((xval_prec-xval)*(xval_prec-xval))+((yval_prec-yval)*(yval_prec-yval))+((zval_prec-zval)*(zval_prec-zval)));
  144.  
  145.   Serial.print("[");
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement