safwan092

Untitled

Dec 19th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27, 16, 2);
  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. lcd.init();
  17. lcd.init();
  18. lcd.backlight();
  19. lcd.setCursor(0, 0);
  20. lcd.print("Scanning: ...");
  21. delay(5000);
  22. Serial.begin(115200);
  23. pinMode(S0, OUTPUT);
  24. pinMode(S1, OUTPUT);
  25. pinMode(S2, OUTPUT);
  26. pinMode(S3, OUTPUT);
  27. pinMode(sensorOut, INPUT);
  28. digitalWrite(S0, HIGH);
  29. digitalWrite(S1, LOW);
  30. }
  31.  
  32.  
  33. void loop() {
  34.  
  35. color = readColor();
  36. delay(10);
  37. switch (color) {
  38. case 1:
  39. Serial.println("\t\t\tRED detected!");
  40. lcd.setCursor(0, 1);
  41. // "****************"
  42. lcd.print("RED detected ");
  43. break;
  44. case 2:
  45. Serial.println("\t\t\tBLUE detected!");
  46. lcd.setCursor(0, 1);
  47. // "****************"
  48. lcd.print("BLUE detected ");
  49. break;
  50. case 3:
  51. Serial.println("\t\t\tGREEN detected!");
  52. lcd.setCursor(0, 1);
  53. // "****************"
  54. lcd.print("GREEN detected ");
  55. break;
  56. case 4:
  57. Serial.println("\t\t\tYELLOW detected!");
  58. lcd.setCursor(0, 1);
  59. // "****************"
  60. lcd.print("YELLOW detected ");
  61. break;
  62. case 0:
  63. lcd.setCursor(0, 1);
  64. // "****************"
  65. lcd.print(" ");
  66. break;
  67. }
  68. color = 0;
  69.  
  70. }
  71.  
  72.  
  73. //Read-Color Function
  74.  
  75. int readColor() {
  76.  
  77. ////////////////////////////////////////////////////////
  78.  
  79. //Setting red filtered photodiodes to be read
  80.  
  81. digitalWrite(S2, LOW);
  82. digitalWrite(S3, LOW);
  83.  
  84.  
  85. //Reading the output frequency
  86. frequency = pulseIn(sensorOut, LOW);
  87. int R = frequency;
  88. //Printing the value on the serial monitor
  89. Serial.print("R= "); //printing name
  90. Serial.print(frequency); //printing RED color frequency
  91. Serial.print(" ");
  92. delay(50);
  93.  
  94. ////////////////////////////////////////////////////////
  95.  
  96. //Setting Green filtered photodiodes to be read
  97.  
  98. digitalWrite(S2, HIGH);
  99. digitalWrite(S3, HIGH);
  100.  
  101. //Reading the output frequency
  102. frequency = pulseIn(sensorOut, LOW);
  103. int G = frequency;
  104.  
  105. //Printing the value on the serial monitor
  106. Serial.print("G= "); //printing name
  107. Serial.print(frequency); //printing RED color frequency
  108. Serial.print(" ");
  109. delay(50);
  110.  
  111. ////////////////////////////////////////////////////////
  112.  
  113. //Setting Blue filtered photodiodes to be read
  114.  
  115. digitalWrite(S2, LOW);
  116. digitalWrite(S3, HIGH);
  117.  
  118. //Reading the output frequency
  119. frequency = pulseIn(sensorOut, LOW);
  120. int B = frequency;
  121.  
  122. //Printing the value on the serial monitor
  123. Serial.print("B= "); //printing name
  124. Serial.print(frequency); //printing RED color frequency
  125. Serial.println(" ");
  126. delay(50);
  127.  
  128. ////////////////////////////////////////////////////////
  129.  
  130. if (R < 60 && R > 20 && G < 150 && G > 40 && B < 150 && B > 40 ) {
  131. color = 1; // Red
  132. }
  133. else if (R < 150 && R > 20 && G < 150 && G > 20 && B < 20 && B > 10 ) {
  134. color = 2; // Blue
  135. }
  136. else if (R < 70 && R > 30 && G < 40 && G > 10 && B < 70 && B > 30 ) {
  137. color = 3; // Green
  138. }
  139. else if (R < 20 && R > 10 && G < 40 && G > 20 && B < 50 && B > 20 ) {
  140. color = 4; // YELLOW
  141. }
  142. return color;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment