|
#include <Servo.h>
Servo servo_pin_1;
Servo servo_pin_2;
int dir1PinA = 13;
int speed;
int dir;
byte res;
void forward()
{analogWrite(10,200);
analogWrite(8,255);
analogWrite(6,200);
analogWrite(4,255);
digitalWrite(9,LOW);
digitalWrite(11,LOW);
digitalWrite(7,LOW);
digitalWrite(5,LOW);
}
void stops()
{digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
digitalWrite(6,LOW);
}
void leftward()
{analogWrite(6,255);
analogWrite(5,255);
analogWrite(10,255);
analogWrite(9,240);
digitalWrite(4,LOW);
digitalWrite(11,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
}
void rightward()
{analogWrite(7,200);
analogWrite(4,255);
analogWrite(11,200);
analogWrite(8,255);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
}
void backward()
{analogWrite(5,255);
analogWrite(7,255);
analogWrite(9,255);
analogWrite(11,255);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
digitalWrite(8,LOW);
digitalWrite(10,LOW);
}
void shang()
{int i;
for(i=0;i<=180;i++)
{
servo_pin_1.write(i);
}
}
void xia()
{int a;
for(a=180;a>=0;a--)
{
servo_pin_1.write(a);
}
}
void you()
{int i;
for(i=80;i<=120;i++)
{
servo_pin_2.write(i);
}
}
void zuo()
{int i;
for(i=120;i>=80;i--)
{
servo_pin_2.write(i);
}
}
void setup()
{
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
servo_pin_1.attach(22);
servo_pin_2.attach(46);
Serial.begin(9600); // 打开串口,设置波特率为9600 bps
Serial3.begin(9600); // 打开串口,设置波特率为9600 bps
speed = 0;
dir = 1;
}
void loop()
{
if (Serial3.available() > 0)
{
res = Serial3.read();
Serial.println(res);
if(res==0x01)
{
forward();
}
if(res==0x02)
{
backward();
}
if(res==0x03)
{
leftward();
}
if(res==0x04)
{
rightward();
}
if(res==0x06)
{
you();
}
if(res==0x05)
{
zuo();
}
if(res==0x07)
{
shang();
}
if(res==0x08)
{
xia();
}
if(res==0x00)
{
stops();
}
}
}
上面是我的wifi小车代码 用的是arduino平台。代码里0x00那段 如果加上的话 就不能控制了 去掉就能控制 请问是为什么 哪位能解释一下啊
|
|