ライントレーサ(ベースライン)

int left_sensor = A4;
int right_sensor = A3;
int thresh = 600;

#define DELAY_MS 100
#define DELAY_MS2 200
#define ML_Ctrl  2   //define the direction control pin of A motor
#define ML_PWM 9   //define the PWM control pin of A motor
#define MR_Ctrl  4   //define the direction control pin of B motor
#define MR_PWM 5   //define the PWM control pin of B motor


void setup() {
Serial.begin(9600);
  pinMode(ML_Ctrl, OUTPUT);//set the direction control pin of A motor to OUTPUT
  pinMode(ML_PWM, OUTPUT);//set the PWM control pin of A motor to OUTPUT
  pinMode(MR_Ctrl, OUTPUT);//set the direction control pin of B motor to OUTPUT
  pinMode(MR_PWM, OUTPUT);//set the PWM control pin of B motor to OUTPUT

//  Serial.println("front");
//  front();
//  Serial.println("back");
//  back();
//  Serial.println("left");
//  left();
//  Serial.println("right");
//  right();
//  Serial.println("stop");
  
}

void loop() {
  int left_val = analogRead(left_sensor);
  int right_val = analogRead(right_sensor);
  Serial.print("left:");
  Serial.print(left_val);
  Serial.print(",right:");
  Serial.println(right_val);
  if (left_val>thresh) {
    //left detected;
    Serial.println("left detect");
    left();
  } else if (right_val>thresh){
    //right detected;    
    Serial.println("right detect");
    right();
  } else {
    front();
  }
}
void back(){
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of A motor to low level
  analogWrite(ML_PWM,100);//set the PWM control speed of A motor to 100
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of B motor to low level
  analogWrite(MR_PWM,100);//set the PWM control speed of B motor to 100  
  delay(DELAY_MS);//delay in 2000ms
  stop_motor();
}

void front(){
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of A motor to high level
  analogWrite(ML_PWM,100);//set the PWM control speed of A motor to 100
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of B motor to high level
  analogWrite(MR_PWM,100);//set the PWM control speed of B motor to 100
  delay(DELAY_MS);//delay in 2000ms
  stop_motor();
}

void left(){
  digitalWrite(ML_Ctrl,HIGH);//set the direction control pin of A motor to HIGH level
  analogWrite(ML_PWM,100);// set the PWM control speed of A motor to 100
  digitalWrite(MR_Ctrl,LOW);//set the direction control pin of B motor to LOW level
  analogWrite(MR_PWM,100);//set the PWM control speed of B motor to 100
  delay(DELAY_MS);//delay in 2000ms
  stop_motor();
}

void right(){
  digitalWrite(ML_Ctrl,LOW);//set the direction control pin of A motor to LOW level
  analogWrite(ML_PWM,100);//100 set the PWM control speed of A motor to 100
  digitalWrite(MR_Ctrl,HIGH);//set the direction control pin of B motor to HIGH level
  analogWrite(MR_PWM,100);//set the PWM control speed of B motor to 100
  delay(DELAY_MS);//delay in 2000ms
  stop_motor();
}

void stop_motor(){
  analogWrite(ML_PWM,0);//set the PWM control speed of A motor to 0
  analogWrite(MR_PWM,0);// set the PWM control speed of B motor to 0
}