int random2; int a; int a1; int a2; #define ML_Ctrl 13 //define the direction control pin of left motor #define ML_PWM 11 //define PWM control pin of left motor #define MR_Ctrl 12 //define the direction control pin of right motor #define MR_PWM 3 //define PWM control pin of right motor #define Trig 5 //ultrasonic trig Pin #define Echo 4 //ultrasonic echo Pin int distance; #define servoPin 9 //servo Pin int pulsewidth; int sensorPin = A3; // select the input pin for the photocell int sensorValue = 0; // variable to store the value coming from the sensor int INAPin = 13; int INBPin = 12; #define light_L_Pin A1 //define the pin of left photo resistor #define light_R_Pin A2 //define the pin of right photo resistor int left_light; int right_light; void Car_front() { digitalWrite(MR_Ctrl, LOW); analogWrite(MR_PWM, 200); digitalWrite(ML_Ctrl, LOW); analogWrite(ML_PWM, 200); } void Car_back() { digitalWrite(MR_Ctrl, HIGH); analogWrite(MR_PWM, 200); digitalWrite(ML_Ctrl, HIGH); analogWrite(ML_PWM, 200); } void Car_left() { digitalWrite(MR_Ctrl, LOW); analogWrite(MR_PWM, 255); digitalWrite(ML_Ctrl, HIGH); analogWrite(ML_PWM, 255); } void Car_right() { digitalWrite(MR_Ctrl, HIGH); analogWrite(MR_PWM, 255); digitalWrite(ML_Ctrl, LOW); analogWrite(ML_PWM, 255); } void Car_Stop() { digitalWrite(MR_Ctrl, LOW); analogWrite(MR_PWM, 0); digitalWrite(ML_Ctrl, LOW); analogWrite(ML_PWM, 0); } //The function to control servo void procedure(int myangle) { for (int i = 0; i <= 50; i = i + (1)) { pulsewidth = myangle * 11 + 500; digitalWrite(servoPin, HIGH); delayMicroseconds(pulsewidth); digitalWrite(servoPin, LOW); delay((20 - pulsewidth / 1000)); } } //The function to control ultrasonic sensor float checkdistance() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig, HIGH); delayMicroseconds(10); digitalWrite(Trig, LOW); float distance = pulseIn(Echo, HIGH) / 58.00; //58.20, that is, 2*29.1=58.2 delay(10); return distance; } //**************************************************************** void setup() { pinMode(servoPin, OUTPUT); procedure(111); //set servo to 90° pinMode(Trig, OUTPUT); pinMode(Echo, INPUT); pinMode(ML_Ctrl, OUTPUT); pinMode(ML_PWM, OUTPUT); pinMode(MR_Ctrl, OUTPUT); pinMode(MR_PWM, OUTPUT); pinMode(INAPin, OUTPUT); pinMode(INBPin, OUTPUT); pinMode(light_L_Pin, INPUT); pinMode(light_R_Pin, INPUT); } void loop() { random2 = random(1, 100); a = checkdistance(); //assign the front distance detected by ultrasonic sensor to variable a sensorValue = analogRead(sensorPin); // read the value from the sensor: left_light = analogRead(light_L_Pin); right_light = analogRead(light_R_Pin); if (sensorValue > 1000) { digitalWrite(INAPin, LOW); digitalWrite(INBPin, LOW); if (a < 20) //when the front distance detected is less than 20 { Car_Stop(); //robot stops delay(500); //delay in 500ms procedure(160); //Ultrasonic platform turns left for (int j = 1; j <= 10; j = j + (1)) { //for statement, the data will be more accurate if ultrasonic sensor detect a few times. a1 = checkdistance(); //assign the left distance detected by ultrasonic sensor to variable a1 } delay(300); procedure(20); //Ultrasonic platform turns right for (int k = 1; k <= 10; k = k + (1)) { a2 = checkdistance(); //assign the right distance detected by ultrasonic sensor to variable a2 } if (a1 < 50 || a2 < 50) //robot will turn to the longer distance side when left or right distance is less than 50cm. { if (a1 > a2) //left distance is greater than right side { procedure(111); //Ultrasonic platform turns back to right ahead Car_left(); //robot turns left delay(500); //turn left for 500ms Car_front(); //go front } else { procedure(111); Car_right(); //robot turns right delay(500); Car_front(); //go front } } else //If both side is greater than or equal to 50cm, turn left or right randomly { if ((long)(random2) % (long)(2) == 0) //When the random number is even { procedure(111); Car_left(); //tank robot turns left delay(500); Car_front(); //go front } else { procedure(111); Car_right(); //robot turns right delay(500); Car_front(); //go front } } } else //If the front distance is greater than or equal to 20cm, robot car will go front { if (left_light > 650 && right_light > 650) { //the value detected photo resistor,go front Car_front(); } else if (left_light > 650 && right_light <= 650) { //the value detected photo resistor,turn left Car_left(); } else if (left_light <= 650 && right_light > 650) { //the value detected photo resistor,turn right Car_right(); } } } else { Car_Stop(); delay(1000); analogWrite(INAPin, 255); digitalWrite(INBPin, LOW); delay(5000); } }