Adafruit TCS34725 to LED code

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Pelayo
 
Posts: 6
Joined: Thu Jul 15, 2021 5:19 pm

Adafruit TCS34725 to LED code

Post by Pelayo »

Hi,
I am trying to do this:
characterize natural light comming in through the window in one room and reproduce it in another room, with no windows, were I have this LED strip with 60 warmWhite and 60 ColdWhite LED units&meter (12 volts, 1.1 Amp maximum). The LEDs are powered through a Sparkfun Power Driver Shield Kit ( DEV-10618 ), using two of the 12 volts Mossfet channels.

I have been using the sensor for other purposes and it woks, apparently, fine.
I have also been using the LED strip commanded from an array of >5000 discrete, secuential values, for hours and it performed OK.

Now I have this code (see below) were
1.- I get the colorTemp and Lux values from the sensor in the window;
2.- I calculate a running average with the last 10 readings (I tried that alone and it works) to smooth the data
3.- I send the data to my LEDS so that they shine in proportion to the natural light recorded (with Lux artificially topped at 5000 Lux) and trying to balance warm&cold leds to reproduce the colorTemp.

I run it and it works.....but only for some time. Maybe a couple of hours. Then it freezes. It stops sending data to the serial and the last analogWrites sent to the LED stay constant until resetted.

After the thing is resetted, it starts nicely again and it runs for 30, 60 or 90 minutes and stops...

I disable the Serialprint commands, in case it is the computer that "gets tired" of receiving the data, and I disconnect the Arduino from the computer, so that everything gets powered only from the Sparfun power supply. No difference.

Tried longer delays on the loop (5000). No difference.
Apparently no element is getting overheated.

What could be the problem??
Memory should be enough, shouldn't it?
Any suggestions???
Thanks for any help.

Code: Select all


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

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */


/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);


// Code from "www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing" for runing average n=10.


const int numReadings = 10;
const int maxLUX = 5000;
const int minLUX = 20;

int LUXreadings[numReadings];        // the Lux readings from the analog input
int readIndex = 0;                                // the index of the current reading
long totalLUX = 0;                               // the running total Lux. 
int averageLUX = 0;                            // the average
int TEMPreadings[numReadings];      // the Temperature readings from the analog input
long totalTEMP = 0;                             // the running total temperature. 
int averageTEMP = 0;                          // the average Temperature

//int inputPin = A0;                             //not applicable. Our vars colorTemp and Lux are calculated from
//                                                             A4 & A5 inputs. see below


void setup(void) {
  Serial.begin(9600);      Serial.println("running:  T_LuxCapture_runningAvg_toLED");
  
      pinMode(6, OUTPUT);// cold LED in D6
      pinMode(5, OUTPUT);// warm LED in D5
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);}
  

// initialize all the readings for the running average to 0:

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    LUXreadings[thisReading] = 0;
}
}
float intensity ;
int bright5;
int bright6;
int Cold;
int Warm;
int Temperature;

void loop(void) 
{
  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);


if (lux >= maxLUX){
  lux = maxLUX;
  }
  else if (lux <= minLUX){
  lux = 0;
  }
  else {
  lux = lux;
  } 

if (colorTemp >= 6000){
    colorTemp = 6000;
}
else if (colorTemp <= 2700){
  colorTemp = 2700;
}
else {
  colorTemp = colorTemp;
  } 

// back to the running avge code
//   subtract the last reading:

  totalLUX = totalLUX - LUXreadings[readIndex];
  totalTEMP = totalTEMP - TEMPreadings[readIndex];

/* the running avge code goes
    
                          // read from the sensor: 
                                 readings[readIndex] = analogRead(inputPin);
   
   in our case it should be 
   
          readings[readIndex] = lux....; 
   
   since colorTemp and Lux are calculated from A4 & A5 inputs
*/
   
  LUXreadings[readIndex] = lux;
  TEMPreadings[readIndex] = colorTemp;
 
                                     // add the reading to the total:
  totalLUX = totalLUX + LUXreadings[readIndex];
  totalTEMP = totalTEMP + TEMPreadings[readIndex];
                                     // advance to the next position in the array:
  readIndex = readIndex + 1;

                                     // if we're at the end of the array...
  if (readIndex >= numReadings) {
                                  // ...wrap around to the beginning:
    readIndex = 0;
  }

                                 // calculate the average:
int  averageLux = totalLUX / numReadings;
int  averageTEMP = totalTEMP / numReadings;
 intensity = (float)averageLux / maxLUX;
 
Temperature = averageTEMP * intensity;
    Cold = map(Temperature, 2700, 6000, 0, 255);
    Warm = map(Temperature, 2700, 6000, 255, 0);
    bright5 = Warm*intensity;
    bright6 = Cold*intensity;

  analogWrite(5,bright5);
  analogWrite(6,bright6);
                                           // send it to the computer as ASCII digits to check actual calculations
  Serial.print("\tavLux\t"); Serial.print(averageLux); Serial.print("\tLux:\t");   Serial.print(lux, DEC); 
      
  Serial.print("\tTotLUX:\t"); Serial.print(totalLUX); Serial.print("\tavTª\t"); 
  Serial.print(averageTEMP); Serial.print("\tTª:\t "); Serial.print(colorTemp, DEC); 
      
  Serial.print("\tTotTª:\t"); Serial.print(totalTEMP); Serial.print("\t");
  Serial.print(intensity, DEC);Serial.print("\tB5\t");Serial.print(bright5, DEC);
  Serial.print("\tB6\t");Serial.println(bright6, DEC);
  
 
  
  delay(100);        // delay in between reads for stability
}

User avatar
Pelayo
 
Posts: 6
Joined: Thu Jul 15, 2021 5:19 pm

Re: Adafruit TCS34725 to LED code

Post by Pelayo »

Sorry, there are a cople of wrong lines on the posted code.
This is correct (I think; it is what I have been using)
My apologies.

Thanks again.

Code: Select all

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

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 3.3V DC
   Connect GROUND to common ground */


/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);


// Code from "www.arduino.cc/en/Tutorial/BuiltInExamples/Smoothing" for runing average n=10.


const int numReadings = 10;
const int maxLUX = 5000;
const int minLUX = 20;

int LUXreadings[numReadings];      // the Lux readings from the analog input
int readIndex = 0;              // the index of the current reading
long totalLUX = 0;              // the running total Lux. 
int averageLUX = 0;             // the average
int TEMPreadings[numReadings];      // the Temperature readings from the analog input
long totalTEMP = 0;              // the running total temperature. 
int averageTEMP = 0;             // the average Temperature

//int inputPin = A0;   //not applicable. Our vars colorTemp and Lux are calculated from
//                        A4 & A5 inputs. see below


void setup(void) {
  Serial.begin(9600);Serial.println("running:  T_LuxCapture_runningAvg_toLED");
  
      pinMode(6, OUTPUT);// cold LED in D6
      pinMode(5, OUTPUT);// warm LED in D5
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);}
  

// initialize all the readings for the running average to 0:

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    LUXreadings[thisReading] = 0;
}
}
float intensity ;
int bright5;
int bright6;
int Cold;
int Warm;
int Temperature;

void loop(void) 
{
  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  lux = tcs.calculateLux(r, g, b);


if (lux >= maxLUX){
  lux = maxLUX;
  }
  else if (lux <= minLUX){
  lux = 0;
  }
  else {
  lux = lux;
  } 

if (colorTemp >= 6000){
    colorTemp = 6000;
}
else if (colorTemp <= 2700){
  colorTemp = 2700;
}
else {
  colorTemp = colorTemp;
  } 

// back to the running avge code
//   subtract the last reading:

  totalLUX = totalLUX - LUXreadings[readIndex];
  totalTEMP = totalTEMP - TEMPreadings[readIndex];
/* the running avge code goes
    
// read from the sensor: 
          readings[readIndex] = analogRead(inputPin);
   
   in our case it should be 
   
          readings[readIndex] = lux....; 
   
   since colorTemp and Lux are calculated from A4 & A5 inputs*/
   
  LUXreadings[readIndex] = lux;
  TEMPreadings[readIndex] = colorTemp;
 
  // add the reading to the total:
  totalLUX = totalLUX + LUXreadings[readIndex];
  totalTEMP = totalTEMP + TEMPreadings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
int  averageLux = totalLUX / numReadings;
int  averageTEMP = totalTEMP / numReadings;
 intensity = (float)averageLux / maxLUX;
 
// Temperature = averageTEMP * intensity;
    Cold = map(averageTEMP, 2700, 6000, 0, 255);
    Warm = map(averageTEMP, 2700, 6000, 255, 0);
    bright5 = Warm*intensity;
    bright6 = Cold*intensity;


  analogWrite(5,bright5);
  analogWrite(6,bright6);
  // send it to the computer as ASCII digits to check actual calculations
  Serial.print("\tavLux\t"); Serial.print(averageLux); Serial.print("\tLux:\t"); 
  Serial.print(lux, DEC); 
  
    
  Serial.print("\tTotLUX:\t"); Serial.print(totalLUX); Serial.print("\tavTª\t"); 
  Serial.print(averageTEMP); Serial.print("\tTª:\t "); Serial.print(colorTemp, DEC); 
    
  
  Serial.print("\tTotTª:\t"); Serial.print(totalTEMP); Serial.print("\t");
  Serial.print(intensity, DEC);Serial.print("\tB5\t");Serial.print(bright5, DEC);
  Serial.print("\tB6\t");Serial.println(bright6, DEC);
  
 
  
  delay(1000);        // delay in between reads for stability
}


User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adafruit TCS34725 to LED code

Post by adafruit_support_mike »

Try keeping track of the microcontroller’s free memory over time. That kind of ‘dies after so much time’ often means yiu have a memory leak simewhere.. blocks of memory ae being allocated, but not released for reuse.

User avatar
Pelayo
 
Posts: 6
Joined: Thu Jul 15, 2021 5:19 pm

Re: Adafruit TCS34725 to LED code

Post by Pelayo »

adafruit_support_mike wrote:Try keeping track of the microcontroller’s free memory over time. That kind of ‘dies after so much time’ often means yiu have a memory leak simewhere.. blocks of memory ae being allocated, but not released for reuse.
Thanks, I will try that, but I will need some indication on how to do that tracking......
Is that something easy to do?

Thanks again.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit TCS34725 to LED code

Post by adafruit_support_bill »


Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”