donmike25

edited

Feb 21st, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. int motor1Pin1 = 11; // pin 7 on L293D IC
  3. int motor1Pin2 = 10; // pin 8 on L293D IC
  4. int motor2Pin1 = 9; // pin 12 on L293D IC
  5. int motor2Pin2 = 8; // pin 13 on L293D IC
  6. int EnPinL = 2; // pin 12 on L293D IC
  7. int EnPinR = 3; // pin 13 on L293D IC
  8. int Sensor_1 = 0;
  9. int Sensor_2 = 0;
  10. int pirPin = 13;
  11. int ledPir = 4;
  12. SoftwareSerial mySerial(6, 7);
  13. char mode = -1;
  14.  
  15. int timeflag = 0;
  16. int starttime = 0;
  17.  
  18. void setup() {
  19. // sets the pins as outputs:
  20. Serial.begin(9600);
  21. mySerial.begin(9600);
  22. pinMode(motor1Pin1, OUTPUT);
  23. pinMode(motor1Pin2, OUTPUT);
  24. pinMode(motor2Pin1, OUTPUT);
  25. pinMode(motor2Pin2, OUTPUT);
  26. pinMode(EnPinL, OUTPUT);
  27. pinMode(EnPinR, OUTPUT);
  28. pinMode(pirPin, INPUT);
  29. pinMode(ledPir, OUTPUT);
  30.  
  31. digitalWrite(ledPir, LOW);
  32. }
  33.  
  34. void motorback() {
  35. digitalWrite(motor1Pin1, LOW);
  36. digitalWrite(motor1Pin2, HIGH);
  37. digitalWrite(motor2Pin1, HIGH);
  38. digitalWrite(motor2Pin2, LOW);
  39. }
  40.  
  41. void motorfront() {
  42. digitalWrite(motor1Pin1, HIGH);
  43. digitalWrite(motor1Pin2, LOW);
  44. digitalWrite(motor2Pin1, LOW);
  45. digitalWrite(motor2Pin2, HIGH);
  46. }
  47.  
  48. void motorleft()
  49. {
  50. digitalWrite(motor1Pin1, LOW);
  51. digitalWrite(motor1Pin2, LOW);
  52. digitalWrite(motor2Pin1, LOW);
  53. digitalWrite(motor2Pin2, HIGH);
  54. }
  55.  
  56. void motorright() {
  57. digitalWrite(motor1Pin1, HIGH);
  58. digitalWrite(motor1Pin2, LOW);
  59. digitalWrite(motor2Pin1, LOW);
  60. digitalWrite(motor2Pin2, LOW);
  61. }
  62.  
  63. void motorstop() {
  64. digitalWrite(motor1Pin1, LOW);
  65. digitalWrite(motor1Pin2, LOW);
  66. digitalWrite(motor2Pin1, LOW);
  67. digitalWrite(motor2Pin2, LOW);
  68. }
  69.  
  70. void readsensor() {
  71.  
  72. }
  73.  
  74. char getChar() {
  75. while (mySerial.available() == 0);
  76. return ((char) mySerial.read());
  77. }
  78.  
  79. int getSerial() {
  80. if (mySerial.available()) {
  81. return getChar();
  82. }
  83. return -1;
  84. }
  85.  
  86. void loop() {
  87. delay(100);
  88.  
  89. char cmd = getSerial();
  90. //char cmd = getChar();
  91. Serial.print(cmd);
  92. if (cmd == 'M' || cmd == 'P' || cmd == 'Z') {
  93. mode = cmd;
  94. }
  95. if (mode == 'M') {
  96. //remote();
  97. Serial.println("Remote|");
  98. //remote();
  99. }
  100.  
  101. if (mode == 'P') {
  102. //patrol();
  103. Serial.println("Patrol|");
  104. delay(200);
  105.  
  106.  
  107.  
  108. patrol();
  109.  
  110.  
  111.  
  112. }
  113.  
  114. if (mode == 'Z') {
  115. //siram();
  116. Serial.println("Siram|");
  117. //siram();
  118. }
  119. }
  120.  
  121. void patrol() {
  122.  
  123. if(timeflag == 0){
  124. starttime = millis();
  125. Serial.print(starttime);
  126. timeflag = 1;
  127. }
  128.  
  129. readsurface();
  130. Serial.print(millis());
  131. Serial.print("/n");
  132. if(millis() >= (starttime + 10000)){
  133. scanPIR();
  134. timeflag = 0;
  135. }
  136. }
  137.  
  138. void scanPIR() {
  139. motorstop();
  140. delay(2000);
  141. if (digitalRead(pirPin) == HIGH) {
  142. Serial.println("Motion Detected");
  143. digitalWrite(ledPir, HIGH);
  144. delay(1000);
  145. } else {
  146. Serial.println("No Motion");
  147. digitalWrite(ledPir, LOW);
  148. }
  149.  
  150. }
  151.  
  152. void readsurface() {
  153. Sensor_2 = analogRead(A0);
  154. Sensor_1 = analogRead(A1);
  155.  
  156. if (Sensor_1 < 300 && Sensor_2 > 300) {
  157. Serial.println("Moving Backward");
  158. motorback();
  159. delay(1000);
  160. Serial.println("Right Turn");
  161. motorright();
  162. delay(1000);
  163. } else if (Sensor_2 < 300 && Sensor_1 > 300) {
  164. Serial.println("Moving Backward");
  165. motorback();
  166. delay(1000);
  167. Serial.println("Left Turn");
  168. motorleft();
  169. delay(1000);
  170. } else {
  171. Serial.println("Moving Forward");
  172. motorfront();
  173. }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment