Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312164
  • 博文数量: 27
  • 博客积分: 758
  • 博客等级: 军士长
  • 技术积分: 369
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 23:10
文章分类

全部博文(27)

文章存档

2014年(1)

2012年(26)

我的朋友

分类: 嵌入式

2012-05-29 00:32:47


  1. #include <Servo.h>

  2. #define SERVO1_DPIN 8
  3. #define SERVO2_DPIN 9
  4. #define JOYSTICK_KEY_DPIN 7
  5. #define JOYSTICK_X_APIN A0
  6. #define JOYSTICK_Y_APIN A1


  7. Servo servo1,servo2;
  8. int oriX,oriY;
  9. int minX,maxX,minY,maxY;
  10. int stepX, stepY; // servo motor increment in degrees
  11. int thresholdX, thresholdY;
  12. bool servo_on;


  13. void setup()
  14. {
  15.     servo_on=true;
  16.     stepX=1;
  17.     stepY=1;
  18.     oriX = 0;
  19.     oriY = 0;
  20.     thresholdX=10;
  21.     thresholdY=10;
  22.     
  23.     Serial.begin(9600);
  24.     pinMode(JOYSTICK_KEY_DPIN, INPUT);
  25.     servo1.attach(SERVO1_DPIN);
  26.     servo2.attach(SERVO2_DPIN);
  27.     servo1.write(90);
  28.     servo2.write(90);
  29. }

  30. void loop()
  31. {
  32.       int X,Y;
  33.       float r;
  34.       int deg;
  35.       // ------------------------------------------
  36.       // check joystick key
  37.       // ------------------------------------------
  38.       int key_down = digitalRead(JOYSTICK_KEY_DPIN);
  39.       if(key_down == 0)
  40.       {
  41.           delay(100);
  42.           key_down = digitalRead(JOYSTICK_KEY_DPIN);
  43.           if(key_down==0){
  44.               servo_on=!servo_on;
  45.               Serial.println("Joystick KEY pressed");
  46.           }
  47.       }
  48.       
  49.      // ------------------------------------------
  50.      // check joystick X value
  51.      // ------------------------------------------
  52.      int sensorValue = analogRead(JOYSTICK_X_APIN);
  53.      if(oriX == 0)
  54.      {
  55.          oriX = sensorValue;
  56.          minX=0-oriX;
  57.          maxX=1023-oriX;
  58.          Serial.print("oriX = ");
  59.          Serial.println(oriX);
  60.      }
  61.     
  62.      X=sensorValue - oriX;
  63.      Serial.print("X = ");
  64.      Serial.println(X);
  65.      
  66.      if(servo_on){
  67.          deg=servo1.read();
  68.          if(X>thresholdX){
  69.              r=(float)X/(float)maxX;
  70.              servo1.write(deg+stepX);
  71.              delay(15);
  72.          }else if(X<-thresholdX){
  73.              r=(float)X/(float)minX;
  74.              servo1.write(deg-stepX);
  75.              delay(15);
  76.          }
  77.      }
  78.      
  79.      // ------------------------------------------
  80.      // check joystick Y value
  81.      // ------------------------------------------
  82.      sensorValue = analogRead(JOYSTICK_Y_APIN);
  83.      if(oriY == 0)
  84.      {
  85.          oriY = sensorValue;
  86.          minY=0-oriY;
  87.          maxY=1023-oriY;
  88.          Serial.print("oriY = ");
  89.          Serial.println(oriY);
  90.      }
  91.      
  92.      Y=sensorValue - oriY;
  93.      Serial.print("Y = ");
  94.      Serial.println(Y);
  95.     
  96.      if(servo_on){
  97.          deg=servo2.read();
  98.          if(Y>thresholdY){
  99.              r=(float)Y/(float)maxY;
  100.              servo2.write(deg+stepY);
  101.              delay(15);
  102.          }else if(Y<-thresholdY){
  103.              r=(float)Y/(float)minY;
  104.              servo2.write(deg-stepY);
  105.              delay(15);
  106.          }
  107.      }
  108.  
  109. }

阅读(1145) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~