Advertisement
safwan092

Untitled

Feb 17th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <MuVisionSensor.h>
  3. #include <Wire.h>
  4. #include <SoftwareSerial.h>
  5. #define I2C_MODE
  6. #define MU_ADDRESS 0x60
  7.  
  8.  
  9. MuVisionSensor Mu(MU_ADDRESS);
  10. #define RX 7
  11. #define TX 8
  12.  
  13. SoftwareSerial ss(RX, TX);
  14.  
  15. int Sensor1 = A0; // connect ir sensor module to Arduino pin 7
  16. int Sensor2 = 9; // connect ir sensor module to Arduino pin 9
  17. int Sensor3 = 10; // connect ir sensor module to Arduino pin 10
  18. int Sensor4 = 11; // connect ir sensor module to Arduino pin 11
  19. int Move = 3; // connect ir sensor module to Arduino pin 11
  20. int Speed1 = 4;
  21. int Speed2 = 5;
  22. int Stop = 6;
  23. int data1, data2, data3, data4;
  24. void setup()
  25. {
  26. Serial.begin(9600); // Init Serila at 115200 Baud
  27. ss.begin(9600);
  28. Serial.println("Serial Working"); // Test to check if serial is working or not
  29. pinMode(13, OUTPUT);
  30. digitalWrite(13, 0);
  31. pinMode(Sensor1, INPUT); // IR Sensor pin INPUT
  32. pinMode(Sensor2, INPUT); // IR Sensor pin INPUT
  33. pinMode(Sensor3, INPUT); // IR Sensor pin INPUT
  34. pinMode(Sensor4, INPUT); // IR Sensor pin INPUT
  35. pinMode(Move, OUTPUT ); // IR Sensor pin INPUT
  36. pinMode(Speed1, OUTPUT ); // IR Sensor pin INPUT
  37. pinMode(Speed2, OUTPUT ); // IR Sensor pin INPUT
  38. pinMode(Stop, OUTPUT ); // IR Sensor pin INPUT
  39. digitalWrite (Move , LOW);
  40. digitalWrite (Speed1 , LOW);
  41. digitalWrite (Speed2 , LOW);
  42. digitalWrite (Stop , LOW);
  43. /////////////////////////Cam
  44. uint8_t err = MU_ERROR_FAIL;
  45. Wire.begin();
  46. err = Mu.begin(&Wire);
  47. if (err == MU_OK) {
  48. Serial.println("MU initialized.");
  49. } else {
  50. do {
  51. Serial.println("fail to initialize MU! Please check protocol "
  52. "version or make sure MU is working on the "
  53. "correct port with correct mode.");
  54. delay(5000);
  55. } while (1);
  56. }
  57. // enable vision: number card
  58. Mu.VisionBegin(VISION_COLOR_RECOGNITION);
  59. Mu.write(VISION_COLOR_RECOGNITION, kXValue, 100); // set detect region center x value(0~100)
  60. Mu.write(VISION_COLOR_RECOGNITION, kYValue, 100); // set detect region center y value(0~100)
  61. Mu.write(VISION_COLOR_RECOGNITION, kWidthValue, 50); // set detect region center width value(0~100)
  62. Mu.write(VISION_COLOR_RECOGNITION, kHeightValue, 50); // set detect region center height value(0~100)
  63. Mu.CameraSetAwb(kLockWhiteBalance);
  64.  
  65. }
  66.  
  67. void loop()
  68. {
  69. int sensorStatus1 = digitalRead(Sensor1);
  70. int sensorStatus2 = digitalRead(Sensor2);
  71. int sensorStatus3 = digitalRead(Sensor3);
  72. int sensorStatus4 = digitalRead(Sensor4);
  73. data1 = sensorStatus1;
  74. data2 = sensorStatus2;
  75. data3 = sensorStatus3;
  76. data4 = sensorStatus4;
  77.  
  78. Serial.print(sensorStatus1);
  79. Serial.print(" ");
  80. Serial.print(sensorStatus2);
  81. Serial.print(" ");
  82. Serial.print(sensorStatus3);
  83. Serial.print(" ");
  84. Serial.println(sensorStatus4);
  85. sendTxData();
  86.  
  87. if (sensorStatus1 == 0 || sensorStatus2 == 0 || sensorStatus3 == 0 || sensorStatus4 == 0 )
  88. {
  89. colorswitch();
  90. // if the pin is high turn off the onboard Led
  91. digitalWrite(Move, HIGH); // LED LOW
  92. Serial.println("Motion Start!"); // print Motion Detected! on the serial monitor window
  93. }
  94. else
  95. {
  96. //else turn on the onboard LED
  97. digitalWrite(Move, LOW); // LED High
  98. Serial.println("Motion Stop!"); // print Motion Ended! on the serial monitor window
  99. }
  100.  
  101.  
  102. }
  103.  
  104.  
  105. void colorswitch() {
  106.  
  107. if (Mu.GetValue(VISION_COLOR_RECOGNITION, kStatus)) { // update vision result and get status, 0: undetected, other: detected
  108. switch (Mu.GetValue(VISION_COLOR_RECOGNITION, kLabel)) { // get vision result: label value
  109.  
  110. case MU_COLOR_GREEN:
  111. Serial.println("green");
  112. Serial.println("Speed [1] + Motion Start!");
  113. digitalWrite (Speed1 , HIGH);
  114. digitalWrite (Speed2 , LOW);
  115. digitalWrite (Stop , LOW);
  116. digitalWrite(13, 1);
  117. delay(500);
  118. break;
  119. case MU_COLOR_PURPLE:
  120. Serial.println("purple");
  121. Serial.println("Motion Stop!!!! [Color Detected]");
  122. digitalWrite (Speed1 , LOW);
  123. digitalWrite (Speed2 , LOW);
  124. digitalWrite (Stop , HIGH);
  125. delay(500);
  126. digitalWrite (Speed1 , LOW);
  127. digitalWrite (Speed2 , LOW);
  128. digitalWrite (Stop , LOW);
  129. digitalWrite(13, 0);
  130. delay(500);
  131. break;
  132. case MU_COLOR_YELLOW:
  133. Serial.println("yellow");
  134. Serial.println("Speed [2] + Motion Start!");
  135. digitalWrite (Speed1 , LOW);
  136. digitalWrite (Speed2 , HIGH);
  137. digitalWrite (Stop , LOW);
  138. digitalWrite(13, 0);
  139. delay(500);
  140. break;
  141. default:
  142. digitalWrite (Speed1 , LOW);
  143. digitalWrite (Speed2 , LOW);
  144. digitalWrite (Stop , LOW);
  145. //delay(500);
  146. break;
  147. }
  148. }
  149. }
  150.  
  151. void sendTxData() {
  152. ss.print(data1); ss.print("A");
  153. ss.print(data2); ss.print("B");
  154. ss.print(data3); ss.print("C");
  155. ss.print(data4); ss.print("D");
  156. ss.print("\n");
  157. Serial.print(data1); Serial.print("A");
  158. Serial.print(data2); Serial.print("B");
  159. Serial.print(data3); Serial.print("C");
  160. Serial.print(data4); Serial.print("D");
  161. Serial.print("\n");
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement