TCS3200 Program Confusing

Hey everyone,

I'm trying to program the TCS3200 and come across this program made by another person, and i'm a bit confused at the amount of count++ and i++ there are and why there are so many "for" statements. The variables are incremented, then other things happen, and I just don't get why they are incremented. Here's the code:

#define S2 7
#define S3 8
#define OUT 10
#define LED 9
#define  Filters_R  0
#define  Filters_G  1
#define  Filters_B  2

int Count[3] = {0};
long count = 0;
int counter = 0;
long start_time = 0;
int G_flag = 1;
int RGB = 0;
long freq = 0;

void setup()
{
  pinMode(OUT, INPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, LOW);

  Serial.begin(9600);
  Serial.println("Starting...");
}

void loop()
{
  Serial.println("loop");
  for(int i=0; i<3; i++)
  {
    RGB=i;
    Select_Filters(i);
    start_time = millis();
    count=0;
    for(int i=start_time; i<start_time+10000; i++)
    {
      if(digitalRead(10) == HIGH)
      {
        count++;
      }
      counter++;
    }
    freq = count / 7200;
    Serial.print("value: ");
    Serial.println(count % 255);
    Serial.print("count: ");
    Serial.println(count);
    Serial.print("counter ");
    Serial.println(counter);
    counter=0;
    count=0;
    freq=0;
  }
  delay(3000);
}

void Select_Filters(int RGB)
{
  switch(RGB)
  {
    case Filters_R:          //Red
    digitalWrite(S2, LOW);    
    digitalWrite(S3, LOW);
    Serial.println("-----select Red color");
    break;
    
    case Filters_G:          //Green
    digitalWrite(S2, HIGH);    
    digitalWrite(S3, HIGH);
    Serial.println("-----select Green color");
    break;
    
    case Filters_B:          //Blue
    digitalWrite(S2, LOW);    
    digitalWrite(S3, HIGH);
    Serial.println("-----select Blue color");
    break;
    
    default:                  //Clear(no filter)
    digitalWrite(S2, HIGH);    
    digitalWrite(S3, LOW);
    Serial.println("-----no filter");
    break;
  }
}

void callback()
{
  count++;
}

With the increments, I'm also confused why there is a callback() statement at the end...

No idea about the callback - looks like code copied and left behind on edit.

The sensor works by sending back a frequency proportional to the intensity of the selected colour (filter). The code is counting the number of 'HIGH' values it finds in one second (1000 ms) and using that as the frequency.

IMHO this is bad coding practice and will give incorrect results. Have a look at my library for this sensor (link below) and read the doc that comes with it on how the sensor should be calibrated.

marco_c:
No idea about the callback - looks like code copied and left behind on edit.

The sensor works by sending back a frequency proportional to the intensity of the selected colour (filter). The code is counting the number of 'HIGH' values it finds in one second (1000 ms) and using that as the frequency.

IMHO this is bad coding practice and will give incorrect results. Have a look at my library for this sensor (link below) and read the doc that comes with it on how the sensor should be calibrated.

Ok, thanks.

I don't get it,

