Pages: [1]   Go Down
Author Topic: How to display a String to Arduino TFT screen?  (Read 1215 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
 /*
 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);

}

« Last Edit: August 25, 2013, 11:32:44 am by uhrbahn » Logged

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1005
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: August 23, 2013, 07:40:48 am by guix » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guix and thanks for reception  smiley

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!





Logged

Atlanta, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1790
AKA: Ray Burne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the display text example:
http://arduino.cc/en/Guide/TFT
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

France
Offline Offline
Edison Member
*
Karma: 37
Posts: 1005
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 ...
  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 smiley-kitty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Merci beaucoup, Monsieur Guix!

 smiley-kiss
« Last Edit: August 25, 2013, 12:19:57 pm by uhrbahn » Logged

Pages: [1]   Go Up
Jump to: