arduino智能小车为什么有的引脚可以不定义输入输出 100
#include<Servo.h>#defineSERVO3#defineINT_A19#defineINT_B18#defineINT_A211#defineINT_B...
#include<Servo.h>
#define SERVO 3
#define INT_A1 9
#define INT_B1 8
#define INT_A2 11
#define INT_B2 10
#define Triq 5
#define Echo 4
#define LASER 6
#define LASER_ON digitalWrite(LASER,1)
#define LASER_OFF digitalWrite(LASER,0)
Servo myservo;
float distance;
byte BT_COM;
byte BT_PWM=75;
byte LASER_Flag;
void BT_SERVO_REDUCE()
{
while(!Serial.available())
{
BT_PWM=2;
if(BT_PWM<5)BT_PWM=5;
myservo.write(BT_PWM);
delay(50);
}
}
void BT_SERVO_ADD()
{
while(!Serial.available())
{
BT_PWM+=2;
if(BT_PWM>150)BT_PWM=150;
myservo.write(BT_PWM);
delay(50);
}
}
void LASER_ON_SWITCH()
{
LASER_Flag=1;
while(!Serial.available())
{
LASER_ON;
}
}
void LASER_OFF_SWITCH()
{
LASER_Flag=0;
while(!Serial.available())
{
LASER_OFF;
}
}
void MY_DISTANCE()
{
digitalWrite(Triq,LOW);
delayMicroseconds(2);
digitalWrite(Triq,HIGH);
delayMicroseconds(10);
distance=pulseIn(Echo,HIGH);
distance=distance*0.018;
Serial.print("distance=");
Serial.print(distance);
Serial.println("cm");
}
void turnleft()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,1);
digitalWrite(INT_A2,1);
digitalWrite(INT_B2,0);
}
void turnright()
{
digitalWrite(INT_A1,1);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,1);
}
void forward()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,1);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,1);
}
void backup()
{
digitalWrite(INT_A1,1);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,1);
digitalWrite(INT_B2,0);
}
void stopcar()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,0);
}
void BiZhang()
{
while(!Serial.available())
{
MY_DISTANCE();
delay(50);
if(distance>20&&distance<40)turnleft();
else if(distance<20)backup();
else if(distance>40)forward();
}
}
void setup()
{
pinMode(Triq,OUTPUT);
pinMode(Echo,INPUT);
pinMode(LASER,OUTPUT);
myservo.attach(3);
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop()
{
if(Serial.available())
{
BT_COM=Serial.read();
switch(BT_COM)
{
case 'a': forward();
break;
case 'b': turnright();
break;
case'c': backup();
break;
case'd': turnleft();
break;
case'e': stopcar();
break;
case'f': BT_SERVO_REDUCE();
break;
case'g': BT_SERVO_ADD();
break;
case'h': BiZhang();
break;
case'i': MY_DISTANCE();
break;
case'j': if(LASER_Flag==0)LASER_ON_SWITCH();
else if(LASER_Flag==1)LASER_OFF_SWITCH();
break;
}
}
}
8,9,10,11为什么不定义输入输出 求大神指导 展开
#define SERVO 3
#define INT_A1 9
#define INT_B1 8
#define INT_A2 11
#define INT_B2 10
#define Triq 5
#define Echo 4
#define LASER 6
#define LASER_ON digitalWrite(LASER,1)
#define LASER_OFF digitalWrite(LASER,0)
Servo myservo;
float distance;
byte BT_COM;
byte BT_PWM=75;
byte LASER_Flag;
void BT_SERVO_REDUCE()
{
while(!Serial.available())
{
BT_PWM=2;
if(BT_PWM<5)BT_PWM=5;
myservo.write(BT_PWM);
delay(50);
}
}
void BT_SERVO_ADD()
{
while(!Serial.available())
{
BT_PWM+=2;
if(BT_PWM>150)BT_PWM=150;
myservo.write(BT_PWM);
delay(50);
}
}
void LASER_ON_SWITCH()
{
LASER_Flag=1;
while(!Serial.available())
{
LASER_ON;
}
}
void LASER_OFF_SWITCH()
{
LASER_Flag=0;
while(!Serial.available())
{
LASER_OFF;
}
}
void MY_DISTANCE()
{
digitalWrite(Triq,LOW);
delayMicroseconds(2);
digitalWrite(Triq,HIGH);
delayMicroseconds(10);
distance=pulseIn(Echo,HIGH);
distance=distance*0.018;
Serial.print("distance=");
Serial.print(distance);
Serial.println("cm");
}
void turnleft()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,1);
digitalWrite(INT_A2,1);
digitalWrite(INT_B2,0);
}
void turnright()
{
digitalWrite(INT_A1,1);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,1);
}
void forward()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,1);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,1);
}
void backup()
{
digitalWrite(INT_A1,1);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,1);
digitalWrite(INT_B2,0);
}
void stopcar()
{
digitalWrite(INT_A1,0);
digitalWrite(INT_B1,0);
digitalWrite(INT_A2,0);
digitalWrite(INT_B2,0);
}
void BiZhang()
{
while(!Serial.available())
{
MY_DISTANCE();
delay(50);
if(distance>20&&distance<40)turnleft();
else if(distance<20)backup();
else if(distance>40)forward();
}
}
void setup()
{
pinMode(Triq,OUTPUT);
pinMode(Echo,INPUT);
pinMode(LASER,OUTPUT);
myservo.attach(3);
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop()
{
if(Serial.available())
{
BT_COM=Serial.read();
switch(BT_COM)
{
case 'a': forward();
break;
case 'b': turnright();
break;
case'c': backup();
break;
case'd': turnleft();
break;
case'e': stopcar();
break;
case'f': BT_SERVO_REDUCE();
break;
case'g': BT_SERVO_ADD();
break;
case'h': BiZhang();
break;
case'i': MY_DISTANCE();
break;
case'j': if(LASER_Flag==0)LASER_ON_SWITCH();
else if(LASER_Flag==1)LASER_OFF_SWITCH();
break;
}
}
}
8,9,10,11为什么不定义输入输出 求大神指导 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询