发送端:
[mw_shl_code=cpp,true]int key0=0;
char flag;
void setup()
{
pinMode(key0,INPUT);
Serial.begin(9600);
}
void loop()
{
if(digitalRead(key0)==0)
{
flag='a';
Serial.print(flag);
}
delay(20);
}[/mw_shl_code]
接收端:
[mw_shl_code=cpp,true]int E1=5;
int E2=6;
int M1=4;
int M2=7;
void stop(void)
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void advance(char a,char b)
{
analogWrite(E1,a);
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,LOW);
}
void back_off(char a,char b)
{
analogWrite(E1,a);
digitalWrite(M1,LOW);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
}
void turn_L(char a,char b)
{
analogWrite(E1,a);
digitalWrite(M1,LOW);
analogWrite(E2,b);
digitalWrite(M2,LOW);
}
void turn_R(char a,char b)
{
analogWrite(E1,a);
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
}
void setup()
{
int i;
for(i=4;i<=7;i++)
pinMode(i,OUTPUT);
Serial.begin(9600);
}
void loop()
{
char val=Serial.read();
if(val!=-1)
{
switch(val)
{
case 'w':
advance(200,200);
break;
case 's':
back_off(200,200);
break;
case 'a':
turn_L(200,200);
break;
case 'd':
turn_R(200,200);
break;
}
delay(1000);
}
else stop();
}[/mw_shl_code] |