safwan092

Untitled

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