Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

RGB values received from TCS34725
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: RGB values received from TCS34725

by Systembolaget on Fri Sep 21, 2018 4:16 am

Here is the final code I used. Works well with very many colours, also pastel ones that others seem to have difficulty differentiating. Most important is a matt black rectangular shroud around the Adafruit breakout-board to stop any light ingress that would otherwise influence the sample's illumination. Hope it helps.

Code: Select all | TOGGLE FULL SIZE
/******* Tutorial 6a. Colour finder and sorter *******/

/******* Adafruit Metro Mini, ams TCS34725 *******/

/******* LIBRARIES *******/

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "Servo.h"

/******* VARIABLES *******/

const int SAMPLES[14][5] = { // Values from colour training (averaged raw r, g and b; actuator movement)
  {8771, 6557, 3427,  10},
  {7013, 2766, 1563,  20},
  {4092, 1118, 1142,  30},
  {4488, 1302, 1657,  40},
  {3009, 1846, 2235,  50},
  {7546, 6686, 6323,  60},
  {2650, 3139, 4116,  70},
  { 857,  965, 1113,  80},
  {2926, 5012, 5331,  90},
  { 964, 2014, 2418, 100},
  {1260, 2200, 1459, 110},
  {4784, 5898, 3138, 120},
  {5505, 5242, 2409, 130},
  {5406, 3893, 1912, 140}, // When adding samples, no particular order is required
};

bool sensorTriggered = false; // Sample present yes or no

byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]); // Determine number of samples in array

const byte pinLED = A0; // Connect ams TCS34725 breakout board LED pin to pin A0 on Adafruit Metro Mini

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); // Longer integration time for higher colour sensing fidelity

Servo HD1900A;

/******* FUNCTIONS *******/

void setup(void) {

  Serial.begin(9600);

  pinMode (pinLED, OUTPUT);

  HD1900A.attach(3); // Connect servo to pin 3 on Adafruit Metro Mini
  HD1900A.write(0); // Rotate to initial position

} // End of setup

void loop(void) {

  digitalWrite(pinLED, HIGH); // Turn on-board LED on; set to LOW to turn off or when using alternative illumination

  uint16_t r, g, b, c;

  tcs.getRawData(&r, &g, &b, &c); // Change Adafruit_TCS34725.cpp line 223 to omit &c (only needed for colour temperature and Lux calculation)

  byte foundColour = findColour(r, g, b);

  if (sensorTriggered) {

    HD1900A.write(SAMPLES[foundColour][3]); // Rotate servo to matching position

  } else {

    Serial.println("No swatch present!"); // Or buzzer?
    //HD1900A.write(0); // Rotate to initial position

  }

} // End of loop

byte findColour(int r, int g, int b) {

  int distance = 10000; // Raw distance from white to black (change depending on selected integration time and gain)
  byte foundColour;

  for (byte i = 0; i < samplesCount; i++) {

    int temp = sqrt(pow(r - SAMPLES[i][0], 2) + pow(g - SAMPLES[i][1], 2) + pow(b - SAMPLES[i][2], 2)); // Calculate Euclidean distance between colours

    if (temp < distance) {

      distance = temp;

      foundColour = i;

    }

  }

  if (distance > 500) { // Threshold distance from SAMPLES calibration process

    sensorTriggered = false;

  } else {

    sensorTriggered = true;

  }

  return foundColour;

} // End of findColour

Systembolaget
 
Posts: 252
Joined: Wed Mar 08, 2017 1:01 pm

Re: RGB values received from TCS34725

by adafruit_support_bill on Fri Sep 21, 2018 5:40 am

Thanks for sharing your code!

adafruit_support_bill
 
Posts: 78733
Joined: Sat Feb 07, 2009 10:11 am

Re: RGB values received from TCS34725

by mirz11 on Mon Oct 01, 2018 2:26 am

Systembolaget wrote:Here is the final code I used. Works well with very many colours, also pastel ones that others seem to have difficulty differentiating. Most important is a matt black rectangular shroud around the Adafruit breakout-board to stop any light ingress that would otherwise influence the sample's illumination. Hope it helps.

Code: Select all | TOGGLE FULL SIZE
/******* Tutorial 6a. Colour finder and sorter *******/

/******* Adafruit Metro Mini, ams TCS34725 *******/

/******* LIBRARIES *******/

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "Servo.h"

/******* VARIABLES *******/

const int SAMPLES[14][5] = { // Values from colour training (averaged raw r, g and b; actuator movement)
  {8771, 6557, 3427,  10},
  {7013, 2766, 1563,  20},
  {4092, 1118, 1142,  30},
  {4488, 1302, 1657,  40},
  {3009, 1846, 2235,  50},
  {7546, 6686, 6323,  60},
  {2650, 3139, 4116,  70},
  { 857,  965, 1113,  80},
  {2926, 5012, 5331,  90},
  { 964, 2014, 2418, 100},
  {1260, 2200, 1459, 110},
  {4784, 5898, 3138, 120},
  {5505, 5242, 2409, 130},
  {5406, 3893, 1912, 140}, // When adding samples, no particular order is required
};

bool sensorTriggered = false; // Sample present yes or no

byte samplesCount = sizeof(SAMPLES) / sizeof(SAMPLES[0]); // Determine number of samples in array

const byte pinLED = A0; // Connect ams TCS34725 breakout board LED pin to pin A0 on Adafruit Metro Mini

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); // Longer integration time for higher colour sensing fidelity

Servo HD1900A;

/******* FUNCTIONS *******/

void setup(void) {

  Serial.begin(9600);

  pinMode (pinLED, OUTPUT);

  HD1900A.attach(3); // Connect servo to pin 3 on Adafruit Metro Mini
  HD1900A.write(0); // Rotate to initial position

} // End of setup

void loop(void) {

  digitalWrite(pinLED, HIGH); // Turn on-board LED on; set to LOW to turn off or when using alternative illumination

  uint16_t r, g, b, c;

  tcs.getRawData(&r, &g, &b, &c); // Change Adafruit_TCS34725.cpp line 223 to omit &c (only needed for colour temperature and Lux calculation)

  byte foundColour = findColour(r, g, b);

  if (sensorTriggered) {

    HD1900A.write(SAMPLES[foundColour][3]); // Rotate servo to matching position

  } else {

    Serial.println("No swatch present!"); // Or buzzer?
    //HD1900A.write(0); // Rotate to initial position

  }

} // End of loop

byte findColour(int r, int g, int b) {

  int distance = 10000; // Raw distance from white to black (change depending on selected integration time and gain)
  byte foundColour;

  for (byte i = 0; i < samplesCount; i++) {

    int temp = sqrt(pow(r - SAMPLES[i][0], 2) + pow(g - SAMPLES[i][1], 2) + pow(b - SAMPLES[i][2], 2)); // Calculate Euclidean distance between colours

    if (temp < distance) {

      distance = temp;

      foundColour = i;

    }

  }

  if (distance > 500) { // Threshold distance from SAMPLES calibration process

    sensorTriggered = false;

  } else {

    sensorTriggered = true;

  }

  return foundColour;

} // End of findColour


Thank you for sharing the code!

mirz11
 
Posts: 2
Joined: Fri Sep 21, 2018 2:52 am

Please be positive and constructive with your questions and comments.