Advertisement
KoMeDiAnT

Color

Oct 8th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <MD_TCS230.h>
  3.  
  4. #define  S0_OUT  2
  5. #define  S1_OUT  3
  6. #define  S2_OUT  4
  7. #define  S3_OUT  5
  8. MD_TCS230 ColorSensor(S2_OUT, S3_OUT, S0_OUT, S1_OUT);
  9.  
  10. #define R_OUT 6
  11. #define G_OUT 7
  12. #define B_OUT 8
  13.  
  14. void set_rgb_led(int r, int g, int b) {
  15.   if (g > 100) {
  16.     g = (255 + g) / 2;
  17.     r /= 2;
  18.     b /= 2;
  19.   }
  20.   analogWrite(R_OUT, 255 - r);
  21.   analogWrite(G_OUT, 255 - g);
  22.   analogWrite(B_OUT, 255 - b);
  23. }
  24.  
  25. void setup()
  26. {
  27.     Serial.begin(115200);
  28.     Serial.println("Started!");
  29.  
  30.     sensorData whiteCalibration;
  31.     whiteCalibration.value[TCS230_RGB_R] = 42920;
  32.     whiteCalibration.value[TCS230_RGB_G] = 43330;
  33.     whiteCalibration.value[TCS230_RGB_B] = 55360;
  34.  
  35.     sensorData blackCalibration;
  36.     blackCalibration.value[TCS230_RGB_R] = 6610;
  37.     blackCalibration.value[TCS230_RGB_G] = 6000;
  38.     blackCalibration.value[TCS230_RGB_B] = 7350;
  39.  
  40.     ColorSensor.begin();
  41.     ColorSensor.setDarkCal(&blackCalibration);
  42.     ColorSensor.setWhiteCal(&whiteCalibration);
  43.  
  44.     pinMode(R_OUT, OUTPUT);
  45.     pinMode(G_OUT, OUTPUT);
  46.     pinMode(B_OUT, OUTPUT);
  47. }
  48.  
  49. void loop()
  50. {
  51.     colorData rgb;
  52.     ColorSensor.read();
  53.     while (!ColorSensor.available())
  54.         ;
  55.     ColorSensor.getRGB(&rgb);
  56.     set_rgb_led(rgb.value[TCS230_RGB_R], rgb.value[TCS230_RGB_G], rgb.value[TCS230_RGB_B]);
  57.     print_rgb(rgb);
  58.     delay(50);
  59. }
  60.  
  61. void print_rgb(colorData rgb)
  62. {
  63.     Serial.print(rgb.value[TCS230_RGB_R]);
  64.     Serial.print(" ");
  65.     Serial.print(rgb.value[TCS230_RGB_G]);
  66.     Serial.print(" ");
  67.     Serial.print(rgb.value[TCS230_RGB_B]);
  68.     Serial.println();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement