Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2027
  • 博文数量: 1
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-03 21:02
文章分类
文章存档

2013年(1)

我的朋友
最近访客

分类:

2013-01-03 21:12:17


  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. }

阅读(317) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~