Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "Adafruit_TCS34725.h"
  3. #include <Adafruit_NeoPixel.h>
  4. #include <Adafruit_CircuitPlayground.h>
  5.  
  6.  
  7. // our RGB -> eye-recognized gamma color
  8. byte gammatable[256];
  9.  
  10.  
  11. Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
  12.  
  13. void setup() {
  14.  
  15. Serial.begin(9600);
  16. Serial.println("Color View Test!");
  17.  
  18. CircuitPlayground.begin();
  19. CircuitPlayground.clearPixels();
  20.  
  21.  
  22. if (tcs.begin()) {
  23. Serial.println("Found sensor");
  24. } else {
  25. Serial.println("No TCS34725 found ... check your connections");
  26. while (1); // halt!
  27. }
  28.  
  29. // thanks PhilB for this gamma table!
  30. // it helps convert RGB colors to what humans see
  31. for (int i=0; i<256; i++) {
  32. float x = i;
  33. x /= 255;
  34. x = pow(x, 2.5);
  35. x *= 255;
  36.  
  37. gammatable[i] = x;
  38. //Serial.println(gammatable[i]);
  39. }
  40.  
  41. for (int i=0; i<3; i++){ //this sequence flashes the first pixel three times as a countdown to the color reading.
  42. CircuitPlayground.setPixelColor (0, 188, 188, 188); //white, but dimmer-- 255 for all three values makes it blinding!
  43.  
  44. delay(1000);
  45. CircuitPlayground.setPixelColor (0, 0, 0, 0);
  46.  
  47. delay(500);
  48. }
  49.  
  50. uint16_t clear, red, green, blue;
  51.  
  52. tcs.setInterrupt(false); // turn on LED
  53.  
  54. delay(60); // takes 50ms to read
  55.  
  56. tcs.getRawData(&red, &green, &blue, &clear);
  57.  
  58. tcs.setInterrupt(true); // turn off LED
  59.  
  60. Serial.print("C:\t"); Serial.print(clear);
  61. Serial.print("\tR:\t"); Serial.print(red);
  62. Serial.print("\tG:\t"); Serial.print(green);
  63. Serial.print("\tB:\t"); Serial.print(blue);
  64.  
  65. // Figure out some basic hex code for visualization
  66. uint32_t sum = red;
  67. sum += green;
  68. sum += blue;
  69. //sum += clear; // clear contains RGB already so no need to re-add it
  70.  
  71. float r, g, b;
  72. r = red; r /= sum;
  73. g = green; g /= sum;
  74. b = blue; b /= sum;
  75. r *= 256; g *= 256; b *= 256;
  76. Serial.print("\t");
  77. Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
  78. Serial.println();
  79.  
  80. Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" "); Serial.println((int)b );
  81. colorWipe(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b], 0);
  82. }
  83.  
  84. // Fill the dots one after the other with a color
  85. void colorWipe(int r, int g, int b, uint8_t wait) {
  86. for(uint16_t i=0; i< 10; i++) {
  87. CircuitPlayground.setPixelColor(i, r, g, b);
  88.  
  89. delay(wait);
  90. }
  91. }
  92.  
  93. void loop() {
  94.  
  95. //loop is empty because it only takes the color reading once on power up! Turn the scarf off and on again to change the color.
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement