Advertisement
Garro

Arduino

Jun 27th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.38 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////
  2. //©2011 bildr
  3. //Released under the MIT License - Please reuse change and share
  4. //Simple code for the ADXL335, prints calculated orientation via serial
  5. //////////////////////////////////////////////////////////////////
  6. #include <Servo.h>
  7. Servo myservo;
  8. //////////////////////////////////////////////////////////////////
  9. const int xPin = 0;
  10. const int yPin = 1;
  11. const int zPin = 2;
  12.  
  13. int minValX = 259;
  14. int maxValX = 394;
  15. int minValY = 259;
  16. int maxValY = 392;
  17. int minValZ = 266;
  18. int maxValZ = 396;
  19.  
  20. double c = 0;
  21.  
  22. double x;
  23. double y;
  24. double z;
  25.  
  26. int xRead;
  27. int yRead;
  28. int zRead;
  29. //////////////////////////////////////////////////////////////////
  30. void setup(){
  31.   Serial.begin(9600);
  32.   delay(300);
  33.  
  34.   myservo.attach(9);
  35.  
  36.   xRead = analogRead(xPin);
  37.   yRead = analogRead(yPin);
  38.   zRead = analogRead(zPin);
  39.  
  40.   if(xRead > 280 && xRead < 350){
  41.     minValX = xRead - 64;
  42.     maxValX = xRead + 64;
  43.   }
  44.   if(yRead > 280 && yRead < 350){
  45.     minValY = yRead - 64;
  46.     maxValY = yRead + 64;
  47.   }
  48.   if(zRead > 280 && zRead < 350){
  49.     minValZ = zRead - 64;
  50.     maxValZ = zRead + 64;
  51.   }
  52. }
  53. //////////////////////////////////////////////////////////////////
  54. void loop(){
  55.   xRead = analogRead(xPin);
  56.   yRead = analogRead(yPin);
  57.   zRead = analogRead(zPin);
  58.  
  59.   x = map(xRead, minValX, maxValX, -90, 90);
  60.   y = map(yRead, minValY, maxValY, -90, 90);
  61.   z = map(zRead, minValZ, maxValZ, -90, 90);
  62.  
  63.   printNumber(x,y,z,xRead,yRead,zRead);
  64.   moveMotor(x);
  65. }
  66. //////////////////////////////////////////////////////////////////
  67. void moveMotor(double x){
  68.   delay(20);
  69.   if(x>5){
  70.     myservo.write(95);
  71.     delay(200);
  72.   }else if(x<-5){
  73.     myservo.write(77);
  74.     delay(200);
  75.   }else{
  76.     c = 0;
  77.   }if(c<4){
  78.     c = c + 0.3;
  79.   }myservo.write(86);
  80.   delay(300);
  81. }
  82. //////////////////////////////////////////////////////////////////
  83. void printNumber(double x, double y, double z,int xRead,int yRead,int zRead){
  84.   Serial.println();
  85.   Serial.print("x: ");
  86.   Serial.print((int)x);
  87.   Serial.print("(v");
  88.   Serial.print(xRead);
  89.   Serial.print(") | y: ");
  90.   Serial.print((int)y);
  91.   Serial.print("(v");
  92.   Serial.print(yRead);
  93.   Serial.print(") |z: ");
  94.   Serial.print((int)z);
  95.   Serial.print("(v");
  96.   Serial.print(zRead);
  97.   Serial.print(")");
  98. }
  99. //////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement