Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7112885
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类:

2012-06-12 08:18:10


  1. #include "Servo.h"
  2. #include "Arduino.h"
  3. #include "SoftwareSerial.h"

  4. #define MAX_BUF_LEN 60 //max input/output buffer length
  5. #define SERVO1_DPIN 8 //1st servo pin
  6. #define SERVO2_DPIN 9 //2nd servo pin
  7. #define TP 6 //Trig_pin
  8. #define EP 7 //Echo_pin

  9. Servo servo1,servo2;
  10. char scan_mode1, scan_mode2;
  11. int inc1, inc2;
  12. int dir1, dir2;
  13.    
  14. void setup(){
  15.   pinMode(TP,OUTPUT); // set TP output for trigger
  16.   pinMode(EP,INPUT); // set EP input for echo
  17.   
  18.   servo1.attach(SERVO1_DPIN);
  19.   servo2.attach(SERVO2_DPIN);
  20.   servo1.write(90);
  21.   servo2.write(110);
  22.   
  23.   Serial.begin(38400); // init serial 9600
  24.   
  25.   scan_mode1='0';
  26.   scan_mode1='0';
  27.   dir1=1;
  28.   dir2=1;
  29.   inc1=0;
  30.   inc2=0;
  31. }

  32. void loop(){
  33.     int i,n;
  34.     char buf[MAX_BUF_LEN] = {0};
  35.     int degree1, degree2;
  36.     int inc;
  37.     
  38.     if(Serial.available()){
  39.         i=0;
  40.         do{
  41.             buf[i++] = Serial.read();
  42.             delay(2);
  43.         }while(Serial.available() && i<MAX_BUF_LEN);
  44.         
  45.         n=i;
  46.         for(i=0;i<n;i+=4){
  47.             degree1=servo1.read();
  48.             degree2=servo2.read();
  49.             
  50.             // get the degree increment from serial input
  51.             inc=buf[i+2]-'0';
  52.             inc=10*inc+buf[i+3]-'0';
  53.  
  54.             // get the inc sign: positve or negative
  55.             if(buf[i+1]=='-')inc=-inc;
  56.             
  57.    
  58.             if(buf[i]=='x'){
  59.                 servo1.write(degree1+inc);
  60.                 scan_mode1='0';
  61.             }else if(buf[i]=='y'){
  62.                 servo2.write(degree2+inc);
  63.                 scan_mode2='0';
  64.             }else if(buf[i]=='r'){
  65.                 // reset the servos
  66.                 servo1.write(90);
  67.                 servo2.write(110);
  68.                 scan_mode1='0';
  69.                 scan_mode2='0';
  70.                 inc1=0;
  71.                 inc2=0;
  72.             }else if(buf[i]=='s' && buf[i+1]=='x'){
  73.                 scan_mode1='x';
  74.                 inc1=inc;
  75.             }else if(buf[i]=='s' && buf[i+1]=='y'){
  76.                 scan_mode2='y';
  77.                 inc2=inc;
  78.             }
  79.             
  80.         }
  81.     }
  82.   
  83.     if(scan_mode1=='x'){
  84.       degree1=servo1.read();
  85.       if(degree1+dir1*inc1>=180)
  86.       {
  87.         dir1=-1;
  88.         if(scan_mode2=='y'){
  89.           degree2=servo2.read();
  90.           if(degree2+dir2*inc2>=180) dir2=-1;
  91.           else if(degree2+dir2*inc2<=0) dir2=1;
  92.           servo2.write((degree2+dir2*inc2)%180);
  93.         }
  94.       }
  95.       else if(degree1+dir1*inc1<=0){
  96.         dir1=1;
  97.         if(scan_mode2=='y'){
  98.           degree2=servo2.read();
  99.           if(degree2+dir2*inc2>=180) dir2=-1;
  100.           else if(degree2+dir2*inc2<=0) dir2=1;
  101.           servo2.write((degree2+dir2*inc2)%180);
  102.         }
  103.       }
  104.       servo1.write((degree1+dir1*inc1)%180);
  105.     }
  106.     
  107.     delay(20);
  108.     
  109.     long microseconds = TP_init();
  110.     int dist_cm = Distance(microseconds);
  111.     Serial.print("Distance_CM = ");
  112.     Serial.println(dist_cm);
  113.     
  114.     
  115.     delay(200);
  116. }


  117. int Distance(long time)
  118. {
  119.     // Distance_CM = ((Duration of high level)*(Sonic :340m/s))/2
  120.     // = ((Duration of high level)*(Sonic :0.034 cm/us))/2
  121.     // = ((Duration of high level)/(Sonic :29.4 cm/us))/2
  122.     int dist = time/29/2 ;
  123.   
  124.     return dist;
  125. }

  126. long TP_init()
  127. {
  128.   digitalWrite(TP, LOW);
  129.   delayMicroseconds(2);
  130.   digitalWrite(TP, HIGH); // pull the Trig pin to high level for more than 10us impulse
  131.   delayMicroseconds(10);
  132.   digitalWrite(TP, LOW);
  133.   long microseconds = pulseIn(EP,HIGH); // waits for the pin to go HIGH, and returns the length of the pulse in microseconds
  134.   return microseconds; // return microseconds
  135. }

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