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:
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.
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?
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?
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.
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.
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.