safwan092

Untitled

Nov 13th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <DFPlayer_Mini_Mp3.h>
  3. void printDetail(uint8_t type, int value);
  4.  
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6.  
  7. #define S0 4
  8. #define S1 5
  9. #define S2 7
  10. #define S3 6
  11. #define sensorOut 8
  12.  
  13. int frequency = 0;
  14. int color = 0;
  15.  
  16.  
  17. void setup() {
  18.  
  19. mySerial.begin (9600);
  20. mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
  21. mp3_set_volume (25);
  22.  
  23. Serial.begin(115200);
  24.  
  25. pinMode(S0, OUTPUT);
  26. pinMode(S1, OUTPUT);
  27. pinMode(S2, OUTPUT);
  28. pinMode(S3, OUTPUT);
  29. pinMode(sensorOut, INPUT);
  30. //Setting frequency-scaling
  31. digitalWrite(S0, HIGH);
  32. digitalWrite(S1, LOW);
  33.  
  34. Serial.println();
  35. }
  36.  
  37.  
  38. void loop() {
  39.  
  40. color = readColor();
  41. delay(10);
  42. switch (color) {
  43. case 1:
  44. Serial.println("\t\t\tRED detected!");
  45. // ./mp3/0001.mp3
  46. mp3_play (1);
  47. delay (3000);
  48. mp3_stop ();
  49. break;
  50.  
  51. case 2:
  52. Serial.println("\t\t\tBLUE detected!");
  53. // ./mp3/0002.mp3
  54. mp3_play (2);
  55. delay (3000);
  56. mp3_stop ();
  57. break;
  58.  
  59. case 3:
  60. Serial.println("\t\t\tGREEN detected!");
  61. // ./mp3/0003.mp3
  62. mp3_play (3);
  63. delay (3000);
  64. mp3_stop ();
  65. break;
  66.  
  67.  
  68. case 4:
  69. Serial.println("\t\t\tWHITE detected!");
  70. // ./mp3/0004.mp3
  71. mp3_play (4);
  72. delay (3000);
  73. mp3_stop ();
  74. break;
  75.  
  76. case 5:
  77. Serial.println("\t\t\tORANGE detected!");
  78. // ./mp3/0005.mp3
  79. mp3_play (5);
  80. delay (3000);
  81. mp3_stop ();
  82. break;
  83.  
  84. case 6:
  85. Serial.println("\t\t\tBLACK detected!");
  86. // ./mp3/0006.mp3
  87. mp3_play (6);
  88. delay (3000);
  89. mp3_stop ();
  90. break;
  91.  
  92. case 7:
  93. Serial.println("\t\t\tYELLOW detected!");
  94. // ./mp3/0007.mp3
  95. mp3_play (7);
  96. delay (3000);
  97. mp3_stop ();
  98.  
  99. break;
  100. case 8:
  101. Serial.println("\t\t\tPURPLE detected!");
  102. // ./mp3/0008.mp3
  103. mp3_play (8);
  104. delay (3000);
  105. mp3_stop ();
  106.  
  107. break;
  108. case 0:
  109. break;
  110. }
  111. color = 0;
  112.  
  113. }
  114.  
  115.  
  116. //Read-Color Function
  117.  
  118. int readColor() {
  119.  
  120. ////////////////////////////////////////////////////////
  121.  
  122. //Setting red filtered photodiodes to be read
  123.  
  124. digitalWrite(S2, LOW);
  125. digitalWrite(S3, LOW);
  126.  
  127.  
  128. //Reading the output frequency
  129. frequency = pulseIn(sensorOut, LOW);
  130. int R = frequency;
  131. //Printing the value on the serial monitor
  132. Serial.print("R= "); //printing name
  133. Serial.print(frequency); //printing RED color frequency
  134. Serial.print(" ");
  135. delay(50);
  136.  
  137. ////////////////////////////////////////////////////////
  138.  
  139. //Setting Green filtered photodiodes to be read
  140.  
  141. digitalWrite(S2, HIGH);
  142. digitalWrite(S3, HIGH);
  143.  
  144. //Reading the output frequency
  145. frequency = pulseIn(sensorOut, LOW);
  146. int G = frequency;
  147.  
  148. //Printing the value on the serial monitor
  149. Serial.print("G= "); //printing name
  150. Serial.print(frequency); //printing RED color frequency
  151. Serial.print(" ");
  152. delay(50);
  153.  
  154. ////////////////////////////////////////////////////////
  155.  
  156. //Setting Blue filtered photodiodes to be read
  157.  
  158. digitalWrite(S2, LOW);
  159. digitalWrite(S3, HIGH);
  160.  
  161. //Reading the output frequency
  162. frequency = pulseIn(sensorOut, LOW);
  163. int B = frequency;
  164.  
  165. //Printing the value on the serial monitor
  166. Serial.print("B= "); //printing name
  167. Serial.print(frequency); //printing RED color frequency
  168. Serial.println(" ");
  169. delay(50);
  170.  
  171. ////////////////////////////////////////////////////////
  172.  
  173. if (R<75 & R>50 && G < 250 && G > 110 && B < 160 && B > 90 ) {
  174. color = 1; // Red
  175. }
  176. else if (R<45 & R>30 && G < 40 && G > 25 && B < 30 && B > 10 ) {
  177. color = 2; // Blue
  178. }
  179. else if (R<45 & R>30 && G < 40 && G > 25 && B < 250 && B > 200 ) {
  180. color = 3; // Green
  181. }
  182. else if (R<35 & R>4 && G < 45 && G > 15 && B < 40 && B > 10 ) {
  183. color = 4; // WHITE
  184. }
  185. else if (R<40 & R>15 && G < 130 && G > 50 && B < 150 && B > 50 ) {
  186. color = 5; // ORANGE
  187. }
  188. else if (R<270 & R>240 && G < 300 && G > 270 && B < 210 && B > 190 ) {
  189. color = 6; // BLACK
  190. }
  191. else if (R<30 & R>15 && G < 41 && G > 28 && B < 60 && B > 42 ) {
  192. color = 7; // YELLOW
  193. }
  194. else if (R<150 & R>100 && G < 250 && G > 200 && B < 250 && B > 200 ) {
  195. color = 8; // PURPLE
  196. }
  197. return color;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment