Color Sensor question

Hello,
I have this TCS230 color sensor example code.
It samples color every 4000ms.
I need it to sample at least 10 times faster.
I tried to change the delay(4000) lines to delay(400), but it stopped giving numbers and just output zeros.
Is the 4000ms an absolute limit of the hardware or can i accelerate the sample rate?

Here is the code:
Thanks!

#include <TimerOne.h>

#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2

int   g_count = 0;    // count the frequecy
int   g_array[3];     // store the RGB value
int   g_flag = 0;     // filter of RGB queue
float g_SF[3];        // save the RGB Scale factor
 
 
// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);
 
  digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, HIGH); 
}
 
// Select the filter color 
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;
 
  if(Level02 != 0)
    Level02 = HIGH;
 
  digitalWrite(S2, Level01); 
  digitalWrite(S3, Level02); 
}
 
void TSC_Count()
{
  g_count ++ ;
}
 
void TSC_Callback()
{
  switch(g_flag)
  {
    case 0: 
         Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
         Serial.print("->Frequency R=");
         Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
         Serial.print("->Frequency G=");
         Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;
 
    case 3:
         Serial.print("->Frequency B=");
         Serial.println(g_count);
         Serial.println("->WB End");
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)   
         break;
   default:
         g_count = 0;
         break;
  }
}
 
void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(1000000);             // set 1s period
}
 
void setup()
{
  TSC_Init();
  Serial.begin(9600);
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback);  
  attachInterrupt(0, TSC_Count, RISING);  
 
  delay(4000);
 
  for(int i=0; i<3; i++)
    Serial.println(g_array[i]);
 
  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor
 
  Serial.println(g_SF[0]);
  Serial.println(g_SF[1]);
  Serial.println(g_SF[2]);
 
}
 
void loop()
{
   g_flag = 0;
   for(int i=0; i<3; i++)
    Serial.println(int(g_array[i] * g_SF[i]));
   delay(4000);
 
}

You could try pushing the serial speed to 115200

I can't, i have other hardware in the project, that must work at 9600.
I saw a video of someone demonstrating that exact color sensor model and having on screen regenerating the color sampled and he moved the sensor, between the different colors, much faster than a 4 seconds sample rate and it worked well.

I wonder what i should do to have a better sample rate...

roineust, would you mind posting the complete code? i have simular issues and been searching for days to get things to work!

perhaps you need to learn how the timer library works and create an alternative thats suitable to your project? unfortunately i dont know how to build libraries yet, but thats all i can think of.

Im looking for roineusts solution. I tried the others but its not giving me a good color distinction, which is strange, because i thought if i buy an RGB Color Sensor i get RGB values from it. But i was wrong i get dynamic frequencies within dynamic frequencies....

i would predict a solution would be in your case averaging the frequencies of the incoming signal at an appropriate sampling rate using the EEPROM to store individual values, and sorting them into 3 sets. then you can make a few if() conditions in a seperate sketch using the data you collected in the averaging sketch to assign RGB values according to where the incoming signal's rank in the 1 one of the three sets of data collected.

I bought this sensor last week so i will post code if i can get RGB values, if it does produce a continous spectrum of frequencies in the read out i would predict that being a choice by the designer to allow for far more flexibility in the range of ways the sensor can be interfaced into projects rather than limiting it to only RGB values, at some point someone has to write code that handles an analog signal using digital logic theres no way around it, but they might have left that up to you.

You cannot get an instant answer from the sensor because it has to measure the frequency of the signal for R, G and B separately. The shorter your sampling time the less accurate the frequency measurement and the more inaccurate your color detection. So it becomes a balance between speed and accuracy.

If you want a more full explanation of how to calibrate the sensor, see the pdf documentation in my code library for the sensor, links in the signature block. There are also code examples for color recognition and calibration with the library.

ah!. i was sampeling in 50 ms. :stuck_out_tongue: