safwan092

Untitled

Dec 2nd, 2018
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Watch video here: https://www.youtube.com/watch?v=BERHEVgaX40
  2.  
  3. /*
  4. // TCS230 color recognition sensor
  5. // Sensor connection pins to Arduino are shown in comments
  6.  
  7. Color Sensor Arduino
  8. ----------- --------
  9. VCC 5V
  10. GND GND
  11. s0 8
  12. s1 9
  13. s2 12
  14. s3 11
  15. OUT 10
  16. OE GND
  17. */
  18. const int s0 = 8;
  19. const int s1 = 9;
  20. const int s2 = 12;
  21. const int s3 = 11;
  22. const int out = 10;
  23. // LED pins connected to Arduino
  24. int redLed = 2;
  25. int greenLed = 3;
  26. int blueLed = 4;
  27. // Variables
  28. int red = 0;
  29. int green = 0;
  30. int blue = 0;
  31.  
  32. void setup()
  33. {
  34. Serial.begin(9600);
  35. pinMode(s0, OUTPUT);
  36. pinMode(s1, OUTPUT);
  37. pinMode(s2, OUTPUT);
  38. pinMode(s3, OUTPUT);
  39. pinMode(out, INPUT);
  40. pinMode(redLed, OUTPUT);
  41. pinMode(greenLed, OUTPUT);
  42. pinMode(blueLed, OUTPUT);
  43. digitalWrite(s0, HIGH);
  44. digitalWrite(s1, HIGH);
  45. }
  46.  
  47. void loop()
  48. {
  49. color();
  50. Serial.print("R Intensity:");
  51. Serial.print(red, DEC);
  52. Serial.print(" G Intensity: ");
  53. Serial.print(green, DEC);
  54. Serial.print(" B Intensity : ");
  55. Serial.print(blue, DEC);
  56. //Serial.println();
  57.  
  58. if (red < blue && red < green && red < 20)
  59. {
  60. Serial.println(" - (Red Color)");
  61. digitalWrite(redLed, HIGH); // Turn RED LED ON
  62. digitalWrite(greenLed, LOW);
  63. digitalWrite(blueLed, LOW);
  64. }
  65.  
  66. else if (blue < red && blue < green)
  67. {
  68. Serial.println(" - (Blue Color)");
  69. digitalWrite(redLed, LOW);
  70. digitalWrite(greenLed, LOW);
  71. digitalWrite(blueLed, HIGH); // Turn BLUE LED ON
  72. }
  73.  
  74. else if (green < red && green < blue)
  75. {
  76. Serial.println(" - (Green Color)");
  77. digitalWrite(redLed, LOW);
  78. digitalWrite(greenLed, HIGH); // Turn GREEN LED ON
  79. digitalWrite(blueLed, LOW);
  80. }
  81. else{
  82. Serial.println();
  83. }
  84. delay(300);
  85. digitalWrite(redLed, LOW);
  86. digitalWrite(greenLed, LOW);
  87. digitalWrite(blueLed, LOW);
  88. }
  89.  
  90. void color()
  91. {
  92. digitalWrite(s2, LOW);
  93. digitalWrite(s3, LOW);
  94. //count OUT, pRed, RED
  95. red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  96. digitalWrite(s3, HIGH);
  97. //count OUT, pBLUE, BLUE
  98. blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  99. digitalWrite(s2, HIGH);
  100. //count OUT, pGreen, GREEN
  101. green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  102. }
RAW Paste Data