---
# System prepended metadata

title: 自走車(2)
tags: [2019科展]

---

> # 自走車(2)
> # 目錄
> [TOC]
# 腳位
## 超音波
|          | echopin  | trigpin  |
| -------- | -------- | -------- |
| 左邊      | A2       | 13       |
| 右邊      | A3       | 12       |
| 前面      | A4       | 11       |
## 馬達
|          |正轉       |反轉       |轉速(pwm)  |
|----------|----------|-----------|----------|
|右邊       | 4        | 5        | 3        |
|左邊       | 8        | 7        | 9        |
## 紅外線接收器
|          |腳位|
|----------|---|
|左邊       |10  |
|右邊       |2   |
# 程式
> ## 108/07/25
```cpp
//108/07/25
//馬達轉速(常數)
const int m_pwm_a = 3;//右邊
const int m_pwm_b = 9;//左邊
//馬達轉速(變數)
int pwm_a = 0;//右邊
int pwm_b = 0;//左邊
//右邊馬達正反轉
const int ain1 = 5;//反轉
const int ain2 = 4;//正轉
//左邊馬達正反轉
const int bin1 = 8;//正轉
const int bin2 = 7;//反轉
//超音波_前
#define echopinf A4
#define trigpinf 11
long distance_f;
long distance_f_2;
//超音波_右
#define echopinr A3
#define trigpinr 12
long distance_r;
//超音波_左
#define echopinl A2
#define trigpinl 13
long distance_l;
//紅外線_右
#define ur_r A3
int ur_R;
//紅外線_左
#define ur_l A4
int ur_L;

#define m1 A6
#define m2 A7
#define Start_Button A0
#define Mode_SW A1

boolean Time_Status = false;
int Previous_Time = 0;
int Current_Time = 0;
int Timmer = 0;
long  Timer_Start_Time= 0;
boolean Car_Cal = false;
boolean Car_RR = false;
boolean Car_RL = false;
boolean Car_F = false;
boolean Timer_Status = false;
boolean Car_Start_Flag = false;
boolean OP_Mode = false;
boolean Button_Click = false;
boolean Button_Previous_Status = 1;
boolean Button_Current_Status = 1;
boolean ReadButton;
boolean Car_SP = false;
boolean start_flag = false;

int val = 0;// variable for reading the pin status
int val2 =0;
int counter = 0;
int counter2 = 0;
int sum = 0;
int sum2 = 0;
int currentState = 0;
int currentState2 = 0;
int previousState = 0;
int previousState2 = 0;
long previousTime = 0;
long currentTime =0;
int Timer = 0;

int run_time;
int timer_1, timer_2;

/*-----------------------------------------------------------------------------------------------------------*/

void setup() {
  //馬達(輸出)
  pinMode(m_pwm_a, OUTPUT);//右邊
  pinMode(m_pwm_b, OUTPUT);//左邊
  //右邊馬達(輸出)
  pinMode(ain1, OUTPUT);//反轉
  pinMode(ain2, OUTPUT);//正轉
  //左邊馬達(輸出)
  pinMode(bin1, OUTPUT);//反轉
  pinMode(bin2, OUTPUT);//正轉
  
  pinMode(6, OUTPUT);
  //設定超音波_前
  pinMode(echopinf, INPUT);//輸出
  pinMode(trigpinf, OUTPUT);//輸入
  //設定超音波_右
  pinMode(echopinr, INPUT);//輸出
  pinMode(trigpinr, OUTPUT);//輸入
  //設定超音波_左
  pinMode(echopinl, INPUT);//輸出
  pinMode(trigpinl, OUTPUT);//輸入 

  pinMode(Start_Button,INPUT);
  pinMode(Mode_SW,INPUT);
  
  Serial.begin(9600);
  Serial.println("begin");
  pinMode(m1, INPUT);
  pinMode(m2, INPUT);
  previousTime=millis();
  Serial.println("Starting...");
  Serial.println(digitalRead(Start_Button));
//  delay(5000);
}

/*-----------------------------------------------------------------------------------------------------------*/

void loop() {
   while (Car_Start_Flag == false){
    CheckButton();
    if (Button_Click == 1){
    OP_Mode = digitalRead(Mode_SW);
    Car_Start_Flag = true;
    Button_Click = 0;
    delay(500);
    }
  }
 while (start_flag == false){
  distance_f = scan_f();
  distance_f_2 = scan_f();
  delay(500);
  Serial.println(distance_f);
  forward();
  delay(1200)
  Car_F = false;
  distance_r = scan_r();
  if (distance_r >= 30) {
    turn_right();
    delay(300);
    forward();
    delay(1200);    
  }
  Car_F = false;
  Car_RR = false;
  start_flag = true;
 }
 distance_r = scan_r();
 distance_l = scan_l();
 distance_f = scan_f();
 delay(500);
 if (distance_r >= 20){
  turn_right();
  delay(300);
  forward();
  delay(500);     
  }else if (distance_f >= 20){
   forward();
   delay(500);
  }else if (distance_l >= 20){
  turn_left();
  delay(300);
  forward();
  delay(500);
  }else{
  turn_right();
  delay(300);
  turn_right();
  delay(300);
 }  
 
 
 car_stop();
 Car_F = false;
 Car_RR = false;
 Car_RL = false;
 Car_SP = false;

}
/*-----------------------------------------------------------------------------------------------------------*/
void CheckRPM(){
  val = digitalRead(m1);// read input value
  val2 = digitalRead(m2);
  if (val == HIGH) { // check if the input is HIGH (button released)
  //  digitalWrite(ledPin, HIGH); // turn LED on
    currentState = 1;
  }
  else {
  //  digitalWrite(ledPin, LOW); // turn LED off
    currentState = 0;
  }
  if(currentState != previousState){
    if(currentState == 1){
      counter = counter + 1;
      sum++;
    }
  }
  if (val2 == HIGH) { // check if the input is HIGH (button released)
    //digitalWrite(ledPin, HIGH); // turn LED on
    currentState2 = 1;
  }
  else {
    //digitalWrite(ledPin, LOW); // turn LED off
    currentState2 = 0;
  }
  if(currentState2 != previousState2){
    if(currentState2 == 1){
      counter2 = counter2 + 1;
      sum2 ++;
    }
  }    
  if (Timer_Status == true){
    Serial.print("Motor1 RPM=");
    Serial.println(counter);
    Serial.println(sum);
    Serial.print("Motor2 RPM=");
    Serial.println(counter2);
    Serial.println(sum2);
    previousTime=millis();
    counter = 0;
    counter2 = 0;
    Timer_Status = false;
  }

  previousState = currentState;
  previousState2 = currentState2;
  delay(10);
  // car_stop();
  // delay(1000);

}

void CheckButton(){
  ReadButton=digitalRead(Start_Button);
  if (ReadButton == HIGH){
    Button_Current_Status = 1;
  }
  else{
    Button_Current_Status = 0;
  }
  if (Button_Current_Status != Button_Previous_Status){
    if (Button_Current_Status == 1){
      Button_Click = 1;
    }
    }
  Button_Previous_Status=Button_Current_Status;  
  }

long scan_f()
{ //感測副程式_前
  long dur, dis;
  
    digitalWrite(trigpinf, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinf, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinf, LOW);
    dur = pulseIn(echopinf, HIGH);
    dis = (dur / 2) / 29.1;

  return dis;
}
long scan_r()
{ //感測副程式_右
  long dur, dis;
  
    digitalWrite(trigpinr, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinr, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinr, LOW);
    dur = pulseIn(echopinr, HIGH);
    dis = (dur / 2) / 29.1;
  
  return dis;
}
long scan_l()
{ //感測副程式_左
  long dur, dis;
  
    digitalWrite(trigpinl, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinl, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinl, LOW);
    dur = pulseIn(echopinl, HIGH);
    dis = (dur / 2) / 29.1;
  
  return dis;
}
void forward() {
  pwm_a = 100;
  pwm_b = 100;
  run_time;
  analogWrite(m_pwm_a, pwm_a);
  analogWrite(m_pwm_b, pwm_b);
  digitalWrite(ain1, 0);
  digitalWrite(ain2, 1);
  digitalWrite(bin1, 1);
  digitalWrite(bin2, 0);
  digitalWrite(6, 1);
  Car_F=true;
  timer_1 = millis();
  do{
    timer_2 = millis();
    if (ur_R == 0){
      pwm_b -= 5;
      analogWrite(m_pwm_b, pwm_b);
    }
    if (ur_L == 0){
      pwm_a -= 5;
      analogWrite(m_pwm_a, pwm_a);
    }
  }while (timer_1 - timer_2 >=1200)
  
 
}



void turn_right() {
  analogWrite(m_pwm_a, 100);
  analogWrite(m_pwm_b, 100);
  digitalWrite(ain1, 0);
  digitalWrite(ain2, 1);
  digitalWrite(bin1, 0);
  digitalWrite(bin2, 1);
  digitalWrite(6, 1);
  Car_RR = true;
  delay(280);
  car_stop();
}

void turn_left() {
  analogWrite(m_pwm_a, 100);
  analogWrite(m_pwm_b, 100);
  digitalWrite(ain1, 1);
  digitalWrite(ain2, 0);
  digitalWrite(bin1, 1);
  digitalWrite(bin2, 0);
  digitalWrite(6, 1);
  Car_RL = true;
  delay(280);
  car_stop();
}

void car_stop() {
  digitalWrite(6, 0);
  Car_SP = true;
}
```
> ## 108/07/27
```cpp
//108/07/27
//馬達轉速(常數)
const int m_pwm_a = 3;//右邊
const int m_pwm_b = 9;//左邊
//馬達轉速(變數)
int pwm_a = 0;//右邊
int pwm_b = 0;//左邊
//右邊馬達正反轉
const int ain1 = 5;//反轉
const int ain2 = 4;//正轉
//左邊馬達正反轉
const int bin1 = 8;//正轉
const int bin2 = 7;//反轉
//超音波_前
#define echopinf A4
#define trigpinf 11
long distance_f;
long distance_f_2;
//超音波_右
#define echopinr A3
#define trigpinr 12
long distance_r;
//超音波_左
#define echopinl A2
#define trigpinl 13
long distance_l;
//紅外線_右
#define ur_r 2
int ur_R;
//紅外線_左
#define ur_l 10
int ur_L;

#define m1 A6
#define m2 A7
#define Start_Button A0
#define Mode_SW A1

boolean Time_Status = false;
int Previous_Time = 0;
int Current_Time = 0;
int Timmer = 0;
long  Timer_Start_Time= 0;
boolean Car_Cal = false;
boolean Car_RR = false;
boolean Car_RL = false;
boolean Car_F = false;
boolean Timer_Status = false;
boolean Car_Start_Flag = false;
boolean OP_Mode = false;
boolean Button_Click = false;
boolean Button_Previous_Status = 1;
boolean Button_Current_Status = 1;
boolean ReadButton;
boolean Car_SP = false;
boolean start_flag = false;

int val = 0;// variable for reading the pin status
int val2 =0;
int counter = 0;
int counter2 = 0;
int sum = 0;
int sum2 = 0;
int currentState = 0;
int currentState2 = 0;
int previousState = 0;
int previousState2 = 0;
long previousTime = 0;
long currentTime =0;
int Timer = 0;

int run_time;
int timer_1, timer_2;

/*-----------------------------------------------------------------------------------------------------------*/

void setup() {
  //馬達(輸出)
  pinMode(m_pwm_a, OUTPUT);//右邊
  pinMode(m_pwm_b, OUTPUT);//左邊
  //右邊馬達(輸出)
  pinMode(ain1, OUTPUT);//反轉
  pinMode(ain2, OUTPUT);//正轉
  //左邊馬達(輸出)
  pinMode(bin1, OUTPUT);//反轉
  pinMode(bin2, OUTPUT);//正轉
  
  pinMode(6, OUTPUT);
  //設定超音波_前
  pinMode(echopinf, INPUT);//輸出
  pinMode(trigpinf, OUTPUT);//輸入
  //設定超音波_右
  pinMode(echopinr, INPUT);//輸出
  pinMode(trigpinr, OUTPUT);//輸入
  //設定超音波_左
  pinMode(echopinl, INPUT);//輸出
  pinMode(trigpinl, OUTPUT);//輸入 

  pinMode(Start_Button,INPUT);
  pinMode(Mode_SW,INPUT);
  
  Serial.begin(9600);
  Serial.println("begin");
  pinMode(m1, INPUT);
  pinMode(m2, INPUT);
  previousTime=millis();
  Serial.println("Starting...");
  Serial.println(digitalRead(Start_Button));
//  delay(5000);
}

/*-----------------------------------------------------------------------------------------------------------*/

void loop() {
   while (Car_Start_Flag == false){
    CheckButton();
    if (Button_Click == 1){
    OP_Mode = digitalRead(Mode_SW);
    Car_Start_Flag = true;
    Button_Click = 0;
    delay(500);
    }
  }
 while (start_flag == false){
  distance_f = scan_f();
  forward();
  delay(run_time);
  Car_F = false;
  distance_r = scan_r();
  delay(500);
  if (distance_r >= 30) {
    turn_right();
    delay(run_time);
    forward();
    delay(run_time);    
  }
  Car_F = false;
  Car_RR = false;
  start_flag = true;
 }
 distance_r = scan_r();
 distance_l = scan_l();
 distance_f = scan_f();
 Serial.print("f:");Serial.println(distance_f);
 delay(500);
 if (distance_r >= 30){
  turn_right();
  delay(run_time);
  forward();
  delay(run_time);     
  }else if (distance_f >= 30){
  forward();
  delay(run_time);
  }else if (distance_l >= 30){
  turn_left();
  delay(run_time);
  forward();
  delay(run_time);
  }else{
  turn_right();
  delay(run_time);
  turn_right();
  delay(run_time);
 }  
 
 
 car_stop();
 Car_F = false;
 Car_RR = false;
 Car_RL = false;
 Car_SP = false;

}
/*-----------------------------------------------------------------------------------------------------------*/
void CheckRPM(){
  val = digitalRead(m1);// read input value
  val2 = digitalRead(m2);
  if (val == HIGH) { // check if the input is HIGH (button released)
  //  digitalWrite(ledPin, HIGH); // turn LED on
    currentState = 1;
  }
  else {
  //  digitalWrite(ledPin, LOW); // turn LED off
    currentState = 0;
  }
  if(currentState != previousState){
    if(currentState == 1){
      counter = counter + 1;
      sum++;
    }
  }
  if (val2 == HIGH) { // check if the input is HIGH (button released)
    //digitalWrite(ledPin, HIGH); // turn LED on
    currentState2 = 1;
  }
  else {
    //digitalWrite(ledPin, LOW); // turn LED off
    currentState2 = 0;
  }
  if(currentState2 != previousState2){
    if(currentState2 == 1){
      counter2 = counter2 + 1;
      sum2 ++;
    }
  }    
  if (Timer_Status == true){
    Serial.print("Motor1 RPM=");
    Serial.println(counter);
    Serial.println(sum);
    Serial.print("Motor2 RPM=");
    Serial.println(counter2);
    Serial.println(sum2);
    previousTime=millis();
    counter = 0;
    counter2 = 0;
    Timer_Status = false;
  }

  previousState = currentState;
  previousState2 = currentState2;
  delay(10);
  // car_stop();
  // delay(1000);

}

void CheckButton(){
  ReadButton=digitalRead(Start_Button);
  if (ReadButton == HIGH){
    Button_Current_Status = 1;
  }
  else{
    Button_Current_Status = 0;
  }
  if (Button_Current_Status != Button_Previous_Status){
    if (Button_Current_Status == 1){
      Button_Click = 1;
    }
    }
  Button_Previous_Status=Button_Current_Status;  
  }

long scan_f()
{ //感測副程式_前
  long dur, dis;
  
    digitalWrite(trigpinf, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinf, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinf, LOW);
    dur = pulseIn(echopinf, HIGH);
    dis = (dur / 2) / 29.1;

  return dis;
}
long scan_r()
{ //感測副程式_右
  long dur, dis;
  
    digitalWrite(trigpinr, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinr, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinr, LOW);
    dur = pulseIn(echopinr, HIGH);
    dis = (dur / 2) / 29.1;
  
  return dis;
}
long scan_l()
{ //感測副程式_左
  long dur, dis;
  
    digitalWrite(trigpinl, LOW);
    delayMicroseconds(2);
    digitalWrite(trigpinl, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigpinl, LOW);
    dur = pulseIn(echopinl, HIGH);
    dis = (dur / 2) / 29.1;
  
  return dis;
}
void forward() {
  pwm_a = 150;
  pwm_b = 150;
  run_time = 700;
  analogWrite(m_pwm_a, pwm_a);
  analogWrite(m_pwm_b, pwm_b);
  digitalWrite(ain1, 0);
  digitalWrite(ain2, 1);
  digitalWrite(bin1, 1);
  digitalWrite(bin2, 0);
  digitalWrite(6, 1);
  Car_F=true;
  timer_1 = millis();
  do{
    timer_2 = millis();
    ur_R = digitalRead(ur_r);
    ur_L = digitalRead(ur_l);
    if (ur_R == 1){
      pwm_b -= 10;
      analogWrite(m_pwm_b, pwm_b);
      while (ur_R == 1){
        ur_R = digitalRead(ur_r);
      }
      pwm_b = 150;
      analogWrite(m_pwm_b, pwm_b);
    }
    if (ur_L == 1){
      pwm_a -= 10;
      analogWrite(m_pwm_a, pwm_a);
      while (ur_L == 1){
       ur_L = digitalRead(ur_r);
      }
      pwm_a = 150;
      analogWrite(m_pwm_a, pwm_a);
    }
  }while (timer_2 - timer_1 <= run_time);
  car_stop();
}



void turn_right() {
  pwm_a = 100;
  pwm_b = 100;
  run_time = 280;
  analogWrite(m_pwm_a, pwm_a);
  analogWrite(m_pwm_b, pwm_b);
  digitalWrite(ain1, 0);
  digitalWrite(ain2, 1);
  digitalWrite(bin1, 0);
  digitalWrite(bin2, 1);
  digitalWrite(6, 1);
  Car_RR = true;
  timer_1 = millis();
  do{
    timer_2 = millis();
  }while (timer_2 - timer_1 >= run_time);
}

void turn_left() {
  pwm_a = 100;
  pwm_b = 100;
  run_time = 280;
  analogWrite(m_pwm_a, pwm_a);
  analogWrite(m_pwm_b, pwm_b);
  digitalWrite(ain1, 1);
  digitalWrite(ain2, 0);
  digitalWrite(bin1, 1);
  digitalWrite(bin2, 0);
  digitalWrite(6, 1);
  Car_RL = true;
  timer_1 = millis();
  do{
    timer_2 = millis();
  }while (timer_2 - timer_1 >= run_time); 
}

void car_stop() {
  digitalWrite(6, 0);
  Car_SP = true;
}
```

###### tags: `2019科展`