---
# System prepended metadata

title: Untitled

---


```c=
void setup() {
  //UNO會讀取來自於IR sensor的A0和D0的感測值，設定讀取訊號的腳位為INPUT
  //pinMode(A0,INPUT);//選A0或D0任一即可
  pinMode(2,INPUT);//左邊sensor的D0
  pinMode(3,INPUT);//右邊sensor的D0
  
  //被控制端(L298N-馬達)
  pinMode(10,OUTPUT);//馬達IN_1腳位
  pinMode(9,OUTPUT);//馬達IN_2腳位
  pinMode(8,OUTPUT);//馬達IN_3腳位
  pinMode(7,OUTPUT);//馬達IN_4腳位
  
  //序列埠監控視窗baud rate設定
  Serial.begin(9600); 

  Serial.begin(9600); 
  pinMode(10,OUTPUT);//馬達IN_1腳位
  pinMode(9,OUTPUT);//馬達IN_2腳位
  pinMode(8,OUTPUT);//馬達IN_3腳位
  pinMode(7,OUTPUT);//馬達IN_4腳位
  pinMode(6,OUTPUT);//馬達EN_a腳位 左
  pinMode(5,OUTPUT);//馬達EN_b腳位 右
}
void loop() 
{ 
  analogWrite(6,255);//馬達2速度=200 左
  analogWrite(5,255);//馬達1速度=200 右
  Serial.print("123" );
    if(digitalRead(2)==0&&digitalRead(3)==0)
      {//如果兩個sensor感測值"都"為0
        digitalWrite(10,LOW);//馬達左邊
        digitalWrite(9,HIGH);
        digitalWrite(8,LOW);//馬達右邊
        digitalWrite(7,HIGH);
        delay(50);
        digitalWrite(10,LOW);//馬達左邊
        digitalWrite(9,LOW);
        digitalWrite(8,LOW);//馬達右邊
        digitalWrite(7,LOW);
        delay(50);
        Serial.print("GO" );
        
      }
      
      else
      {
          if(digitalRead(2)==0&&digitalRead(3)==1)
          {
            digitalWrite(10,LOW);//馬達左邊
            digitalWrite(9,LOW);
            digitalWrite(8,LOW);//馬達右邊
            digitalWrite(7,HIGH);
            delay(50);
            digitalWrite(10,LOW);//馬達左邊
            digitalWrite(9,LOW);
            digitalWrite(8,LOW);//馬達右邊
            digitalWrite(7,LOW);
        delay(50);
            
            Serial.print("left GO" );
          }  
             else
             {  
                if(digitalRead(2)==1&&digitalRead(3)==0)
                {
                 digitalWrite(10,LOW);//馬達左邊
                digitalWrite(9,HIGH);
                digitalWrite(8,LOW);//馬達右邊
                digitalWrite(7,LOW);
                delay(50);
                digitalWrite(10,LOW);//馬達左邊
                digitalWrite(9,LOW);
                digitalWrite(8,LOW);//馬達右邊
                digitalWrite(7,LOW);
                delay(50);
                Serial.print("right GO" );
                }
             else
             {
              digitalWrite(10,LOW);//馬達左邊
              digitalWrite(9,LOW);
              digitalWrite(8,LOW);//馬達右邊
              digitalWrite(7,LOW);
              Serial.print("STOP" );
            }
      }
   }
}
```