Showing posts with label robotics. Show all posts
Showing posts with label robotics. Show all posts

Saturday, August 7, 2021

Project 17: Arduino 3rd Printed Biped Robot Walking & Dancing

 Project 17:  Arduino 3rd Printed Biped Robot Walking & Dancing

Main Ideas:

In this project we have used 3rd printed parts to build bipedal walking robot is a type of humanoid robot

, Arduino Nano board was used as it need small board to put it inside the robot body, and we have used the Ultra Sonic HC-SR04 Sensor connected to the robot body to be used for object detecting, so it will check if any obstacle to avoid them. We have programed the robot to do different activities i.e. walk, move back and dancing.







Circuit / Parts

·       Ultra Sonic HC-SR04 Sensor

·       4 Servo Motors

·       Arduino Nano

·       9 Volt battery

·       3rd printed parts refer to below link

https://www.myminifactory.com/object/3d-print-bob-the-biped-robot-2759#



/*CONNECTION DETIALS

   Servo1 -> pin 3 of arduino Nano Left Hip motor
   Servo2 -> pin 5 of arduino Nano Right Hip motor
   Servo4 -> pin 9 of arduino Nano Right Ankle Motor
   Servo5 -> pin 10 of arduino Nano Left Ankle Motor
*/


Code

#include <VarSpeedServo.h>

#include <Ultrasonic.h>

Ultrasonic ultrasonic(11, 12, 10000UL); // (Trig PIN,Echo PIN) Echo PIN:12 orange wire, Trig PIN=11 yellow Wire

 

//Left Hip motor 110

VarSpeedServo LeftHipmotor;

//Right Hip motor 100

VarSpeedServo RightHipmotor;

//Right Ankle Motor 90

VarSpeedServo RightAnklemotor;

//Left Ankle Motor 80

VarSpeedServo LeftAnklemotor;

/*CONNECTION DETIALS

 

   Servo1 -> pin 3 of arduino Nano Left Hip motor

   Servo2 -> pin 5 of arduino Nano Right Hip motor

   Servo4 -> pin 9 of arduino Nano Right Ankle Motor

   Servo5 -> pin 10 of arduino Nano Left Ankle Motor

*/

const int LeftHipmotorpin = 3;  // the digital pin used for the servo

const int RightHipmotorpin = 5;  // the digital pin used for the servo

const int RightAnklemotorpin = 9;  // the digital pin used for the servo

const int LeftAnklemotorpin = 10;  // the digital pin used for the servo

 

 

 

float distanceCmFront;

int ultrastoplimit = 20; // define when to stop

int ultrareducespeedlimit = 40; //define when to reduce speed

int currentStatus;

 

/*CONNECTION DETIALS

 

   Servo1 -> pin 3 of arduino Nano Left Hip motor

   Servo2 -> pin 5 of arduino Nano Right Hip motor

   Servo4 -> pin 9 of arduino Nano Right Ankle Motor

   Servo5 -> pin 10 of arduino Nano Left Ankle Motor

*/

 

 

 

int count = 0; // save the time that we started moveback

int movetype = 0; //=0 walk =1 new move , =2 ankel, =3 ankel1

void setup() {

  Serial.begin(9600);

  // put your setup code here, to run once:

  distanceCmFront = 0;

  //**Initial position of all four servo motors**//

  /*

    servo1.write(110);

    servo2.write(100);

    servo4.write(90);

    servo5.write(80);

  */

  //**inititialised**//

  set_ini();

 

}

 

void loop() {

 

  count = count + 1;

  Serial.println (count);

  if (count >= 15)

  {

    stopm();

    delay(2000);

    ScanUltrasonicFront ();

    count = 0;

    movetype = movetype + 1; //=0 walk =1 ankel , =2 new move , =3 ankel1

    if (movetype > 3)

    {

      movetype = 0;

    }

  }

  Serial.print (" current move :");

  Serial.println (movetype);

  if (movetype == 0)

  {

    walk();

  }

  else if (movetype == 1)

  {

    new_move_ankel();

    new_move();

  }

  else if (movetype == 2)

  {

    new_move();

  }

  else if (movetype == 2)

  {

    new_move_ankel1();

  }

 

}

 

void new_move()

{

 

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

  LeftHipmotor.write(145, 55, true);

  RightHipmotor.write(130, 55, true);

  delay(50);

  LeftHipmotor.write(110, 55, true);

  RightHipmotor.write(100, 55, true);

 

 

}

 

void new_move_ankel()

{

 

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

  RightAnklemotor.write(90, 50, true);

  LeftAnklemotor.write(80, 50, true);

  delay(50);

  RightAnklemotor.write(70, 50, true);

  LeftAnklemotor.write(60, 50, true);

 

}

 

 

void new_move_ankel1()

{

 

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

  RightAnklemotor.write(90, 30, true);

  LeftAnklemotor.write(80, 30, true);

  delay(50);

  RightAnklemotor.write(110, 30, true);

  LeftAnklemotor.write(60, 30, true);

 

}

 

void walk()

{

  lef_up_rev();

  delay(10);

  right_up();

  delay(10);

  set_ini();

}

 

void set_ini()

{

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

  LeftHipmotor.write(110, 50, true);

  RightHipmotor.write(100, 50, true);

  RightAnklemotor.write(90, 50, true);

  LeftAnklemotor.write(80, 50, true);

 

}

 

void stopm()

{

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

  LeftHipmotor.write(110, 50, true);

  RightHipmotor.write(100, 50, true);

  RightAnklemotor.write(90, 50, true);

  LeftAnklemotor.write(80, 50, true);

  LeftHipmotor.detach();

  RightHipmotor.detach();

  RightAnklemotor.detach();

  LeftAnklemotor.detach();

  delay (500);

}

 

 

void lef_up_rev()

{

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

 

  LeftAnklemotor.write(100, 55, true);

  RightAnklemotor.write(100, 55, true);

  delay(10);

  RightHipmotor.write(130, 55, true);

  delay(10);

 

  //RightAnklemotor.write(90, 50, true);

}

 

void lef_up()

{

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

 

  LeftAnklemotor.write(60, 55, true);

  RightAnklemotor.write(80, 55, true);

  delay(10);

  RightHipmotor.write(60, 55, true);

  delay(10);

 

  //RightAnklemotor.write(90, 50, true);

}

 

void right_up_rev()

{

  //  LeftHipmotor.write(110, 50, true);

  //  RightHipmotor.write(100, 50, true);

  //  RightAnklemotor.write(90, 50, true);

  //  LeftAnklemotor.write(80, 50, true);

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

  RightAnklemotor.write(110, 55, true);

  LeftAnklemotor.write(100, 55, true);

 

  delay(10);

  LeftHipmotor.write(90, 55, true);

  delay(10);

 

  //LeftAnklemotor.write(80, 50, true);

}

 

void right_up()

{

  LeftHipmotor.attach(LeftHipmotorpin);

  RightHipmotor.attach(RightHipmotorpin);

  RightAnklemotor.attach(RightAnklemotorpin);

  LeftAnklemotor.attach(LeftAnklemotorpin);

  //(postion , speed , sync or not)

 

  RightAnklemotor.write(70, 55, true);

  LeftAnklemotor.write(60, 55, true);

 

  delay(10);

  LeftHipmotor.write(145, 55, true);

  delay(10);

 

  //LeftAnklemotor.write(80, 50, true);

}

 

void ScanUltrasonicFront ()

{

  distanceCmFront = ultrasonic.read(CM);

  Serial.print(distanceCmFront); // CM or INC

  Serial.println(" cm Down" );

  delay(10);

 

}


Friday, June 25, 2021

Project 10: Arduino Robot Arm

 Project 10:  Arduino Robot Arm




Arm Logic 

Mode1: Robot Arm will move front down then up and move to left then open / close claw, and go back to middle and then go to right side and then open / close claw and then go back to middle again.
Mode2: Robot Arm will move front down, then pick an object and then move it up, and turn to left then open / close claw to drop the object, and move front down to pick object and then move it up and return to middle and drop it.





#include <Servo.h>
Servo middle, left, right, claw ;  // creates 4 "servo objects"
int  middlepin=11;  // orange attaches the Base servo
int  leftpin=10;   // green wire attaches the left servo
int  rightpin=9;   // gray wire attaches right servo
int  clawpin=6;   // yellow wire attaches the servo on pin 6 to the claw object
int pos = 0;    // variable to store the servo position
void setup() 
{ 
  Serial.begin(9600);
  /*
  middle.attach(leftpin);  // orange attaches the servo on pin leftpin to the middle object
  left.attach(rightpin);  // yellow wire attaches the servo on pin rightpin to the left object
  right.attach(clawpin);  // purple wire attaches the servo on pin  to the right object
  claw.attach(6);  // white wire attaches the servo on pin 6 to the claw object

   middle.attach(middlepin);  // orange attaches the servo on pin leftpin to the middle object
  left.attach(leftpin);  // yellow wire attaches the servo on pin rightpin to the left object
  right.attach(rightpin);  // purple wire attaches the servo on pin 9 to the right object
  claw.attach(clawpin);  // white wire attaches the servo on pin 6 to the claw object
*/
 
  // hand();
  //slowmove();
//right_left();
// right_left_Up();
 //right_left_front();
 //right_left_Up();

 //base();

 //right_left_front_dwon();
 //right_left_Up();
 //basefslow();
//newmove();
 
} 