when I calibrate the color sensor, it shows up as 0,0,0 for the results for the results. I have all the pins connected, but then when I try measuring a color, it keeps showing up with 0,0,0. Also, whenever I upload a program, this message pops up: WARNING: Category 'Sensor Control' in library MD_TCS230 is not valid. Setting to 'Uncategorized'. I'm a novice at this stuff, and but I don't know what this means. I properly installed the library (or that's what i thought...), so why would this come up? This code is the calibration code and it's from the user marco_c. I give the credit to him. Here is the code:

// TCS230 sensor calibration and color readings
//
// Input and output using the Serial console.
//
#include <MD_TCS230.h>
#include <FreqCount.h>

#define BLACK_CAL 0
#define WHITE_CAL 1
#define READ_VAL 2

// Pin definitions
#define  S2_OUT  12
#define  S3_OUT  13
#define  OE_OUT   8    // LOW = ENABLED 

MD_TCS230 CS(S2_OUT, S3_OUT, OE_OUT);

void setup() 
{
  Serial.begin(57600);
  Serial.print(F("n[TCS230 Calibrator Example]"));

  CS.begin();
}

char getChar()
// blocking wait for an input character from the input stream
{
 while (Serial.available() == 0)
 ;
 return(toupper(Serial.read()));
}

void clearInput()
// clear all characters from the serial input
{
 while (Serial.read() != -1)
 ;
}

uint8_t fsmReadValue(uint8_t state, uint8_t valType, uint8_t maxReads)
// Finite State Machine for reading a value from the sensor
// Current FSM state is passed in and returned
// Type of value being read is passed in
{
 static uint8_t selChannel;
 static uint8_t readCount;
 static sensorData sd;

 switch(state)
 {
 case 0: // Prompt for the user to start
 Serial.print(F("nnReading value for "));
 switch(valType)
 {
 case BLACK_CAL: Serial.print(F("BLACK calibration")); break;
 case WHITE_CAL: Serial.print(F("WHITE calibration")); break;
 case READ_VAL: Serial.print(F("DATA")); break;
 default: Serial.print(F("??")); break;
 }
 
 Serial.print(F("nPress any key to start ..."));
 state++;
 break;

 case 1: // Wait for user input
 getChar();
 clearInput();
 state++;
 break;

 case 2: // start the reading process
 CS.read();
 state++;
 break;

 case 3: // wait for a read to complete
 if (CS.available()) 
 {
 sensorData sd;
 colorData rgb;

 switch(valType)
 {
 case BLACK_CAL: 
 CS.getRaw(&sd); 
 CS.setDarkCal(&sd); 
 break;

 case WHITE_CAL: 
 CS.getRaw(&sd); 
 CS.setWhiteCal(&sd); 
 break;

 case READ_VAL: 
 CS.getRGB(&rgb);
 Serial.print(F("nRGB is ["));
 Serial.print(rgb.value[TCS230_RGB_R]);
 Serial.print(F(","));
 Serial.print(rgb.value[TCS230_RGB_G]);
 Serial.print(F(","));
 Serial.print(rgb.value[TCS230_RGB_B]);
 Serial.print(F("]"));
 break;
 }
 state++;
 }
 break;

 default: // reset fsm
 state = 0;
 break;
 }

 return(state);
}


void loop() 
{
  static uint8_t runState = 0; 
  static uint8_t readState = 0;

  switch(runState)
  {
  case 0: // calibrate black
  readState = fsmReadValue(readState, BLACK_CAL, 2);
  if (readState == 0) runState++;
  break;

  case 1: // calibrate white
  readState = fsmReadValue(readState, WHITE_CAL, 2);
  if (readState == 0) runState++;
  break;

  case 2: // read color
  readState = fsmReadValue(readState, READ_VAL, 1);
  break;

  default:
  runState = 0; // start again if we get here as something is wrong
  }
}

What pin are you connecting the output from the sensor to? This will depend on the type of Arduino you are using and is documented in the freqcount library header file or their web site.

brokenAvocado:
Also, whenever I upload a program, this message pops up: WARNING: Category 'Sensor Control' in library MD_TCS230 is not valid. Setting to 'Uncategorized'. I'm a novice at this stuff, and but I don't know what this means. I properly installed the library (or that's what i thought...), so why would this come up?

You can safely ignore that warning. It has to do with an extra option for keywords files and doesn't affect the operation at all.

marco_c:
What pin are you connecting the output from the sensor to? This will depend on the type of Arduino you are using and is documented in the freqcount library header file or their web site.

I connected the s3 s2 and oe to their respected pins in the program, and also I have an arduino nano, and pin 12 and 13 are available and I tried different arduino nanos and tcs230 color sensors to make sure the ones I have aren't faulty, but nothing has changed. Are there any other reasons why the program isn't working?

No. The question is where have you connected the OUT, not the output enable (OE).

marco_c:
No. The question is where have you connected the OUT, not the output enable (OE).

in that case, pin 10

Does this agree with what the Freqcount documentation says it should be for your Arduino type? Please refer to the documentation. You only have the choice of 1 pin.

GND 4 Power Supply Ground. All Voltages are referenced to this ground
VDD 5 Supply Voltage (2.7-5.5V)
/OE 3 I Enable fO (active low). When OE is high the OUT becomes high impedance, allowing multiple sensors to share the same OUT line
OUT 6 O Output frequency fO
S0, S1 1, 2 I Output frequency scale selection inputs (see 0 0)
S2, S3 7, 8 I Photodiode (color filter) selection inputs (see 0 0)

That is what the chart in the documentation said. Also, why does it need to be these pins? But also, these pins aren't following the program and my color sensor's leds first come on then they switch off after I put in the program. what is this?

VDD 5 Supply Voltage (2.7-5.5V)

Are you connecting voltage to a pin? This should be to the +5V on the Arduino

OUT 6 O Output frequency fO

This needs to be the pin documented in FreqCount Library, for Measuring Frequencies in the 1 kHz to 5 MHz Range. For example, for a Uno this is pin 5.

The rest are arbitrary and you can do whatever as long as you tell the library what they are.

marco_c:
Are you connecting voltage to a pin? This should be to the +5V on the Arduino

Yes, I already know that.

marco_c:
This needs to be the pin documented...
...The rest are arbitrary and you can do whatever as long as you tell the library what they are.

It doesn't have the arduino nano, so what is the nearest board to an arduino nano, or what is the pin for the nano?

It doesn't have the arduino nano, so what is the nearest board to an arduino nano, or what is the pin for the nano?

I am guessing that that as the nano has the same processor as the Uno and the same pinouts for just about everything else, you could use the Uno.

Well i understood the whole program except for this spot: uint8_t fsmReadValue(uint8_t state, uint8_t valType, uint8_t maxReads)

I know what a finite state machine is, but first of all, does it come from the libraries? I haven't seen this in any of the key words for the two libraries. Also, is it possible to program uint8_t to hold a variable in this manner? But most importantly, I've seen data types hold functions, but is that possible? I'm still a newbie to the arduino language and still confused on some things.

That is a function definition. The code after it in the braces runs when you call that code and uses the arguments supplied. It's not from a library, that's a function that the author of this code is writing himself. You are looking at the part where he is creating that function.

Google "C++ functions" for some more explanation.

Delta_G:
That is a function definition. The code after it in the braces runs when you call that code and uses the arguments supplied. It's not from a library, that's a function that the author of this code is writing himself. You are looking at the part where he is creating that function.

Google "C++ functions" for some more explanation.

Oh so its literally part of Arduino?

brokenAvocado:
Oh so its literally part of Arduino?

No, it's a function he is creating in the sketch. Google "C++ functions". It's something you need to know if you're going to go much further with Arduino.

Delta_G:
No, it's a function he is creating in the sketch. Google "C++ functions". It's something you need to know if you're going to go much further with Arduino.

Is there anything else I should be aware of that's also from C++?

I know what a finite state machine is, but first of all, does it come from the libraries?

A FSM is just a concept, like "sorting". It can be implemented in many different ways using programming languages.

I haven't seen this in any of the key words for the two libraries.

As Delta_G says, this is defining a function, effectively adding a code block with parameters and calling it something.

Also, is it possible to program uint8_t to hold a variable in this manner? But most importantly, I've seen data types hold functions, but is that possible?

UINT8_T Unsigned INTeger 8 bits. The _t is a convention that is used to show this is a type. These types are defined in the library and allow for explicit sizing of variables in a portable way. 'Data type with function' is called a 'class'. Most Arduino 'libraries' are actually just classes.

I'm still a newbie to the arduino language and still confused on some things.

The Arduino language is basically just C++. I would encourage you to find a good online text or video tutorial to get familiar with this before you get too much further or you will get frustrated very quickly.