|
蓝牙app:
智能小车代码
- int EN1 = 14;
- int EN2 = 15;
- int EN3 = 16;
- int EN4 = 17;
- int ENA = 3;
- int ENB = 5;
- void setup()
- {
- int i;
- for(i=8;i<=13;i++)
- pinMode(i, OUTPUT);
- Serial.begin(9600); //定义波特率为19200,必须和APC一致
- }
- void loop()
- {
- char val;
- while(1)
- {
- val = Serial.read();
- if(val!=-1)
- {
- switch(val)
- {
- case 'a':
- qian();
- delay(50);
- break;
- case 'b':
- hou();
- delay(50);
- break;
- case 'c':
- zuo();
- delay(50);
- break;
- case 'd':
- you();
- delay(50);
- break;
- case 'e':
- ting();
- delay(50);
- break;
- }
- }
- }
- }
- void ting(void)
- {
- digitalWrite(EN1,LOW);
- digitalWrite(EN2,LOW);
- digitalWrite(EN3,LOW);
- digitalWrite(EN4,LOW);
- digitalWrite(ENA,LOW);
- digitalWrite(ENB,LOW);
- }
-
- void qian(void)
- {
- digitalWrite(EN1,LOW);
- digitalWrite(EN2,HIGH);
- digitalWrite(EN3,LOW);
- digitalWrite(EN4,HIGH);
- digitalWrite(ENA,HIGH);
- digitalWrite(ENB,HIGH);
- }
- void hou(void)
- {
- digitalWrite(EN1,HIGH);
- digitalWrite(EN2,LOW);
- digitalWrite(EN3,HIGH);
- digitalWrite(EN4,LOW);
- digitalWrite(ENA,HIGH);
- digitalWrite(ENB,HIGH);
- }
- void zuo(void)
- {
- digitalWrite(EN1,HIGH);
- digitalWrite(EN2,LOW);
- digitalWrite(EN3,LOW);
- digitalWrite(EN4,HIGH);
- digitalWrite(ENA,HIGH);
- digitalWrite(ENB,HIGH);
-
- }
- void you(void)
- {
- digitalWrite(EN1,LOW);
- digitalWrite(EN2,HIGH);
- digitalWrite(EN3,HIGH);
- digitalWrite(EN4,LOW);
- digitalWrite(ENA,HIGH);
- digitalWrite(ENB,HIGH);
- }
复制代码
|
评分
-
查看全部评分
|