Arduino 寻迹程序——王洋

Arduino 单片机 四驱 四路红外寻线


下面为arduino单片机控制四驱四红外传感器的程序,没有揉合颜色传感器程序,后期会有GY-33匹配arduino的相应程序。更多的程序即黑带情况需要大家自行研究学习,下面只给出了简单的寻迹,更多详细内容如arduino引脚功能,程序主要关键字等。


  int l_go = 13 ;       // 左前电机 前进
  int l_back = 12 ;       // 后退
  
  int ll_go = 9 ;      //左后电机  前进
  int ll_back = 8 ;      //后退

  int r_go = 4 ;       // 右前电机  前进
  int r_back = 7 ;       //后退
  
  int rr_go = 2 ;      //右后电机   前进
  int rr_back = 3 ;      //后退
  
  int l_pwm =11  ;     // 左前电机 控速
  int ll_pwm = 10 ;     // 左后
   
  int r_pwm = 6  ;     // 左后电机 控速
  int rr_pwm = 5  ;     // 右后

  const int x1 = A0 ;    // x1 x2 x3 x4 红外传感
  const int x2 = A1 ;   //  A1 接传感器的 X2
  const int x3 = A2 ;   //  _____
  const int x4 = A3 ;   //  _____

  int l1 ;       //
  int l2 ;       //
  int l3 ;       //
  int l4 ;       //

void setup()          // 端口初始化函数

{  

  Serial.begin(9600);  // 可认为 为数据传输速度

  pinMode( l_go ,  OUTPUT  );  
  pinMode( ll_go ,  OUTPUT  );
  pinMode( l_back ,  OUTPUT  );
  pinMode( ll_back ,  OUTPUT  );
 
  pinMode( r_go,  OUTPUT  );
  pinMode( rr_go ,  OUTPUT  );
  pinMode( r_back ,  OUTPUT  );
  pinMode( rr_back ,  OUTPUT  );

  pinMode(r_pwm, INPUT);
  pinMode(rr_pwm, INPUT);
  pinMode(r_pwm, INPUT);
  pinMode(rr_pwm, INPUT);
   
   pinMode ( l1 , INPUT );
   pinMode ( l2 , INPUT );
   pinMode ( l3 , INPUT );
   pinMode ( l4 , INPUT );

   digitalWrite ( l1 ,  HIGH ) ;
   digitalWrite ( l2 ,  HIGH ) ;
   digitalWrite ( l3 ,  HIGH ) ;
   digitalWrite ( l4 ,  HIGH ) ;
}


void Go ( int l_speed , int ll_speed , int r_speed , int rr_speed )    // 向前走

{
  digitalWrite ( l_go , HIGH ) ;
  digitalWrite ( l_back , LOW  ) ;
  analogWrite  ( l_pwm , l_speed ) ;
 
  digitalWrite ( ll_go , HIGH ) ;
  digitalWrite ( ll_back , LOW  ) ;
  analogWrite  ( ll_pwm , ll_speed ) ;

  
  digitalWrite ( r_go , HIGH ) ;
  digitalWrite ( r_back , LOW  ) ;
  analogWrite  ( r_pwm , r_speed ) ;
  
  digitalWrite ( rr_go , HIGH ) ;
  digitalWrite ( rr_back , LOW  ) ;
  analogWrite  ( rr_pwm , rr_speed ) ; 
  
}

void Back ( int l_speed,int ll_speed ,int r_speed ,int rr_speed )     // 往后走

 {
  digitalWrite ( l_go , LOW ) ;
  digitalWrite ( l_back , HIGH ) ;
  digitalWrite ( ll_go , LOW ) ;
  digitalWrite ( ll_back , HIGH) ;
 
  analogWrite  ( l_pwm , l_speed ) ;
  analogWrite  ( ll_pwm , ll_speed ) ;

 
 
  digitalWrite ( r_go , LOW  ) ;
  digitalWrite ( r_back , HIGH  ) ;
  digitalWrite ( rr_go , LOW  ) ;
  digitalWrite ( rr_back , HIGH ) ;
  
  analogWrite  ( r_pwm , r_speed ) ;
  analogWrite  ( rr_pwm , rr_speed ) ;
}


/////////////////////////////////////////////////////
void wheel_90( )  //  **90度转弯函数** **以及半圆弧函数**
{
  //省略
}
/////////////////////////////////////////////////////




void loop ()  // 主函数  无限循环
{
    l1 = digitalRead (x1);    //灯灭  HIGH 没扫到黑带
    l2 = digitalRead (x2);  
    l3 = digitalRead (x3);
    l4 = digitalRead (x4);
//
//以下只罗列个别情况,具体情况参赛者自行分析。只提供思路。
//
if ( l2 == HIGH && l1 == LOW && l3 == LOW && l4 == HIGH )   //直走
      {
         Go ( 100 ,100 ,100 ,100 ) ; 
         delay (100); 
      }
if ( l2 == LOW && l1 == LOW && l3 == HIGH && l4 == HIGH )     //左微转 
      {
         Go ( 80 ,80 ,100 ,100 ) ;  
      }
if ( l2 == HIGH && l1 == HIGH  && l3 == LOW && l4 == LOW )    //右微转  
      {
        Go ( 100 ,100 ,80 ,80 ) ;  
      }

  }



娱乐:
void wheel_w ( int l_speed,int ll_speed ,int r_speed ,int rr_speed ) // 原地旋转函数
{
  digitalWrite ( l_go , HIGH ) ;
  digitalWrite ( l_back , LOW ) ;
  digitalWrite ( ll_go , HIGH ) ;
  digitalWrite ( ll_back , LOW ) ;
 
  analogWrite  ( l_pwm , l_speed ) ;
  analogWrite  ( ll_pwm , ll_speed ) ;

  digitalWrite ( r_go , LOW  ) ;
  digitalWrite ( r_back , HIGH  ) ;
  digitalWrite ( rr_go , LOW  ) ;
  digitalWrite ( rr_back , HIGH ) ;
  
  analogWrite  ( r_pwm , r_speed ) ;
  analogWrite  ( rr_pwm , rr_speed ) ;
}

颜色模块后续…

你可能感兴趣的:(arduino)