Advertisement
safwan092

Untitled

Feb 17th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 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, 50);//100 // set detect region center x value(0~100)
  60. Mu.write(VISION_COLOR_RECOGNITION, kYValue, 50);//100 // set detect region center y value(0~100)
  61. Mu.write(VISION_COLOR_RECOGNITION, kWidthValue, 5);//50 // set detect region center width value(0~100)
  62. Mu.write(VISION_COLOR_RECOGNITION, kHeightValue, 5);//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. //MU_COLOR_BLACK
  111. //MU_COLOR_BLUE
  112. //MU_COLOR_CYAN
  113. //MU_COLOR_GREEN
  114. //MU_COLOR_PURPLE
  115. //MU_COLOR_RED
  116. //MU_COLOR_WHITE
  117. //MU_COLOR_YELLOW
  118. ////////////////////
  119. case MU_COLOR_YELLOW:
  120. Serial.println("yellow");
  121. Serial.println("Speed [2] + Motion Start!");
  122. digitalWrite (Speed1 , LOW);
  123. digitalWrite (Speed2 , HIGH);
  124. digitalWrite (Stop , LOW);
  125. digitalWrite(13, 0);
  126. delay(500);
  127. break;
  128. case MU_COLOR_GREEN:
  129. Serial.println("green");
  130. Serial.println("Speed [1] + Motion Start!");
  131. digitalWrite (Speed1 , HIGH);
  132. digitalWrite (Speed2 , LOW);
  133. digitalWrite (Stop , LOW);
  134. digitalWrite(13, 1);
  135. delay(500);
  136. break;
  137. case MU_COLOR_PURPLE:
  138. Serial.println("purple");
  139. Serial.println("Motion Stop!!!! [Color Detected]");
  140. digitalWrite (Speed1 , LOW);
  141. digitalWrite (Speed2 , LOW);
  142. digitalWrite (Stop , HIGH);
  143. delay(500);
  144. digitalWrite (Speed1 , LOW);
  145. digitalWrite (Speed2 , LOW);
  146. digitalWrite (Stop , LOW);
  147. digitalWrite(13, 0);
  148. delay(500);
  149. break;
  150.  
  151. default:
  152. //digitalWrite (Speed1 , LOW);
  153. // digitalWrite (Speed2 , LOW);
  154. // digitalWrite (Stop , LOW);
  155. //delay(500);
  156. break;
  157. }
  158. }
  159. }
  160.  
  161. void sendTxData() {
  162. ss.print(data1); ss.print("A");
  163. ss.print(data2); ss.print("B");
  164. ss.print(data3); ss.print("C");
  165. ss.print(data4); ss.print("D");
  166. ss.print("\n");
  167. //Serial.print(data1); Serial.print("A");
  168. //Serial.print(data2); Serial.print("B");
  169. //Serial.print(data3); Serial.print("C");
  170. //Serial.print(data4); Serial.print("D");
  171. //Serial.print("\n");
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement