Go Down

Topic: How to display a String to Arduino TFT screen? (Read 1 time) previous topic - next topic

uhrbahn

Aug 23, 2013, 01:57 pm Last Edit: Aug 25, 2013, 06:32 pm by uhrbahn Reason: 1
Hi Everyone,

I`m trying to read out a 100kOhm NTC-Sensor by Steinhart-Hart-Equatation and using the 3.3V voltage pin as an analog reference.
The MEGA2560-R3 board is in use and i`m trying to display the values of the sensor to the Arduino TFT LCD Screen.

I finally got acceptable values from the sensor to the Serial Monitor, but have no clue how to give that values to the TFT.
So could Anyone please have a look at my code, for telling me if i need to change more than the underlined Codeline?

Thanks for your Help!



Code: [Select]
 
/*
NTC 100kohm, 160x128pixel LCD-Display,
LCD-Anschl├╝sse;
*LCD MISO-PIN   an Arduino digital-Pin50
*LCD SCK-PIN    an Arduino digital-Pin52
*LCD MOSI-PIN   an Arduino digital-Pin51
*LCD CS-PIN      an Arduino PMW-Pin10
*LCD D/C-PIN    an Arduino PMW-Pin9
*LCD RESET-PIN an Arduino PMW-Pin8
*LCD BL-PIN      an +5V
*/

// which analog pin to connect
#define THERMISTORPIN A0        
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000      
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25  
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 10
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 4092
// the value of the 'other' resistor
#define SERIESRESISTOR 1000    

#include <TFT.h>  // Arduino LCD library
#include <SPI.h>

 // pin definition for the Mega
#define cs   10
#define dc   9
#define rst  8

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char sensorPrintout[4];

int samples[NUMSAMPLES];

void setup(void) {
 Serial.begin(9600);
 analogReference(EXTERNAL);
 TFTscreen.begin(); // Put this line at the beginning of every sketch that uses the GLCD:
 TFTscreen.background(0, 0, 0); // clear the screen with a black background
// write the static text to the screen
 TFTscreen.stroke(255,255,255); // set the font color to white
 TFTscreen.setTextSize(2); // set the font size
 TFTscreen.text("Sensor Value :\n ",0,0);  // write the text to the top left corner of the screen
 TFTscreen.setTextSize(5); // ste the font size very large for the loop
}

void loop(void) {
 uint8_t i;
 float average;

 // take N samples in a row, with a slight delay
 for (i=0; i< NUMSAMPLES; i++) {
  samples[i] = analogRead(THERMISTORPIN);
  delay(10);
 }

 // average all the samples out
 average = 0;
 for (i=0; i< NUMSAMPLES; i++) {
    average += samples[i];
 }
 average /= NUMSAMPLES;

 Serial.print("Average analog reading ");
 Serial.println(average);

 // convert the value to resistance
 average = 1023 / average - 1;
 average = SERIESRESISTOR / average;
 Serial.print("Thermistor resistance ");
 Serial.println(average);

 float steinhart;
 steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
 steinhart = log(steinhart);                  // ln(R/Ro)
 steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
 steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
 steinhart = 1.0 / steinhart;                 // Invert
 steinhart -= 273.15;                         // convert to C

 Serial.print("Temperature ");
 Serial.print(steinhart);
 Serial.println(" *C");

 delay(1000);

[u] // Read the value of the sensor on A0
 String sensorVal = String(analogRead(A0));[/u]

 // convert the reading to a char array
 sensorVal.toCharArray(sensorPrintout, 4);

 // set the font color
 TFTscreen.stroke(255,255,255);
 // print the sensor value
 TFTscreen.text(sensorPrintout, 0, 20);
 // wait for a moment
 delay(250);
 // erase the text you just wrote
 TFTscreen.stroke(0,0,0);
 TFTscreen.text(sensorPrintout, 0, 20);

}



guix

#1
Aug 23, 2013, 02:33 pm Last Edit: Aug 23, 2013, 02:40 pm by guix Reason: 1
Hello and welcome

Why do you create a String to then convert it to a char array? Can't you just use the char array?

And before the line
Code: [Select]

String sensorVal = String(analogRead(A0));

you have code to print the averaged sensor value to the serial monitor... So instead of printing to serial monitor, print it to your display.

Convert float to char array with dtostrf

uhrbahn

Hello guix and thanks for reception  :)

Right, there is no need for creating a String and convert it to a char array.

Can you please give ma a hint on how to print the averaged sensor value to my display instead of printing
to the serial monitor?


Thanks!






mrburnette


uhrbahn

The display text examplehttp://arduino.cc/en/Tutorial/TFTDisplayText says that i "need to convert the string content to a char array, storing it in the global array i declared in the beginning of my program."

But there is no need to create a String to then convert it to a char array as guix already said.
And as guix also said is it possible to convert float to char array with dtostrf, but my
ArduinoIDE seems to doesn`t know this command and my IDE also seems to doesn`know the
<stdlib>-library..
Any Help?

guix


Can you please give ma a hint on how to print the averaged sensor value to my display instead of printing
to the serial monitor?


Proably something like that:
Code: [Select]

 ...
 Serial.print("Temperature ");
 Serial.print(steinhart);
 Serial.println(" *C");


 char valueText[8]; //enough space for "-XXX.XX" + 1 for the null terminator
 dtostrf( steinhart, 1, 2, valueText ); // convert float to char array (with 2 digits precision)
 TFTscreen.text( valueText, 0, 20 );
 ...


Untested, but should work...


And just because the word "dtostrf" doesn't appear colored in the IDE doesn't mean it's not recognized. If it compiles without errors, it is recognized ^_^

uhrbahn

#6
Aug 25, 2013, 07:10 pm Last Edit: Aug 25, 2013, 07:19 pm by uhrbahn Reason: 1
Merci beaucoup, Monsieur Guix!

:*

Go Up