<div class="content">
<a name="t3"</a>
<h2>硬件部分</h2>
<hr>
<p    用高低正负电平控制小车左右后轮来操作小车前进、后退、左右转。
具体程序如下:
</p>
<p    代码:<pre<code>
    #define LEFT_AHEAD 9
    #define LEFT_BACK 10
    #define RIGHT_AHEAD 12
    #define RIGHT_BACK 13
    #define FAST 100
    #define MEDIUM 70
    
    char order;
    
    void setup()
    {
     // put your setup code here, to run once:
     Serial.begin(9600);
     pinMode(LEFT_AHEAD,OUTPUT);
     pinMode(LEFT_BACK,OUTPUT);
     pinMode(RIGHT_AHEAD,OUTPUT);
     pinMode(RIGHT_BACK,OUTPUT);
     digitalWrite(LEFT_AHEAD,LOW);
     digitalWrite(LEFT_BACK,LOW);
     digitalWrite(RIGHT_AHEAD,LOW);
     digitalWrite(RIGHT_BACK,LOW);
    
     goAhead();
     delay(1000);
     stop();
     delay(100);
     turnRight();
     delay(1000);
     stop();
    }
    
    void loop()
    {
     // put your main code here, to run repeatedly:
     if (Serial.available()>0) {
     order = Serial.read();
     Serial.println("com get " + String(order));
     if (order=='w') {
     goAhead();
     Serial.println("go ahead");
     }
     else if (order=='s') {
     goBack();
     Serial.println("go back");
     }
     else if (order=='a') {
     turnLeft();
     Serial.println("turn left");
     }
     else if (order=='d') {
     turnRight();
     Serial.println("go right");
     }
     else if (order=='x') {
     stop();
     Serial.println("stop");
     }
     }
    }
    
    void stop()
    {
     digitalWrite(LEFT_AHEAD,LOW);
     digitalWrite(LEFT_BACK,LOW);
     digitalWrite(RIGHT_AHEAD,LOW);
     digitalWrite(RIGHT_BACK,LOW);
    }
    
    void goAhead()
    {
     digitalWrite(LEFT_BACK,LOW);
     digitalWrite(RIGHT_BACK,LOW);
    
     analogWrite(LEFT_AHEAD,MEDIUM);
     analogWrite(RIGHT_AHEAD,MEDIUM);
    }
    
    void goBack()
    {
     digitalWrite(LEFT_AHEAD,LOW);
     digitalWrite(RIGHT_AHEAD,LOW);
    
     analogWrite(LEFT_BACK,MEDIUM);
     analogWrite(RIGHT_BACK,MEDIUM);
    }
    
    void turnLeft()
    {
     digitalWrite(LEFT_AHEAD,LOW);
     digitalWrite(RIGHT_BACK,LOW);
    
     analogWrite(LEFT_BACK,FAST);
     analogWrite(RIGHT_AHEAD,FAST);
    }
    
    void turnRight()
    {
     digitalWrite(LEFT_BACK,LOW);
     digitalWrite(RIGHT_AHEAD,LOW);
    
     analogWrite(LEFT_AHEAD,FAST);
     analogWrite(RIGHT_BACK,FAST);
</code</pre>
</div>
</div>