#define echoPinLeft 11 // Defining that echo pin of Left Ultrasonic sensor is connected to Digital Pin 11 and it will be called as echoPinLeft #define echoPinRight 2 // Defining that echo pin of Right Ultrasonic sensor is connected to Digital Pin 2 and it will be called as echoPinRight #define echoPinFront 8 // Defining that echo pin of Front Ultrasonic sensor is connected to Digital Pin 8 and it will be called as echoPinFront #define trigPinLeft 10 // Defining that trig pin of Left Ultrasonic sensor is connected to Digital Pin 10 and it will be called as trigPinLeft #define trigPinRight 9 // Defining that trig pin of Right Ultrasonic sensor is connected to Digital Pin 9 and it will be called as trigPinRight #define trigPinFront 7 // Defining that trig pin of Front Ultrasonic sensor is connected to Digital Pin 7 and it will be called as trigPinFront #define M1_a 3 // Defining pin no. and name of the pin to which 1st terminal of Left Motor is connected to Arduino. #define M1_b 4 // Defining pin no. and name of the pin to which 2nd terminal of Left Motor is connected to Arduino. #define M2_a 5 // Defining pin no. and name of the pin to which 1st terminal of Right Motor is connected to Arduino. #define M2_b 6 // Defining pin no. and name of the pin to which 2nd terminal of Right Motor is connected to Arduino. #define cutter 12 // Defining that the motor which will cut the grass is connected to Digital Pin 12 of Arduino UNO and will be called as cutter int wallDistance=10; // Initializing a integer type variable which stored the distance of the wall from left Ultrasonic sensor is stored const int frontWallDistance=30; // Initializing a constant integer type variable which stores the distance from the front at which the rover should stop moving straight. const int maximumRange = 30; long durationLeft; // Variable in which time taken by the left ultrasonic wave to reflect back and reach to the sensor is stored long distanceLeft; // Variable in which the distance of the object from the Left sensor is stored. long durationRight; // Variable in which time taken by the right ultrasonic wave to reflect back and reach to the sensor is stored long distanceRight; // Variable in which the distance of the object from the Right sensor is stored. long durationFront; // Variable in which time taken by the front ultrasonic wave to reflect back and reach to the sensor is stored long distanceFront; // Variable in which the distance of the object from the Front sensor is stored. void setup() { pinMode (M1_a, OUTPUT); // Defining Left Motor as OUTPUT pinMode (M1_b, OUTPUT); // Defining Left Motor as OUTPUT pinMode (M2_a, OUTPUT); // Defining Right Motor as OUTPUT pinMode (M2_b, OUTPUT); // Defining Right Motor as OUTPUT Serial.begin(9600); pinMode (trigPinLeft, OUTPUT); pinMode (trigPinRight, OUTPUT); pinMode (trigPinFront, OUTPUT); pinMode (echoPinLeft, INPUT ); pinMode (echoPinRight, INPUT ); pinMode (echoPinFront, INPUT ); } void loop() { readDistanceLeft(); readDistanceRight(); readDistanceFront(); left_right_u(); wallDistance=wallDistance + 12; readDistanceLeft(); readDistanceRight(); readDistanceFront(); right_left_u(); wallDistance=wallDistance+12; } void forward() // Defining function called forward(). Whenever this function will be called, the driving base will move forward. { digitalWrite(M1_a,HIGH); // Driving Base will move forward only if both the motors are rotating in forward direction. digitalWrite(M1_b,LOW); // On testing it came out that if HIGH value is provided to M1_a and LOW Value is provided to M1_b, then Left Motor will rotate in forward direction. digitalWrite(M2_a,LOW); // On testing it came out that if LOW value is provided to M2_a and HIGH Value is provided to M2_b, then Left Motor will rotate in forward direction. digitalWrite(M2_b,HIGH); // So Now both are Rotating in forward Direction, thus driving base will move in forward direction. } void backward() // Defining function called backward(). Whenever this function will be called, the driving base will move backward. { digitalWrite(M1_a,LOW); // Driving Base will move backward only if both the motors are rotating in backward direction. digitalWrite(M1_b,HIGH); // On testing it came out that if LOW value is provided to M1_a and HIGH Value is provided to M1_b, then Left Motor will rotate in backward direction. digitalWrite(M2_a,HIGH); // On testing it came out that if HIGH value is provided to M2_a and LOW Value is provided to M2_b, then Left Motor will rotate in backward direction. digitalWrite(M2_b,LOW); // So Now both are Rotating in backward Direction, thus driving base will move in backward direction. } void left() // Defining function called left(). Whenever this function will be called, the driving base will move left. { digitalWrite(M1_a,LOW); // Driving Base will move left if left motor is OFF and right motor is rotating in forward direction. digitalWrite(M1_b,LOW); // Motor will be OFF if there is no potential difference. Thus providing LOW value to both M1_a and M1_b to turn it OFF digitalWrite(M2_a,LOW); // On testing it came out that if LOW value is provided to M2_a and HIGH Value is provided to M2_b, then Left Motor will rotate in forward direction. digitalWrite(M2_b,HIGH); // So Now left motor is OFF and right motor is rotating in forward direction, thus driving base will turn left. } void right() // Defining function called right(). Whenever this function will be called, the driving base will move left. { digitalWrite(M1_a,HIGH); // Driving Base will move right if right motor is OFF and left motor is rotating in forward direction. digitalWrite(M1_b,LOW); // On testing it came out that if HIGH value is provided to M1_a and LOW Value is provided to M1_b, then Left Motor will rotate in forward direction. digitalWrite(M2_a,LOW); // Motor will be OFF if there is no potential difference. Thus providing LOW value to both M2_a and M2_b to turn it OFF digitalWrite(M2_b,LOW); // So Now right motor is OFF and left motor is rotating in forward direction, thus driving base will turn right. } void stop1() // Defining function called stop1(). Whenever this function will be called, the driving base will stop moving. { digitalWrite(4,LOW); // Driving base will stop only if both its motors are not rotating digitalWrite(3,LOW); // Motor will be OFF if there is no potential difference. Thus providing LOW value to both M1_a and M1_b to turn it OFF digitalWrite(5,LOW); // Motor will be OFF if there is no potential difference. Thus providing LOW value to both M2_a and M2_b to turn it OFF digitalWrite(6,LOW); // So Now both right motor and left motor are not rotating, thus driving base will not move. } void readDistanceLeft() // Defining function called readDistanceLeft(). { // Whenever this function will be called, the distance of the nearest object from the left US sensor will be stored in the variable distanceLeft digitalWrite(trigPinLeft,LOW); delayMicroseconds(2); digitalWrite(trigPinLeft,HIGH); delayMicroseconds(10); durationLeft=pulseIn(echoPinLeft,HIGH); distanceLeft= durationLeft/58.2; Serial.print("distanceLeft :"); Serial.println(distanceLeft); } void readDistanceRight() // Defining function called readDistanceRight(). { // Whenever this function will be called, the distance of the nearest object from the right US sensor will be stored in the variable distanceRight digitalWrite(trigPinRight,LOW); delayMicroseconds(2); digitalWrite(trigPinRight,HIGH); delayMicroseconds(10); durationRight=pulseIn (echoPinRight,HIGH); distanceRight= (durationRight)/58.2; Serial.print("distanceRight :"); Serial.println(distanceRight); } void readDistanceFront() // Defining function called readDistanceFront(). { // Whenever this function will be called, the distance of the nearest object from the front US sensor will be stored in the variable distanceFront digitalWrite(trigPinFront,LOW); delayMicroseconds(2); digitalWrite(trigPinFront,HIGH); delayMicroseconds(10); durationFront=pulseIn (echoPinFront,HIGH); distanceFront= (durationFront)/58.2; Serial.print("distanceFront :"); Serial.println(distanceFront); } void left_right_u() // Defining a function called left_right_u(). { // Whenever this function will be called, driving base will follow the left wall and maintain a particular distance and move forward. // The rover will stop only when there is some object in front of it at a distance of 30cm. readDistanceLeft(); readDistanceRight(); readDistanceFront(); while(distanceFront>frontWallDistance) { readDistanceLeft(); readDistanceRight(); readDistanceFront(); if(distanceLeft==wallDistance) { forward(); } if(distanceLeft>wallDistance) { left(); } if(distanceLeft=(wallDistance + 12)) { readDistanceLeft(); readDistanceRight(); readDistanceFront(); right(); if(distanceRight<(wallDistance + 12)) { left(); } if(distanceRight==(wallDistance + 12)) { stop1(); delay(1000); break; } } } if(distanceRight==(wallDistance + 12)) { stop1(); delay(1000); break; } } } void right_left_u() // Defining a function called right_left_u(). { // Whenever this function will be called, driving base will follow the right wall and maintain a particular distance and move forward. // The rover will stop only when there is some object in front of it at a distance of 30cm. while(distanceFront>frontWallDistance) { readDistanceLeft(); readDistanceRight(); readDistanceFront(); if(distanceRight==wallDistance) { forward(); } if(distanceRight>wallDistance) { right(); } if(distanceRight=(wallDistance + 12)) { readDistanceLeft(); readDistanceRight(); readDistanceFront(); left(); if(distanceLeft<(wallDistance + 12)) { right(); break; } if(distanceLeft==(wallDistance + 12)) { stop1(); delay(1000); break; } } } if(distanceLeft==(wallDistance + 12)) { stop1(); break; } } }