safwan092

Color Sensor Green Red LED Arduino Uno

Nov 26th, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #define RedLED 2
  2. #define GreenLED 3
  3. #define S0 4
  4. #define S1 5
  5. #define S2 7
  6. #define S3 6
  7. #define sensorOut 8
  8.  
  9. int frequencyR = 0;
  10. int frequencyG = 0;
  11. int frequencyB = 0;
  12.  
  13. int currentColor = 0;
  14.  
  15. /////////////////////////////
  16. //Color is RED
  17. int Red[3] = { 27, 100, 80};
  18. //Color is Green
  19. int Green[3] = { 92, 60, 80};
  20. //Color is Yellow
  21. int Yellow[3] = { 20, 32, 53};
  22. /////////////////////////////
  23. //Color is White
  24. int White[3] = { 19, 21, 16};
  25. //Color is Black
  26. int Black[3] = {155, 160, 118};
  27. //Color is Blue
  28. int Blue[3] = {120, 50, 33};
  29. /////////////////////////////
  30.  
  31. void setup() {
  32. pinMode(RedLED, OUTPUT);
  33. pinMode(GreenLED, OUTPUT);
  34. pinMode(S0, OUTPUT);
  35. pinMode(S1, OUTPUT);
  36. pinMode(S2, OUTPUT);
  37. pinMode(S3, OUTPUT);
  38. pinMode(sensorOut, INPUT);
  39. digitalWrite(S0, HIGH);
  40. digitalWrite(S1, LOW);
  41. digitalWrite(RedLED, LOW);
  42. digitalWrite(GreenLED, LOW);
  43.  
  44. Serial.begin(9600);
  45. }
  46.  
  47. void loop() {
  48. TurnOnRedLED(2000);
  49. TurnOnGreenLED(2000);
  50. ReadColorSensor();
  51. SerialOutColor(frequencyR, frequencyG, frequencyB);
  52. }
  53.  
  54.  
  55. void ReadColorSensor() {
  56. digitalWrite(S2, LOW);
  57. digitalWrite(S3, LOW);
  58. frequencyR = pulseIn(sensorOut, LOW);
  59. Serial.print("R= ");
  60. Serial.print(frequencyR);
  61. Serial.print(" ");
  62. delay(100);
  63. digitalWrite(S2, HIGH);
  64. digitalWrite(S3, HIGH);
  65. frequencyG = pulseIn(sensorOut, LOW);
  66. Serial.print("G= ");
  67. Serial.print(frequencyG);
  68. Serial.print(" ");
  69. delay(100);
  70. digitalWrite(S2, LOW);
  71. digitalWrite(S3, HIGH);
  72. frequencyB = pulseIn(sensorOut, LOW);
  73. Serial.print("B= ");
  74. Serial.print(frequencyB);
  75. Serial.println(" ");
  76. delay(100);
  77. }
  78.  
  79. void SerialOutColor(int a, int b, int c) {
  80. //Color is RED
  81. if (a > (Red[0] - 10) && a < (Red[0] + 10) && b > (Red[1] - 10) && b < (Red[1] + 10) && c > (Red[2] - 10) && c < (Red[2] + 10)) {
  82. currentColor = 1;
  83. Serial.println("Color is RED");
  84. delay(2000);
  85.  
  86. }
  87. //Color is Green
  88. else if (a > (Green[0] - 10) && a < (Green[0] + 10) && b > (Green[1] - 10) && b < (Green[1] + 10) && c > (Green[2] - 10) && c < (Green[2] + 10)) {
  89. currentColor = 1;
  90. Serial.println("Color is Green");
  91. delay(2000);
  92. }
  93. //Color is Yellow
  94. else if (a > (Yellow[0] - 8) && a < (Yellow[0] + 8) && b > (Yellow[1] - 8) && b < (Yellow[1] + 8) && c > (Yellow[2] - 8) && c < (Yellow[2] + 8)) {
  95. currentColor = 1;
  96. Serial.println("Color is Yellow");
  97. delay(2000);
  98. }
  99. //Color is White
  100. else if (a > (White[0] - 8) && a < (White[0] + 8) && b > (White[1] - 8) && b < (White[1] + 8) && c > (White[2] - 8) && c < (White[2] + 8)) {
  101. currentColor = 2;
  102. Serial.println("Color is White");
  103. delay(2000);
  104. }
  105. //Color is Black
  106. else if (a > (Black[0] - 10) && a < (Black[0] + 10) && b > (Black[1] - 10) && b < (Black[1] + 10) && c > (Black[2] - 10) && c < (Black[2] + 10)) {
  107. currentColor = 2;
  108. Serial.println("Color is Black");
  109. delay(2000);
  110. }
  111. //Color is Blue
  112. else if (a > (Blue[0] - 10) && a < (Blue[0] + 10) && b > (Blue[1] - 10) && b < (Blue[1] + 10) && c > (Blue[2] - 10) && c < (Blue[2] + 10)) {
  113. currentColor = 2;
  114. Serial.println("Color is Blue");
  115. delay(2000);
  116. }
  117. }
  118.  
  119. void TurnOnRedLED(int Time) {
  120. if (currentColor == 1) {
  121. digitalWrite(RedLED, HIGH);
  122. delay(Time);
  123. digitalWrite(RedLED, LOW);
  124. currentColor = 0;
  125. }
  126. }
  127. void TurnOnGreenLED(int Time) {
  128. if (currentColor == 2) {
  129. digitalWrite(GreenLED, HIGH);
  130. delay(Time);
  131. digitalWrite(GreenLED, LOW);
  132. currentColor = 0;
  133. }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment