Advertisement
Eng-Mohamed-Essam

Untitled

Aug 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <CytronMotorDriver.h> // Including the Cytron motor driver library
  2. #include "CytronMotorDriver.h"
  3. #include <Servo.h> // Including servo motor's Library
  4. #include <LiquidCrystal.h> // Including the LCD Library
  5. //Configuring the motors
  6. CytronMD motor1 (PWM_DIR, 3, 23); // Declaring the pins for PWM (SPEED) and DIR (DIRECTION) for front motor on the right
  7. CytronMD motor2 (PWM_DIR, 4, 24); // Front motor on the left
  8. CytronMD motor3 (PWM_DIR, 10, 26); // Back motor on the left
  9. CytronMD motor4 (PWM_DIR, 9, 27); // Back motor on the right
  10. int Position = 90;
  11. int servoPin = 13;
  12. int echoPin = 12; // Echo pin of the UltraSnonic Sensor
  13. int TrigPin = 11; // Trigger Pin of the UltraSonic Sensor
  14. Servo Direction; // Creating a servo object
  15. float PingTime; // Time for ping to travel and come back
  16. float speedofsound = 1234.8 ; // Speed of sound in Kilometers Per hour
  17. float Distance;  
  18. int CustomKey = 100;
  19. int DistanceLeft, DistanceRight; // Distance left and right for servomotor
  20. LiquidCrystal lcd (22, 28, 30, 31, 32, 33); // Defining the pins for the LCD Screen
  21. int MotionPin = 25; // defining pin taking input from motion detector
  22. int MotionState = 0; // Giving an initial value to the Motion State
  23. int HoldState; // This var holds the Automatic State till it's code finishes compiling or interrupted by a command from the CU
  24. float GetDistance() // fuction used to get distance from the obstacle to the US-Sensor
  25. {
  26.   digitalWrite(TrigPin, LOW); // Setting trigger pin for the US-Sensor to low value
  27.   delayMicroseconds(2000); // Pausing for a bit to make the signal settle
  28.   digitalWrite(TrigPin, HIGH); // Setting the trigger pin for the US-Sensor to High Value
  29.   delayMicroseconds(15); // pausing for a small amout of time
  30.   digitalWrite(TrigPin, LOW); //Setting the trigger pin for the US-Sensor to Low again
  31.  
  32.   PingTime = pulseIn(echoPin , HIGH); // Measure Ping Time at echopin in MicroSeconds    
  33.   PingTime = PingTime / 1000000.; // Converting ping time to seconds as it's in Microseconds
  34.   PingTime = PingTime / 3600.; // Converting Ping time to Hours
  35.  
  36.   float result = speedofsound * PingTime; // Distance in Kilometers
  37.   result = result / 2; // The Distance from the Sensor to target
  38.   result = result * 100000; // Convert the distance from kilometers to Centemeters
  39.   return result; // returning the value of the result distance
  40. }
  41. int F = 0, B = 0, R = 0, L = 0, M = 0, A = 0;
  42. double Pulse; // Declaring the pulses sent by the metal detector
  43. void setup() {
  44.   // put your setup code here, to run once:
  45.   Serial.begin(9600);
  46.   Direction.attach(servoPin);
  47.   pinMode(TrigPin, OUTPUT); // Outputting the value Read
  48.   pinMode(echoPin, INPUT); // Reading From the Sensor
  49.   lcd.begin(16,2);
  50.   lcd.print("Speed: "); // Displaying the word Speed on the first line
  51.   pinMode(MotionPin, INPUT); // Declaring the Motion pin as an input
  52.   pinMode(8, INPUT); // Declaring pin 8 as an input for the metal detector
  53.   pinMode(7, OUTPUT); // Declaring pin 5 as an output from the metal detector
  54. }
  55.  
  56. void loop() {
  57.   // put your main code here, to run repeatedly:
  58.   MotionState = digitalRead(MotionPin); // Reading if motion accurs
  59.   if (MotionState == HIGH){ // This activates the Automatic mode
  60.     HoldState = HIGH; // Holding the Automatic State
  61.   }
  62.   if (HoldState == HIGH || M == HIGH);{
  63.     Distance = GetDistance();
  64.     lcd.clear();
  65.     lcd.setCursor(0, 1);
  66.     lcd.print(CustomKey);
  67.     if (Distance > 2){
  68.       motor1.setSpeed(CustomKey); // motor 1 runs forward with "CustomKey" Speed
  69.       motor2.setSpeed(CustomKey); // motor 2 runs forward with "CustomKey" Speed
  70.       motor3.setSpeed(CustomKey);
  71.       motor4.setSpeed(CustomKey);
  72.     }
  73.     else if (Distance <= 2){  
  74.       motor1.setSpeed(0); // Stop motor 1
  75.       motor2.setSpeed(0); // Stop motor 2
  76.       motor3.setSpeed(0); // stop motor 3
  77.       motor4.setSpeed(0); // Stop motor 4
  78.       Position = 20;
  79.       Direction.write(Position); // Giving the Servo the wanted position
  80.       Distance = GetDistance();
  81.       DistanceLeft = Distance ;
  82.       Position = 170;
  83.       Direction.write(Position);
  84.       Distance = GetDistance();
  85.       DistanceRight = Distance;
  86.       }
  87.       if (DistanceRight > DistanceLeft){
  88.         motor1.setSpeed(-50); // setting motor 1 to half speed and moving backwards
  89.         motor2.setSpeed(100); // setting motor 2 to normal speed and moving forward
  90.         motor3.setSpeed(100); // setting motor 3 to normal speed and moving forward
  91.         motor4.setSpeed(-50); // setting motor 4 to half speed and moving backwards
  92.         delay(500);
  93.       }
  94.       else {
  95.         motor1.setSpeed(100);
  96.         motor2.setSpeed(-50);
  97.         motor3.setSpeed(-50);
  98.         motor4.setSpeed(100);
  99.         delay(500); // delaying the robot for a bit
  100.       }
  101.       digitalWrite(7, HIGH); // Giving a High value to the pin 7 connected to the metal detector sensor circuit
  102.       delayMicroseconds(5000); // Giving a delay before the low value
  103.       digitalWrite(7, LOW); // Giving a Low pulse from the same pin
  104.       delayMicroseconds(100);
  105.       Pulse = pulseIn(8, HIGH, 5000); // Give pulses to pin 8 with a T of 5 sec
  106.       if (Pulse > 920) // if the sensor detects metal
  107.       {
  108.         digitalWrite(6, HIGH); /* Giving an Active High signal to a NO Relay
  109.         that provides 24 Voltages to a pneumatic mechanical  cylinder
  110.         (Double Acting Cylinder ) that pushes the Flag Up */
  111.       }
  112.   }
  113.   // Code for manual Control
  114.   if (Serial.available() > 0){
  115.     if (Serial.read() == '1'){
  116.       F == HIGH;
  117.     }
  118.     else if (Serial.read() == '2'){
  119.       B == HIGH;
  120.     }
  121.     else if (Serial.read() == '3'){
  122.       R == HIGH;
  123.     }
  124.     else if (Serial.read() == '4'){
  125.       L == HIGH;
  126.     }
  127.     else if (Serial.read() == '5'){
  128.       M = HIGH;
  129.     }
  130.     else if (Serial.read() == '6'){
  131.       A = HIGH;
  132.     }
  133.     else if (Serial.read() == '7'){
  134.       CustomKey = Serial.read(); // Reading the next serial value which is the speed
  135.     }
  136.     else if (Serial.read() == "10"){
  137.       F = LOW;
  138.     }
  139.     else if (Serial.read() == "20"){
  140.       B = LOW;
  141.     }
  142.     else if (Serial.read() == "30"){
  143.       R = LOW;
  144.     }
  145.     else if (Serial.read() == "40"){
  146.       L = LOW;
  147.     }
  148.     else if (Serial.read() == "50"){
  149.       M = LOW;
  150.     }
  151.     else if (Serial.read() == "60"){
  152.       A = LOW;
  153.     }
  154.    
  155.   }
  156.   if (F == HIGH){ // Code for moving forward Manually
  157.     motor1.setSpeed(CustomKey);
  158.     motor2.setSpeed(CustomKey);
  159.     motor3.setSpeed(CustomKey);
  160.     motor4.setSpeed(CustomKey);
  161.   }
  162.   else if (B == HIGH){ // Code for moving backwards manually
  163.     motor1.setSpeed(-CustomKey);
  164.     motor2.setSpeed(-CustomKey);
  165.     motor3.setSpeed(-CustomKey);
  166.     motor4.setSpeed(-CustomKey);
  167.   }
  168.   else if (R == HIGH){ // Code for rotating right manually
  169.     motor1.setSpeed(-CustomKey + (CustomKey/2)); // setting it to be half speed
  170.     motor2.setSpeed(CustomKey);
  171.     motor3.setSpeed(CustomKey);
  172.     motor4.setSpeed(-CustomKey + (CustomKey / 2)); // Setting this motor for half speed chosen
  173.   }
  174.   else if (L == HIGH){ //Code for rotating left manually
  175.     motor1.setSpeed(CustomKey);
  176.     motor2.setSpeed(-CustomKey + (CustomKey)/2);
  177.     motor3.setSpeed(-CustomKey + (CustomKey)/2);
  178.     motor4.setSpeed(CustomKey);
  179.   }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement