donmike25

Donmike IR

Feb 8th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. int motor1Pin1 = 11; // pin 7 on L293D IC
  2. int motor1Pin2 = 10; // pin 8 on L293D IC
  3. int motor2Pin1 = 9; // pin 12 on L293D IC
  4. int motor2Pin2 = 8; // pin 13 on L293D IC
  5.  
  6.  
  7. int EnPinL = 6; // pin 12 on L293D IC
  8. int EnPinR = 7; // pin 13 on L293D IC
  9.  
  10. int Sensor_1 = 0;
  11. int Sensor_2 = 0;
  12.  
  13. int pirPin = 13;
  14.  
  15. int ledPir = 4;
  16.  
  17. String readBT;
  18.  
  19. void setup() {
  20. // sets the pins as outputs:
  21. Serial.begin(9600);
  22. pinMode(motor1Pin1, OUTPUT);
  23. pinMode(motor1Pin2, OUTPUT);
  24. pinMode(motor2Pin1, OUTPUT);
  25. pinMode(motor2Pin2, OUTPUT);
  26.  
  27. pinMode(EnPinL, OUTPUT);
  28. pinMode(EnPinR, OUTPUT);
  29.  
  30. pinMode(pirPin, INPUT);
  31.  
  32. pinMode(ledPir, OUTPUT);
  33.  
  34. digitalWrite(ledPir,LOW);
  35.  
  36. }
  37.  
  38.  
  39. void loop() {
  40.  
  41.  
  42. Sensor_2 = analogRead(A0);
  43. Sensor_1 = analogRead(A1);
  44. while (Serial.available()){
  45.  
  46. delay (3);
  47. char c = Serial.read();
  48. readBT += c;
  49. }
  50.  
  51. if (readBT.length() >0) {
  52. Serial.println(readBT);
  53. if (readBT == "2ON")
  54. {
  55. digitalWrite(EnPinL, HIGH);
  56. digitalWrite(EnPinR, HIGH);
  57. }
  58. if (readBT == "2OFF")
  59. {
  60. digitalWrite(EnPinL, LOW);
  61. digitalWrite(EnPinR, LOW);
  62.  
  63. }
  64. readBT ="";}
  65.  
  66. if (digitalRead(pirPin) == HIGH){
  67. Serial.println("Motion Detected");
  68. digitalWrite(ledPir,HIGH);
  69.  
  70. }
  71.  
  72. if (digitalRead(pirPin) == LOW){
  73. Serial.println("No Motion");
  74. digitalWrite(ledPir,LOW);
  75. }
  76. if (Sensor_1 < 300 && Sensor_2 > 300)
  77. {
  78. Serial.println("Moving Backward");
  79. digitalWrite(motor1Pin1, LOW);
  80. digitalWrite(motor1Pin2, HIGH);
  81. digitalWrite(motor2Pin1, HIGH);
  82. digitalWrite(motor2Pin2, LOW);
  83. delay(1000);
  84. Serial.println("Right Turn");
  85. digitalWrite(motor1Pin1, HIGH);
  86. digitalWrite(motor1Pin2, LOW);
  87. digitalWrite(motor2Pin1, LOW);
  88. digitalWrite(motor2Pin2, LOW);
  89. delay(1000);
  90. }
  91.  
  92. else if (Sensor_2 <300 && Sensor_1 > 300)
  93. {
  94. Serial.println("Moving Backward");
  95. digitalWrite(motor1Pin1, LOW);
  96. digitalWrite(motor1Pin2, HIGH);
  97. digitalWrite(motor2Pin1, HIGH);
  98. digitalWrite(motor2Pin2, LOW);
  99. delay(1000);
  100. Serial.println("Left Turn");
  101. digitalWrite(motor1Pin1, LOW);
  102. digitalWrite(motor1Pin2, LOW);
  103. digitalWrite(motor2Pin1, LOW);
  104. digitalWrite(motor2Pin2, HIGH);
  105. delay(1000);
  106. }
  107.  
  108. else
  109. {
  110. Serial.println("Moving Forward");
  111. digitalWrite(motor1Pin1, HIGH);
  112. digitalWrite(motor1Pin2, LOW);
  113. digitalWrite(motor2Pin1, LOW);
  114. digitalWrite(motor2Pin2, HIGH);
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment