Help to show the values returned from TCS34725 sensor in an I2C display

Hi all!! I'm a phd student with a degree in chemistry, and for my phd thesis I need to use Arduino, but my knowledge of Arduino is really really basic, so for first, sorry if the questions seems simple!! My device is composed by a DF Robot 1602 RGB display with the backlight colored as the color returned from the TCS34725 Color sensor: if the color sensor detects red, the backlight of the display is red and so on. My Arduino sketch at the moment is organized in such ways that the RGB values returned from the sensor can be displayed in the serial monitor, but I want to read these values on the DFRobot 1602 display . I've organized the Arduibo sketch in the following way:

* CODE TO CONTROL THE COLOR PICKER */

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

/* INCLUDING THE LIBRARIES */

#include <Wire.h>;

#include <Adafruit_TCS34725.h>; /* TCS34725 Color Sensor Library */

#include <DFRobot_RGBLCD1602.h>; /* DFrobot Display Library */

#include <BasicLinearAlgebra.h>; /* library to calculate the calibration matrix */

/* VARIABLES DECLARATION */

char comando;
char read = 1;
int data;
unsigned long r; /* Declaring the red, green and blue values unsigned long variables */
unsigned long g;
unsigned long b;
unsigned long c;
unsigned long red01;
unsigned long green01;
unsigned long blue01;
double red_normalized;
double green_normalized;
double blue_normalized;
double red_calibrated;
double green_calibrated;
double blue_calibrated;
unsigned long multiplier;
unsigned long factor;
int red_rounded;
int green_rounded;
int blue_rounded;

/* SPECIFYING THAT WE ARE USING THE NAMESPACE BLA */

using namespace BLA;

/* PERIPHERALS INITIALIZATION */

/* TCS34725 definition with default values (integration time= 2.4 ms and gain = 1x) */

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);

/* DFRobot definition (16 characters and two rows) */

DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x60 ,/*lcdCols*/16,/*lcdRows*/2);

/*   */




void setup(void) {
Serial.begin(9600); /* opening of the serial port and setting the data rata to 9600 */

/* Initializating the display DFRobot */

lcd.init();

lcd.print("Color Picker"); /* Printing a message on the lcd screen */


/* Checking and warning if the connections of the sensor are right or not. 
If that condition gives value 1, the connections are correct,
otherwise the wiring needs to be controlled */

if (tcs.begin()) {
Serial.println("Found TCS34725");
} else {
Serial.println("No TCS34725 found ... check your connections");
while(1);
}

}


/* Starting to get the red, green, blue and clear values */


void loop(void) { 
  uint16_t r,g,b,c; /* Defining red, green and blue as variables */

  /* NORMALIZING TO THE CLEAR THE r, g, b VALUES */
  
  multiplier = 255;
  factor = multiplier * 1000000 / c;
  red01 = factor * r;
  green01 = factor * g;
  blue01 = factor * b;
  red_normalized = red01 / 1000000;
  green_normalized = green01 / 1000000;
  blue_normalized = blue01 / 1000000;

  /* CALIBRATING THE NORMALIZED VALUES */

  BLA::Matrix<3, 3> M = { 1.5740,	-0.1524,	-0.1751, -0.4020,	1.5593,	-0.4360, -0.1184,	0.0212,	1.0005}; /* declaring the matrix A 3 rows and 3 columns */

  BLA::Matrix<3, 1> v = { red_normalized, green_normalized, blue_normalized }; /* declaring the column vector */

  BLA::Matrix<3, 1> D = M * v; /* moltiplication of the calibration matrix with the data from the sensor */

  red_calibrated = D(0);

  green_calibrated = D(1);

  blue_calibrated = D(2);

  /* Rounding the calibrated values */

  red_rounded = round(red_calibrated);
  green_rounded = round(green_calibrated);
  blue_rounded = round(blue_calibrated);





  
if (read){
  tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
  Serial.print("Red: "); Serial.print(red_rounded, DEC); Serial.print(" ");
  Serial.print("Green: "); Serial.print(green_rounded, DEC); Serial.print(" ");
  Serial.print("Blue: "); Serial.print(blue_rounded, DEC); Serial.print(" ");
  Serial.print("Clear: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");

  /* setting the Red, Green and Blue values to the Backlight of DFRobot */
  lcd.setRGB(red_rounded, green_rounded, blue_rounded);
}

  

  if (Serial.available()>0){
    comando=Serial.read();
    switch (comando){
      case 'R':
      tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
      Serial.print("Red: "); Serial.print(red_rounded, DEC); Serial.print(" ");
  Serial.print("Green: "); Serial.print(green_rounded, DEC); Serial.print(" ");
  Serial.print("Blue: "); Serial.print(blue_rounded, DEC); Serial.print(" ");
  Serial.print("Clear: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");

  /* setting the Red, Green and Blue values to the Backlight of DFRobot */

  lcd.setRGB(red_rounded, green_rounded, blue_rounded);


 /* printing on the display the RGB values */


void showDataLCD(void){
lcd.clear();
lcd.setCursor (0,0);
lcd.print("R");
lcd.print(red);
lcd.setCursor (6,0);
lcd.print("G");
lcd.print(grn);
lcd.setCursor (12,0);
lcd.print("B");
lcd.print(blu);  
delay(2000);
 }




    

  break;


  case 'W':
  data=Serial.parseInt();
  break;

  case 'G':
  comando=Serial.read();
  tcs.setGain(comando);
  break;
  case 'C':
    read=1;
    
    break;
  case 'S':
    read=0;
    break;


    }
  
  }

}
}



but I receive the following error message: 

C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino: In function 'void loop()':
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino:160:23: error: a function-definition is not allowed here before '{' token
void showDataLCD(void){
^
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino: At global scope:
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino:204:1: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: a function-definition is not allowed here before '{' token

Could you help me please to correct the code?

Really really thanks for your kind help!!!

Correct indentation is the trick here. Indentation doesn't change the meaning of the code in any way, and won't fix an error by itself. But it will help you spot where the error is. With this type of error, it's not always where the error message says it thinks it is.

Classic signs of incorrect indentation include:

  • Sudden changes in the indentation where there is no { or }
      Serial.print("Red: "); Serial.print(red_rounded, DEC); Serial.print(" ");
  Serial.print("Green:
  • Multiple } at the same level of indentation, for example at the end of your code:
  }

}
}

So... click Tools->Auto Format and then post your code again. Do this frequently, and especially before you post code on the forum.

Really really thanks for your kind help!!! I'm really new in programming, and so I'm not practiacal!!! I've used the function auto format and I post again the sketch and the error message:

/* CODE TO CONTROL THE COLOR PICKER */

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

/* INCLUDING THE LIBRARIES */

#include <Wire.h>;

#include <Adafruit_TCS34725.h>; /* TCS34725 Color Sensor Library */

#include <DFRobot_RGBLCD1602.h>; /* DFrobot Display Library */

#include <BasicLinearAlgebra.h>; /* library to calculate the calibration matrix */

/* VARIABLES DECLARATION */

char comando;
char read = 1;
int data;
unsigned long r; /* Declaring the red, green and blue values unsigned long variables */
unsigned long g;
unsigned long b;
unsigned long c;
unsigned long red01;
unsigned long green01;
unsigned long blue01;
double red_normalized;
double green_normalized;
double blue_normalized;
double red_calibrated;
double green_calibrated;
double blue_calibrated;
unsigned long multiplier;
unsigned long factor;
int red_rounded;
int green_rounded;
int blue_rounded;

/* SPECIFYING THAT WE ARE USING THE NAMESPACE BLA */

using namespace BLA;

/* PERIPHERALS INITIALIZATION */

/* TCS34725 definition with default values (integration time= 2.4 ms and gain = 1x) */

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);

/* DFRobot definition (16 characters and two rows) */

DFRobot_RGBLCD1602 lcd(/*RGBAddr*/ 0x60, /*lcdCols*/ 16, /*lcdRows*/ 2);

/*   */




void setup(void) {
  Serial.begin(9600); /* opening of the serial port and setting the data rata to 9600 */

  /* Initializating the display DFRobot */

  lcd.init();

  lcd.print("Color Picker"); /* Printing a message on the lcd screen */


  /* Checking and warning if the connections of the sensor are right or not. 
If that condition gives value 1, the connections are correct,
otherwise the wiring needs to be controlled */

  if (tcs.begin()) {
    Serial.println("Found TCS34725");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1)
      ;
  }
}


/* Starting to get the red, green, blue and clear values */


void loop(void) {
  uint16_t r, g, b, c; /* Defining red, green and blue as variables */

  /* NORMALIZING TO THE CLEAR THE r, g, b VALUES */

  multiplier = 255;
  factor = multiplier * 1000000 / c;
  red01 = factor * r;
  green01 = factor * g;
  blue01 = factor * b;
  red_normalized = red01 / 1000000;
  green_normalized = green01 / 1000000;
  blue_normalized = blue01 / 1000000;

  /* CALIBRATING THE NORMALIZED VALUES */

  BLA::Matrix<3, 3> M = { 1.5740, -0.1524, -0.1751, -0.4020, 1.5593, -0.4360, -0.1184, 0.0212, 1.0005 }; /* declaring the matrix A 3 rows and 3 columns */

  BLA::Matrix<3, 1> v = { red_normalized, green_normalized, blue_normalized }; /* declaring the column vector */

  BLA::Matrix<3, 1> D = M * v; /* moltiplication of the calibration matrix with the data from the sensor */

  red_calibrated = D(0);

  green_calibrated = D(1);

  blue_calibrated = D(2);

  /* Rounding the calibrated values */

  red_rounded = round(red_calibrated);
  green_rounded = round(green_calibrated);
  blue_rounded = round(blue_calibrated);






  if (read) {
    tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
    Serial.print("Red: ");
    Serial.print(red_rounded, DEC);
    Serial.print(" ");
    Serial.print("Green: ");
    Serial.print(green_rounded, DEC);
    Serial.print(" ");
    Serial.print("Blue: ");
    Serial.print(blue_rounded, DEC);
    Serial.print(" ");
    Serial.print("Clear: ");
    Serial.print(c, DEC);
    Serial.print(" ");
    Serial.println(" ");

    /* setting the Red, Green and Blue values to the Backlight of DFRobot */
    lcd.setRGB(red_rounded, green_rounded, blue_rounded);
  }



  if (Serial.available() > 0) {
    comando = Serial.read();
    switch (comando) {
      case 'R':
        tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
        Serial.print("Red: ");
        Serial.print(red_rounded, DEC);
        Serial.print(" ");
        Serial.print("Green: ");
        Serial.print(green_rounded, DEC);
        Serial.print(" ");
        Serial.print("Blue: ");
        Serial.print(blue_rounded, DEC);
        Serial.print(" ");
        Serial.print("Clear: ");
        Serial.print(c, DEC);
        Serial.print(" ");
        Serial.println(" ");

        /* setting the Red, Green and Blue values to the Backlight of DFRobot */

        lcd.setRGB(red_rounded, green_rounded, blue_rounded);


        /* printing on the display the RGB values */


        void showDataLCD(void) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("R");
          lcd.print(red_rounded);
          lcd.setCursor(6, 0);
          lcd.print("G");
          lcd.print(green_rounded);
          lcd.setCursor(12, 0);
          lcd.print("B");
          lcd.print(blu_rounded);
          delay(2000);
        }






        break;


      case 'W':
        data = Serial.parseInt();
        break;

      case 'G':
        comando = Serial.read();
        tcs.setGain(comando);
        break;
      case 'C':
        read = 1;

        break;
      case 'S':
        read = 0;
        break;
    }
  }
}
}



with the following message:


C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino: In function 'void loop()':
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino:176:32: error: a function-definition is not allowed here before '{' token
         void showDataLCD(void) {
                                ^
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino: At global scope:
C:\Users\MSI\Desktop\appoggio\614msrounded\Aveiro02\Aveiro02.ino:216:1: error: expected declaration before '}' token
 }
 ^

exit status 1

Compilation error: a function-definition is not allowed here before '{' token

it seems that the problem is some { but I can't understand where...

Please use separate sets of code tags for your code and the error messages. Never put your comments or questions inside code tags, they can easily be missed and it is confusing!

ok!!! when I've edited the message I've done some confusion...


    switch (comando) {
      case 'R':

. . .

        /* printing on the display the RGB values */

        void showDataLCD(void) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("R");
          lcd.print(red_rounded);
          lcd.setCursor(6, 0);
          lcd.print("G");
          lcd.print(green_rounded);
          lcd.setCursor(12, 0);
          lcd.print("B");
          lcd.print(blu_rounded);
          delay(2000);
        }


        break;

  • You cannot define a function within a switch/case

Functions should never be indented (in C/C++). Look how setup() and loop() are not intended:

void setup(void) {
  Serial.begin(9600); /* opening of the serial port and setting the data rata to 9600 */

and

void loop(void) {
  uint16_t r, g, b, c; /* Defining red, green and blue as variables */

But this function is indented:

/* printing on the display the RGB values */


        void showDataLCD(void) {
          lcd.clear();

So why has Auto Format done that? It is because Auto Format follows the { and }. If a function is indented, that means there is one or more } missing in the code lines above. You need to add more } and hit Auto Format again until the function is no longer indented.

But if you ever see two or more } at the end of a function and they are vertically above each other and not indented, that means you have too many } in the code lines above.

Does that mean you can simply add or remove { or } at the end of the previous function? Maybe, but often not. You need to figure out exactly where any additional } should be. But at least you know approximately where they should be.

  • Put the function below loop( ), outside of other functions.

Also, any lines of code with the same level of indentation are part of the same block and get executed together.

The IDE has another useful feature to help you figure out your { and }. If you put the caret just after a } or {, the IDE will highlight the matching } or {. That may be off the top of the screen, of course!

I've modified the code in the following way:

/* CODE TO CONTROL THE COLOR PICKER */

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

/* INCLUDING THE LIBRARIES */

#include <Wire.h>;

#include <Adafruit_TCS34725.h>; /* TCS34725 Color Sensor Library */

#include <DFRobot_RGBLCD1602.h>; /* DFrobot Display Library */

#include <BasicLinearAlgebra.h>; /* library to calculate the calibration matrix */

/* VARIABLES DECLARATION */

char comando;
char read = 1;
int data;
unsigned long r; /* Declaring the red, green and blue values unsigned long variables */
unsigned long g;
unsigned long b;
unsigned long c;
unsigned long red01;
unsigned long green01;
unsigned long blue01;
double red_normalized;
double green_normalized;
double blue_normalized;
double red_calibrated;
double green_calibrated;
double blue_calibrated;
unsigned long multiplier;
unsigned long factor;
int red_rounded;
int green_rounded;
int blue_rounded;

/* SPECIFYING THAT WE ARE USING THE NAMESPACE BLA */

using namespace BLA;

/* PERIPHERALS INITIALIZATION */

/* TCS34725 definition with default values (integration time= 2.4 ms and gain = 1x) */

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);

/* DFRobot definition (16 characters and two rows) */

DFRobot_RGBLCD1602 lcd(/*RGBAddr*/ 0x60, /*lcdCols*/ 16, /*lcdRows*/ 2);

/*   */




void setup(void) {
  Serial.begin(9600); /* opening of the serial port and setting the data rata to 9600 */

  /* Initializating the display DFRobot */

  lcd.init();

  lcd.print("Color Picker"); /* Printing a message on the lcd screen */


  /* Checking and warning if the connections of the sensor are right or not. 
If that condition gives value 1, the connections are correct,
otherwise the wiring needs to be controlled */

  if (tcs.begin()) {
    Serial.println("Found TCS34725");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1)
      ;
  }
}


/* Starting to get the red, green, blue and clear values */


void loop(void) {
  uint16_t r, g, b, c; /* Defining red, green and blue as variables */

  /* NORMALIZING TO THE CLEAR THE r, g, b VALUES */

  multiplier = 255;
  factor = multiplier * 1000000 / c;
  red01 = factor * r;
  green01 = factor * g;
  blue01 = factor * b;
  red_normalized = red01 / 1000000;
  green_normalized = green01 / 1000000;
  blue_normalized = blue01 / 1000000;

  /* CALIBRATING THE NORMALIZED VALUES */

  BLA::Matrix<3, 3> M = { 1.5740, -0.1524, -0.1751, -0.4020, 1.5593, -0.4360, -0.1184, 0.0212, 1.0005 }; /* declaring the matrix A 3 rows and 3 columns */

  BLA::Matrix<3, 1> v = { red_normalized, green_normalized, blue_normalized }; /* declaring the column vector */

  BLA::Matrix<3, 1> D = M * v; /* moltiplication of the calibration matrix with the data from the sensor */

  red_calibrated = D(0);

  green_calibrated = D(1);

  blue_calibrated = D(2);

  /* Rounding the calibrated values */

  red_rounded = round(red_calibrated);
  green_rounded = round(green_calibrated);
  blue_rounded = round(blue_calibrated);






  if (read) {
    tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
    Serial.print("Red: ");
    Serial.print(red_rounded, DEC);
    Serial.print(" ");
    Serial.print("Green: ");
    Serial.print(green_rounded, DEC);
    Serial.print(" ");
    Serial.print("Blue: ");
    Serial.print(blue_rounded, DEC);
    Serial.print(" ");
    Serial.print("Clear: ");
    Serial.print(c, DEC);
    Serial.print(" ");
    Serial.println(" ");

    /* setting the Red, Green and Blue values to the Backlight of DFRobot */
    lcd.setRGB(red_rounded, green_rounded, blue_rounded);
  }



  if (Serial.available() > 0) {
    comando = Serial.read();
    switch (comando) {
      case 'R':
        tcs.getRawData(&r, &g, &b, &c); /* getting the data from the TCS34725 */
        Serial.print("Red: ");
        Serial.print(red_rounded, DEC);
        Serial.print(" ");
        Serial.print("Green: ");
        Serial.print(green_rounded, DEC);
        Serial.print(" ");
        Serial.print("Blue: ");
        Serial.print(blue_rounded, DEC);
        Serial.print(" ");
        Serial.print("Clear: ");
        Serial.print(c, DEC);
        Serial.print(" ");
        Serial.println(" ");

        /* setting the Red, Green and Blue values to the Backlight of DFRobot */

        lcd.setRGB(red_rounded, green_rounded, blue_rounded);











        break;


      case 'W':
        data = Serial.parseInt();
        break;

      case 'G':
        comando = Serial.read();
        tcs.setGain(comando);
        break;
      case 'C':
        read = 1;

        break;
      case 'S':
        read = 0;
        break;
    }
    void showDataLCD(void) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("R");
      lcd.print(red_rounded);
      lcd.setCursor(6, 0);
      lcd.print("G");
      lcd.print(green_rounded);
      lcd.setCursor(12, 0);
      lcd.print("B");
      lcd.print(blu_rounded);
      delay(2000);
    }
  }
}
}

but I didn't do right: i receive the error message...I have to find another location..

ok, I will check the } and I will control if thir number is right...
Really really thanks for your help!!

I try that function and I check if I can solve!!

Now the sketch is correct, I don't receive error message !!!
Really really thanks for you kind help and for you really clear explanation that has been really helpful :blush:!!!

Really really thanks for your kind explanation: it has been really helpful :blush: :blush: :blush:

I've tried and I could check the { symbols!! really really thanks for you kind help, sincerely :blush: :blush: :blush: :blush:

If you found a final solution, please mark the thread as solved

OK!!! really really thanks for your kind support!!!