void slowmove()
{
   left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
  right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
  //left.write(180); 
  //right.write(0);  
 
  
  for (pos = 45; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    left.write(pos); 
   right.write(180-pos);  
    
    delay(15);                       // waits 15ms for the servo to reach the position
  }

   delay (50);
  left.detach();
  right.detach(); 
  
//  
//  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
//    myservo.write(pos);              // tell servo to go to position in variable 'pos'
//    delay(15);                       // waits 15ms for the servo to reach the position
//  }
}

void basefslow()
{
   // middle
  // right trun =0
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo object
   for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(90-pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   delay(50); 
  middle.detach();
   hand();
   
  //back middle =90
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
  for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 // middle.write(90);  
  //delay (500);
  delay(50); 
  middle.detach();
 hand();
 
   // left trun=180
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo object
   for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  //middle.write(180);  
  delay (50);
  middle.detach();
 hand();
 
  //back middle =90
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
   for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(180-pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
//  middle.write(90);  
  delay (50);
  middle.detach();
   hand();
}

void newmove()
{
  right_left_front_dwon();
  hand();
  right_left_Up();
   // middle
  // left trun=180
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo object
   for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  //middle.write(180);  
  delay (50);
  middle.detach();
  
 hand();

 right_left_front_dwon();
 
  hand();
  right_left_Up();
  
  //back middle =90
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
   for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
     middle.write(180-pos);      
    delay(15);                       // waits 15ms for the servo to reach the position
  }
//  middle.write(90);  
  delay (50);
  middle.detach();
   hand();
 
  
}


void base()
{
   // middle
  // right trun =0
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo object
   middle.write(0);  
  delay (500);
  middle.detach();
   hand();
   
  //back middle =90
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
  middle.write(90);  
  delay (500);
  middle.detach();
 hand();
 
   // left trun
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo object
  middle.write(180);  
  delay (500);
  middle.detach();
 hand();
  //back middle 
  middle.attach(middlepin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
  middle.write(90);  
  delay (500);
  middle.detach();
   hand();
}
void right_left()
{
  left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
  right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
  left.write(0); 
  right.write(90);  
  delay (500);
  left.detach();
  right.detach(); 
}


void right_left_front_dwon()
{
  left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
  right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
  left.write(50); 
  right.write(15);
 // left.write(180); 
//  right.write(90);    
  delay (500);
  left.detach();
  right.detach();
  delay (500); 
}

void right_left_front()
{
  left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
  right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
  left.write(180); 
  right.write(45);  
  delay (500);
  left.detach();
  right.detach(); 
}
void right_left_Up()
{
  left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
  right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
  left.write(0); 
  right.write(90);    // tell servo to go to position in variable 'pos'
  delay (500);
  left.detach();
  right.detach(); 
  delay (500);
}

void hand()
{
  // claw
  // open
  
  claw.attach(clawpin);  // attaches the servo on pin 9 to the servo object
  claw.write(0);  
  delay (700);
  claw.detach();
  
  //close
  
  claw.attach(clawpin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
  claw.write(90);  
  delay (1000);
  claw.detach();

   delay (500);
   
}
void fullstep1()
{
left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
right.attach(rightpin);  // attaches the servo on pin 9 to the servo object

left.write(60); 
//delay (300);
right.write(60);   // tell servo to go to position in variable 'pos'
delay (500);
left.detach();
right.detach(); 

// claw

// open
claw.attach(clawpin);  // attaches the servo on pin 9 to the servo object
claw.write(0);  
delay (500);
claw.detach();

//close
claw.attach(clawpin);  // attaches the servo on pin 9 to the servo objectclawV.write(90);  
claw.write(90);  
delay (500);
claw.detach();

left.attach(leftpin);  // attaches the servo on pin 9 to the servo object
left.write(135);  
delay (1500);
left.detach();

right.attach(rightpin);  // attaches the servo on pin 9 to the servo object
right.write(0);  
delay (1500);
right.detach();
}

void loop() 
{ 
 //newmove();
 //delay (10000);
}



Project 9 Arduino Obstacle Avoiding Robot

Project 9:  Arduino Obstacle Avoiding Robot

Main Ideas:

Obstacle Avoiding Robot is an intelligent device which can automatically sense the obstacle in front of it and avoid them by turning itself in another direction. This design allows the robot to navigate in unknown environment by avoiding collisions, which is a primary requirement for any autonomous mobile robot. An obstacle avoiding robot is a fully autonomous robot which can be able to avoid any obstacle which it face when it move, to sense the obstacle in front we are using the Ultra Sonic HC-SR04 Sensor . Also the Robot can sense the obstacle in back side using Infrared sensor (IR) , so while moving back if there is an obstacle robot will stop and we will try to change the path to avoid the  obstacle.






Drive Logic 

As high level logic used to drive the robot , first robot will check the front obstacle using Ultra Sonic HC-SR04 Sensor if no obstacle in 50 cm range the robot will run in high speed and keep looking for front obstacle, if an obstacle exist in range 35 – 50 cm then the robot will continue to move forward but will reduce the speed , so this will later allow robot to stop smoothly before hitting the  obstacle, if the obstacle less than 35 cm robot will stop moving, then robot will move back and turn left and check the front obstacle to define the speed (high / low) and then move forward.

While robot moving back or turning left or right we have used to Infrared sensor (IR) to make sure robot will not hit back obstacle while moving back or turning.

Narrow path Maneuver

Also we have other mode to handle the narrow path , where the front obstacle is less than 35 , in this case as per our logic explained above robot will try to move back but , if there is an obstacle back then what robot should do ? here we have other logic , robot will move forward but in slow spend for 10 cm then try to turn left , and robot will keep trying this maneuver until robot find a big space to move then robot will go back to the normal logic explained above.




/*
 * Main Ideas: 
Obstacle Avoiding Robot is an intelligent device which can automatically sense the obstacle in front of it 
and avoid them by turning itself in another direction. This design allows the robot to navigate in unknown environment 
by avoiding collisions, which is a primary requirement for any autonomous mobile robot. An obstacle avoiding robot is a fully autonomous 
robot which can be able to avoid any obstacle which it face when it move, to sense the obstacle in front we are using the Ultra Sonic HC-SR04 Sensor 
. Also the Robot can sense the obstacle in back side using Infrared sensor (IR) , so while moving back if there is an obstacle robot will 
stop and we will try to change the path to avoid the  obstacle.

 */
#include <Ultrasonic.h>

Ultrasonic ultrasonicFront(12, 13, 20000UL); // (Trig PIN,Echo PIN) Echo PIN:13 yellow wire, Trig PIN=12 Green Wire
float distanceCmFront;
int ultrastoplimit=20; // define when to stop 
int ultrareducespeedlimit=40; //define when to reduce speed
int currentStatus;

 //move controls
float timeToMoveBack5CM=500;
float timeToMoveBack2CM=250;
float timeToTrunRight5CM=200;
float timeToTrunLeft5CM=200;
float timeToMoveForward10CM=200;

int BackObstacleL;
int IRBL = 8;    // Back LeftIR sensor output pin Blue wire connected to digital pin 53

//Uno PWM pins 3, 5, 6, 9, 10, and 11

//New Motors Pins
//Motor New A Right Side
#define enA 6 //PWM Gray  enable A 
#define in1 4 //Purple IN1
#define in2 7 //Blule IN2

//Motor New B Left Side
#define enB 5 //PWM Orange Enable B
#define in3 2 // Green IN3
#define in4 3 // Blule IN4

int speedMotorA; // control the speed of New motor -right side 
int speedMotorB;// control the speed of New motor -left side

void setup() {
  // initialize serial communication at 9600 bits per second:
 // Serial.begin(9600);
 // Serial.println("start");
// put your setup code here, to run once:
distanceCmFront=0;
currentStatus=1;
//set IR Pin As Input
  pinMode(IRBL, INPUT);
  
//Set Motor A left side pins as output so we can contol the motor
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

 //Motor B New right side pins as output so we can contol the motor
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  //set initial speed for the motors
  speedMotorA=190; // control the speed of motor -left side 
  speedMotorB=190;// control the speed of motor -right side
}

void loop() {
  // put your main code here, to run repeatedly:
  MoveLogic();
  //moveForward();
  //moveBackword5CM();
  //TrunRight5CM();
  //moveForwardone();
}

void MoveLogic()
{
  getStatus();
  switch (currentStatus)
    {
    case 1:
      // // no obstacle in front or back move forward
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
      moveForward();
      break;
    case 2:
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
      // // near front obstacle reduce speed
      moveForwardReduceSpeed();
      break;
    case 3:
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
       /*
      Physical Locations :front obstacle
      Logic: Stop move back trun to  right
      */
      stopmove();
      moveBackword5CM();
      TrunRight5CM();
      break;
    case 4:
    /*
    Physical Locations :front obstacle 30 far and there is Back Obstacle
    Logic:  move slowly to front trun to left
    */
    MoveForward10CM();
    TrunLeft5CM();
    break;  
    default: break; // do nothing
    } // switch (param)
}

void getStatus()
{
  ScanUltrasonicFront();
  if (  distanceCmFront > ultrareducespeedlimit) 
  {
    // no obstacle in front or back move forward
    currentStatus=1;
  }
  else if ( ((distanceCmFront > ultrastoplimit && distanceCmFront <= ultrareducespeedlimit))) 
  {
    // near front obstacle reduce speed
    currentStatus=2;
  }

  else if ( (distanceCmFront <= ultrastoplimit)) 
  {
    
     /*
    Physical Locations :front obstacle 30 far and there is Back Obstacle
    Logic:  move sloley to front trun to left
    */
    CheckIRBack();
    //Back Obstacle 
    if (BackObstacleL == LOW) 
    {
    currentStatus=4; //move forward 10 cm and trun to left
    }
    else
    {
      /*
    Physical Locations :front obstacle
    Logic: Stop move back trun to  right
    */
    currentStatus=3; //Stop move back trun to right
    }
    
  }
 
  //Serial.println(currentStatus);
}

void ScanUltrasonicFront ()
{

  distanceCmFront=ultrasonicFront.read(CM);
  //Serial.print(distanceCmFront); // CM or INC
  //Serial.println(" cm Down" );
  delay(50);
}

void ResetSpeedMotors()
{
  speedMotorA=220; // control the speed of motor -rigt side 
  speedMotorB=220;// control the speed of motor -left side
}

void ReduceSpeed()
{
  //set speed
  speedMotorA=150; // control the speed of motor -rigt side 
  speedMotorB=150;// control the speed of motor -left side
}

void moveForwardone()
  {
    ResetSpeedMotors(); 
   
    // run motor on right side       
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);     
   }
   
void moveForward()
  {
    ResetSpeedMotors(); 
   // run motor on left side   
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);  
    // run motor on right side       
   analogWrite(enB, speedMotorB);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);     
   }
