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!!!