GMU:Artists Lab:Zhen Qin: Difference between revisions

From Medien Wiki
mNo edit summary
 
Line 137: Line 137:
Below: The Code for controlling the motor's speed and direction, via the ultrasonic sensor. The sensor outputs a range of distance values, and by continuously detecting the distance it can predict the mode of visitor's movement.
Below: The Code for controlling the motor's speed and direction, via the ultrasonic sensor. The sensor outputs a range of distance values, and by continuously detecting the distance it can predict the mode of visitor's movement.


  int SNUM[3] ;//定义3个传感器,从SNUM[0]-SNUM[2]为从左到右3个红外避障传感器
  int SNUM[3] ;//
  int INA = 4; //电机A正反转控制端
  int INA = 4; //
  int PWMA = 5; //电机A调速端
  int PWMA = 5; //
  int INB = 7; //电机B正反转控制端
  int INB = 7; //
  int PWMB = 6; //电机B调速端
  int PWMB = 6; //
    
    
  void motospd(int sp1,int sp2) //电机速度控制函数。括号内分别为左右电机速度值,
  void motospd(int sp1,int sp2) //电机速度控制函数。括号内分别为左右电机速度值,
Line 173: Line 173:
   }                 
   }                 
   Serial.println(SNUM[2]);
   Serial.println(SNUM[2]);
   if ((SNUM[0]==1)&&(SNUM[1]==1)&&(SNUM[2]==1))//所有传感器都没有检测到障碍
   if ((SNUM[0]==1)&&(SNUM[1]==1)&&(SNUM[2]==1))//
  motospd(100,100);                          //直行
  motospd(100,100);                          //
   if ((SNUM[0]==0)&&(SNUM[1]==1)&&(SNUM[2]==1)) //左传感器检测到障碍物
   if ((SNUM[0]==0)&&(SNUM[1]==1)&&(SNUM[2]==1)) //
     motospd(100,50);                          //右转
     motospd(100,50);                          //
     if ((SNUM[0]==1)&&(SNUM[1]==1)&&(SNUM[2]==0)) //右传感器检测到障碍物
     if ((SNUM[0]==1)&&(SNUM[1]==1)&&(SNUM[2]==0)) //
     motospd(50,100);                            //左转
     motospd(50,100);                            //
     if ((SNUM[0]==1)&&(SNUM[1]==0)&&(SNUM[2]==1)) //中传感器检测到障碍物
     if ((SNUM[0]==1)&&(SNUM[1]==0)&&(SNUM[2]==1)) //
     motospd(-100,-100);                          //后退
     motospd(-100,-100);                          //
  delay(50);}
  delay(50);}


Line 206: Line 206:
  long microsecondsToCentimeters(long microseconds) {
  long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
   return microseconds / 29 / 2;
  }//结果在Arduino串口监视器上看到传感器测量的距离,单位为英寸和厘米。
  }//


The code part that controls the servo:
The code part that controls the servo: