Hi all,
I'm working on a project reading the illuminance values from a light meter and display it in real time. When displaying the illuminance value on Serial Monitor, I stored the received data from the light meter into an array and then read the part of the array I needed(only need illumiance) using 'for' loop. It works well. As shown in the serial monitor results below, only the 685.5 is the value that I what.
Serial Monitor Result:
I received: 685.5 lux
I received: 685.5 lux
I received: 685.5 lux
However, when I try to use the TFTscreen.text() function to display the illuminance value on the TFT screen, the displayed information is more than what I wanted because in addition to illuminance value, it also includes other data( 685.5 .3961 ). I defined a new array called luxChars[5] and then store the data from receivedChars[6~10] into luxChars[5], which is used in the TFTscreen.text() function to print the value on the TFT screen. Even though the size of luxchars array is only 5, the displayed characters are much more than 5. So does anyone know how to fix this?
Here is my code:
#include <SPI.h>
#include <TFT.h>
#define cs 10 //pin defintion for Arduino
#define dc 9
#define rst 8
TFT TFTscreen = TFT(cs, dc, rst);
char receivedChars[13];
char luxChars[5];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600,SERIAL_7E1); //according to the light meter communication method
TFTscreen.begin();
TFTscreen.background(0, 0, 0);
TFTscreen.stroke(255, 255, 255);
TFTscreen.setTextSize(1);
TFTscreen.text("Illuminance:\n ", 35, 10);
TFTscreen.setTextSize(1);
}
void loop()
{
LuxReading(); //Call LuxReading
delay(500); // if I delete this delay code, it won't work. So it seems it takes some time to receive the data
TFTscreen.stroke(255, 255, 255);
TFTscreen.text(luxChars, 40, 30);
delay(250);
TFTscreen.stroke(0, 0, 0);
TFTscreen.text(luxChars, 40, 30);
}
void LuxReading() //Lux Reading Function
{
int ndx = 0; //variable for receivedChars array
int a = 0; //variable for luxChars array
while (Serial1.available() > 0) //when press the D-OUT button of the light meter, read data
{
char c = Serial1.read(); // store received data into array
if (c != '\n')
{
receivedChars[ndx] = c;
ndx++;
}
else
{
Serial.print("I received: "); // print received data on serial monitor
for (int i=6; i<=10; i++)
{
Serial.print(receivedChars[i]);
luxChars[a] = receivedChars[i];
a++;
}
a = 0;
Serial.println(" lux"); // print illuminance unit
}
}
}
Thanks in advance.