[text] arduino

Viewer

  1. #include <Wire.h>
  2. #include "Adafruit_TCS34725.h"
  3. #include <Servo.h>
  4.  
  5. Servo myservo;  // create servo object to control a servo
  6. // twelve servo objects can be created on most boards
  7.  
  8. int pos = 0;    // variable to store the servo position
  9.  
  10. /* Example code for the Adafruit TCS34725 breakout library */
  11.  
  12. /* Connect SCL    to analog 5
  13.    Connect SDA    to analog 4
  14.    Connect VDD    to 3.3V DC
  15.    Connect GROUND to common ground */
  16.  
  17. /* Initialise with default values (int time = 2.4ms, gain = 1x) */
  18. // Adafruit_TCS34725 tcs = Adafruit_TCS34725();
  19.  
  20. /* Initialise with specific int time and gain values */
  21. Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);
  22.  
  23. void setup(void) {
  24.   Serial.begin(9600);
  25.  
  26.   if (tcs.begin()) {
  27.     Serial.println("Found sensor");
  28.   } else {
  29.     Serial.println("No TCS34725 found ... check your connections");
  30.     while (1);
  31.   }
  32.  
  33.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  34.  
  35.   // Now we're ready to get readings!
  36. }
  37.  
  38. void loop(void) {
  39.   uint16_t r, g, b, c;
  40.  
  41.   tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_614MS);
  42.   delay(154); // Delay for one old integ. time period (to finish old reading)
  43.   delay(615); // Delay for one new integ. time period (to allow new reading)
  44.   tcs.getRawData(&r, &g, &b, &c);
  45.  
  46.   Serial.print("Integ. time: 614.4ms ");
  47.   Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  48.   Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  49.   Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  50.   Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  51.   Serial.println(" ");
  52.  
  53.   tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_300MS);
  54.   delay(615); // Delay for one old integ. time period (to finish old reading)
  55.   delay(300); // Delay for one new integ. time period (to allow new reading)
  56.   tcs.getRawData(&r, &g, &b, &c);
  57.   Serial.print("Integ. time: 300.0ms ");
  58.   Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  59.   Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  60.   Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  61.   Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  62.   Serial.println(" ");
  63.  
  64.   tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_154MS);
  65.   delay(300); // Delay for one old integ. time period (to finish old reading)
  66.   delay(154); // Delay for one new integ. time period (to allow new reading)
  67.   tcs.getRawData(&r, &g, &b, &c);
  68.   Serial.print("Integ. time: 153.6ms ");
  69.   Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  70.   Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  71.   Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  72.   Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  73.   Serial.println(" ");
  74.   //red
  75. if(r < 500 && g > r && r > b) {
  76.   myservo.write(15);
  77. } else if(g > 1000 && g > r && g > b){
  78.   //Green
  79.   myservo.write(110);
  80. } else if(b > 1000 && b > g && b > r)
  81. {
  82. myservo.write(30);}
  83. else if(b > 1000 && g > 1000 && r > 1000) {
  84. //white
  85. myservo.write(45);}
  86. else if(r > 1000 && g > 500 && r < 200){
  87. //orange
  88. myservo.write(60);} else if(r > 1000 && g > 1000 && b < 200){
  89.   //Yellow
  90. myservo.write(75);}
  91.  else {
  92. myservo.write(90);}
  93.  
  94.  
  95.  
  96.  
  97.   /*while (r, DEC < 100)
  98.   {
  99.     if (pos <= 72) { // goes from 0 degrees to 180 degrees
  100.     // in steps of 1 degree
  101.     pos += 1;
  102.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  103.     delay(15);                       // waits 15ms for the servo to reach the position
  104.   }
  105.   else if(pos >= 72) { // goes from 180 degrees to 0 degrees
  106.     pos -= 1;
  107.     myservo.write(pos);              // tell servo to go to position in variable 'pos'
  108.     delay(15);                       // waits 15ms for the servo to reach the position
  109.   }
  110.   }*/
  111. }

Editor

You can edit this paste and save as new:


File Description
  • arduino
  • Paste Code
  • 05 Jun-2023
  • 3.78 Kb
You can Share it: