int signal_emission = 7; //The emission of an ultrasonic signal
int signal_recieve = 8; //Recieving back the signal
//mapping of motor variables to arduino I/O pins
/3/int motor_right_out1 = 13; //Turning of right motor through output 1 from H bridge - clockwise - Green wire
int motor_right_out2 = 12; //Turning of right motor through output 2 from H bridge - anticlockwise - Yellow wire
int speed_control_mtr_left = 3; //Controlling RPM of left motor through PWM pins
int motor_left_out3 = 11; //Turning of left motor through output 3 from H bridge - clockwise - Orange wire
int motor_left_out4 = 10; //Turning of left motor through output 4 from H bridge - anticlockwise - Purple wire
//function for the robot to move forward
void_mForward(){
analogWrite(speed_control_mtr_right, 80); //Using an analog function to determine the speed of the right motor
// right motor turning clockwise
digitalWrite(motor_right_out1,HIGH);
digitalWrite(motor_right_out2,LOW);
analogWrite(speed_control_mtr_left, 80); //Using an analog function to determine the speed of the left motor
// left motor turning clockwise
digitalWrite(motor_left_out3,HIGH);
digitalWrite(motor_left_out4,LOW);
Serial.print("ROBOT_MOVING_FORWARD");
}
//function for robot to move backwards
void_mBackwards(){
analogWrite(speed_control_mtr_right, 80); //Using an analog function to determine the speed of the right motor
// right motor turning anticlockwise
digitalWrite(motor_right_out1,LOW);
digitalWrite(motor_right_out2,HIGH);
analogWrite(speed_control_mtr_left, 80); //Using an analog function to determine the speed of the left motor
// left motor turning anticlockwise
digitalWrite(motor_left_out3,LOW);
digitalWrite(motor_left_out4,HIGH);
Serial.print("ROBOT_MOVING_BACKWARD");
}
//function for robot turning left
void_mLeft(){
analogWrite(speed_control_mtr_right, 80); //Using an analog function to determine the speed of the right motor
// right motor turning clockwise
digitalWrite(motor_right_out1,HIGH);
digitalWrite(motor_right_out2,LOW);
analogWrite(speed_control_mtr_left, 80); //Using an analog function to determine the speed of the left motor
// left motor turning anticlockwise
digitalWrite(motor_left_out3,LOW);
digitalWrite(motor_left_out4,HIGH);
Serial.print("ROBOT_TURNING_LEFT");
}
//function for robot turning right
void_mRight(){
analogWrite(speed_control_mtr_right, 80); //Using an analog function to determine the speed of the right motor
// right motor turning anticlockwise
digitalWrite(motor_right_out1,LOW);
digitalWrite(motor_right_out2,HIGH);
analogWrite(speed_control_mtr_left, 80); //Using an analog function to determine the speed of the left motor
// left motor turning anticlockwise
digitalWrite(motor_left_out3,HIGH);
digitalWrite(motor_left_out4,LOW);
Serial.print("ROBOT_TURNING_RIGHT");
}
//function for robot to stop
void_mStop(){
analogWrite(speed_control_mtr_right, 0); //Using an analog function to determine the speed of the right motor
// right motor stop
digitalWrite(motor_right_out1,LOW);
digitalWrite(motor_right_out2,LOW);
analogWrite(speed_control_mtr_left, 0); //Using an analog function to determine the speed of the left motor
// left motor stop
digitalWrite(motor_left_out3,HIGH);
digitalWrite(motor_left_out4,LOW);
Serial.print("ROBOT_STOP");
}
void setup() {
// put your setup code here, to run once:
//defining Input & Outputs of the ultrasonic sensor
pinMode(signal_emission, OUTPUT); //Emission of wave
pinMode(signal_recieve, INPUT); //Recieving wave
//defining Inputs & Outputs of the motor system
pinMode (speed_control_mtr_right, OUTPUT); //Defining PWM pin 5 as an output
pinMode (motor_right_out1, OUTPUT); //Defining right motor cw as an output
pinMode (motor_right_out2, OUTPUT); //Defining right motor acw as an output
pinMode (speed_control_mtr_left,OUTPUT); //Defining PWM pin 3 as an output
pinMode (motor_left_out3, OUTPUT); //Defining left motor cw as an output
pinMode (motor_left_out4, OUTPUT); //Defining left motor acw as an output