专注电子技术学习与研究
当前位置:单片机教程网 >> Arduino >> 浏览文章

Arduino用键盘控制小车

作者:huqin   来源:本站原创   点击数:  更新时间:2014年04月03日   【字体:

原本想把摄像头接上去的,但找不到好的支架固定,所以先暂时搁着。

 

上次用Rboduino I/O Expansion V3.0扩展板插上去才能用APC220发射模块,后来发现,如果不安装什么云台的话,用Expansion V3.0板子有点浪费,还不如用面包版呢!

=====================================================================================================

 

int IN1 = 2;
int EN1 = 3;                        
int IN2 = 4;   
int IN3 = 5;  
int EN2 = 6;
int IN4 = 7;
                      
void stop(void)
        {
          digitalWrite(IN1,LOW);  
          digitalWrite(IN2,LOW);     
          digitalWrite(IN3,LOW);  
          digitalWrite(IN4,LOW);
        }


void advance_l(void)
        {     
          digitalWrite(IN1,HIGH);  
          digitalWrite(IN2,LOW); 
        } 
       
void advance_r(void)  
       {
          digitalWrite(IN3,LOW);  
          digitalWrite(IN4,HIGH);
        }

void back_off_l(void)
        {
          digitalWrite(IN1,LOW);  
          digitalWrite(IN2,HIGH); 
        } 
       
void back_off_r(void)  
       {
          digitalWrite(IN3,HIGH);  
          digitalWrite(IN4,LOW);
        }

       
void setup(void)
{
    int i;
    for(i=2;i<=7;i++)
    pinMode(i, OUTPUT); 
    digitalWrite(EN1,HIGH);
    digitalWrite(EN2,HIGH);
    Serial.begin(38400);//这个是我的APC220无线发射模块的发射波特率
}
 
void loop(void)
{
       char val = Serial.read();
       if(val!=-1)
         {
           switch(val)
           {
             case 'w'://前进
                         advance_l();
                         advance_r();
                         break;
              case 's'://后退
                         back_off_l();
                         back_off_r();
                         break;
             case 'a'://原地左转
                         advance_r();
                         back_off_l();
                         break;      
             case 'd'://原地右转
                         advance_l();
                         back_off_r();
                         break;
             case 'q'://左边的轮子静止,右边的轮子前进
                         advance_r();
                         break;
             case 'e'://右边的轮子静止,左边的轮子前进
                         advance_l();
                         break;
             case 'z'://左边的轮子静止,右边的轮子后退
                         back_off_r();
                         break;
             case 'c'://右边的轮子静止,左边的轮子后退
                         back_off_l();
                         break;

             }    
           delay(30);
          }
        else stop();         
}

 

======================================================================================================

APC无线发射模块的针脚图:

 


 

只需要接4个针脚,APC就可以运行了。GND:接地。VCC接5V。统统一起连接到Arduino板子上。

====================================================================================================

视频就不放出来了。

关闭窗口

相关文章