void moveForwardReduceSpeed()
{
  ReduceSpeed(); 
   // run motor on left side   
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);  
    // run motor on right side       
   analogWrite(enB, speedMotorB);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);     
}

void stopmove()
 {
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); 
    analogWrite(enB, 0);

    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);  
    analogWrite(enA, 0);
          
    delay (100);
 }

 void MoveForward10CM()
  { 
  
    ReduceSpeed(); 
      unsigned long moveForward10CMStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveForward10CMStartTime)<(timeToMoveForward10CM))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
        // run motor on left side   
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);  
        // run motor on right side       
       analogWrite(enB, speedMotorB);
       digitalWrite(in3, HIGH);
       digitalWrite(in4, LOW);     
      }
  }


 void TrunLeft5CM()
  { 
    CheckIRBack();
    ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToTrunLeft5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
        // trun on right side front
        analogWrite(enA, speedMotorA);
         digitalWrite(in1, LOW);
         digitalWrite(in2, HIGH); 
      
      // turn on motor B left side revers 
       analogWrite(enB, speedMotorB); //analogWrite(enB, 0);// analogWrite(enB, speedMotorB);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        CheckIRBack();
      }
     
       // stop after moveback 
       stopmove();
   }


void TrunRight5CM()
  { 
    CheckIRBack();
    ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToTrunRight5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
         // turn on motor A left in revers
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);  
      
       // run motor on right side  
        analogWrite(enB, speedMotorB);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);

         CheckIRBack();
      }
       // stop after moveback 
         //delay (400);
       stopmove();
   }


 void moveBackword5CM()
    {
       CheckIRBack();

      ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToMoveBack5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToMoveBack10CM (1.1 seconds) has elapsed
      {
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);  
      
      // turn on motor B
        analogWrite(enB, speedMotorB);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);

        CheckIRBack();// check for obstacles using IR sensors, value=1 no obstacles , value==0 obstacles exists
      }
       // stop after moveback 
       stopmove();
    }

void CheckIRBack()
{
   // HIGH MEANS NO OBSTACLE / LOW OBSTACLE
  //BackObstacleR = digitalRead(IRBR);
  BackObstacleL = digitalRead(IRBL);
 if (BackObstacleL == LOW) {
    //Serial.println(" Back Obstacle Left STOP");
  }
 else {
   //Serial.println("Back Obstacle Left Clear ");
  }
}