#include <Servo.h>
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4); 
Servo myservo;  
Servo myservo2;
const int analogInPin0 = A0; ///เซ็นเซอร์มือคีบ
const int analogInPin1 = A1; //เซ็นเซอร์แทรคเส้นต่อOUT1
const int analogInPin2 = A2; //เซ็นเซอร์แทรคเส้นต่อOUT2
const int analogInPin3 = A3; //เซ็นเซอร์แทรคเส้นต่อOUT3
const int analogInPin4 = A4; //เซ็นเซอร์แทรคเส้นต่อOUT4
const int analogInPin5 = A5; //เซ็นเซอร์แทรคเส้นต่อOUT5
int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int Step=0;///ขั้นตอนการทำงาน
int Speed=150;
int aa=30;
void setup() 
{
 myservo.attach(9);
  myservo2.attach(10);
  Serial.begin(9600);
}
void loop() 
{
 
  
sensorValue0 = analogRead(analogInPin0);
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
sensorValue4 = analogRead(analogInPin4);
sensorValue5 = analogRead(analogInPin5);
if(sensorValue0<=200)
{
Step=1;
   Serial.print("s1 = ");
 Serial.print(sensorValue1);
  Serial.print("s2 = ");
 Serial.print(sensorValue2);
  Serial.print("s3 = ");
 Serial.print(sensorValue3);
  Serial.print("s4 = ");
 Serial.print(sensorValue4);
  Serial.print("s5 = ");
 Serial.print(sensorValue5);
 
 Serial.print("sensor = ");
 Serial.print(sensorValue0);
  Serial.print("step[");
 Serial.print(Step);
  Serial.println("]");  
delay(500);
}
else if(Step==1)
{
 LINE(); 
}
else
{
  STOP();
   Serial.print("s1 = ");
 Serial.print(sensorValue1);
  Serial.print("s2 = ");
 Serial.print(sensorValue2);
  Serial.print("s3 = ");
 Serial.print(sensorValue3);
  Serial.print("s4 = ");
 Serial.print(sensorValue4);
  Serial.print("s5 = ");
 Serial.print(sensorValue5);
 
 Serial.print("sensor = ");
 Serial.print(sensorValue0);
  Serial.print("step[");
 Serial.print(Step);
  Serial.println("]");  
}
}
void LINE()
{
 if(sensorValue1<500||sensorValue2<500)//ซ้าย
{
LEFT();
}
else if(sensorValue5<500||sensorValue4<500)//ขวา
{
RIGH(); 
}
 
else
{
GO();  
} 
}
void RIGH()
{
motor1.run(FORWARD);
motor1.setSpeed(Speed); 
motor2.run(BACKWARD);
motor2.setSpeed(Speed);  
delay(100);  
}
void LEFT()
{
motor1.run(BACKWARD);
motor1.setSpeed(Speed); 
motor2.run(FORWARD);
motor2.setSpeed(Speed); 
delay(100);  
}
void GO()
{
motor1.run(FORWARD);
motor1.setSpeed(Speed); 
motor2.run(FORWARD);
motor2.setSpeed(Speed);  
motor3.run(FORWARD);
motor3.setSpeed(Speed); 
motor4.run(FORWARD);
motor4.setSpeed(Speed);   
delay(100);  
}
void STOP()
{motor1.run(FORWARD);
motor1.setSpeed(0); 
motor2.run(FORWARD);
motor2.setSpeed(0);   
}
//////////////V2
#include <Servo.h>
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4); 
Servo myservo;  
Servo myservo2;
const int analogInPin0 = A0; ///เซ็นเซอร์มือคีบ
const int analogInPin1 = A1; //เซ็นเซอร์แทรคเส้นต่อOUT1
const int analogInPin2 = A2; //เซ็นเซอร์แทรคเส้นต่อOUT2
const int analogInPin3 = A3; //เซ็นเซอร์แทรคเส้นต่อOUT3
const int analogInPin4 = A4; //เซ็นเซอร์แทรคเส้นต่อOUT4
const int analogInPin5 = A5; //เซ็นเซอร์แทรคเส้นต่อOUT5
int sensorValue0 = 0;
int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int Step=0;///ขั้นตอนการทำงาน
int Speed=150;
int aa=30;
void setup() 
{
 myservo.attach(9);
  myservo2.attach(10);
  Serial.begin(9600);
}
void loop() 
{
 
  
sensorValue0 = analogRead(analogInPin0);
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
sensorValue4 = analogRead(analogInPin4);
if(sensorValue0<=200)
{
Step=1;
 
delay(500);
}
else if(Step==1)
{
 LINE(); 
}
else
{
  STOP();
  Serial.print("s1 = ");
 Serial.print(sensorValue1);
  Serial.print("s2 = ");
 Serial.print(sensorValue2);
  Serial.print("s3 = ");
 Serial.print(sensorValue3);
  Serial.print("s4 = ");
 Serial.print(sensorValue4);
//  Serial.print("s5 = ");
// Serial.print(sensorValue5);
 
 Serial.print("  BT=");
 Serial.print(sensorValue0);
  Serial.print("step[");
 Serial.print(Step);
  Serial.println("]");   
}
}
void LINE()
{
 if(sensorValue1<300||sensorValue2<300)//ซ้าย
{
LEFT();
}
else if(sensorValue3<300||sensorValue4<300)//ขวา
{
RIGH(); 
}
 
else
{
GO();  
} 
}
void RIGH()
{
motor1.run(FORWARD);
motor1.setSpeed(Speed); 
motor2.run(BACKWARD);
motor2.setSpeed(Speed);  
delay(100);  
}
void LEFT()
{
motor1.run(BACKWARD);
motor1.setSpeed(Speed); 
motor2.run(FORWARD);
motor2.setSpeed(Speed); 
delay(100);  
}
void GO()
{
motor1.run(FORWARD);
motor1.setSpeed(Speed); 
motor2.run(FORWARD);
motor2.setSpeed(Speed);  
motor3.run(FORWARD);
motor3.setSpeed(Speed); 
motor4.run(FORWARD);
motor4.setSpeed(Speed);   
delay(100);  
}
void STOP()
{motor1.run(FORWARD);
motor1.setSpeed(0); 
motor2.run(FORWARD);
motor2.setSpeed(0);   
}