char* conversion

Hey guys so Im trying to display a sensor value on a sainsmart 1.8 lcd. In the .h file there is no command to print a uint32_t in the screen, only uint8_t, and uint16_t. So I'm not sure how to go about this.

In the .h file is:

tft.drawString(uint8_t x, uint8_t y, char *c, uint16_t color, uint8_t size=1);

the .CPP file for this shows:

//draw a string from memoryvoid ST7735: :drawString(uint8_t x, uint8_t y, char *c, uint16_t color, uint8_t size) {while (c[0] != 0)

Now unless there is another way you guys know how to do this, I was trying to convert the char*c to an int that hold the sensor val.

I have tried a bunch of ways i found on the web. none work. I even tried editing the .cpp and .h....I cant get it. Can anyonr help please!

okay this is what I got to work but it has to fill screen black after each update or it will write on top of each print. however this is making it blink ughh.... There is a tft.fillScreen(color) function but is updates slower than the fillRect.... any ideas????

#include <ST7735.h>
#include <SPI.h>
#include <stdlib.h>


#define cs 10
int
dc = 9;
#define rst 8
int O3 = 2;
#define O4 3
int  I0 = A0;
int I1 = A1;
ST7735 tft = ST7735(cs, dc, rst);  
char d[] = "1234";

String stringVal ="";

const int analogInPin1 = I1; // Analog input pin that the Accelerometer's
//first pin is attached to
const int analogInPin2 = I0; // Analog input pin that the Accelerometer's
//second pin is attached to
const int analogOutPin1= O3; // Analog output pin that the LED is attached

const int analogOutPin2= O4; // Analog output pin that the LED is attached
int VAL = 0;
int sensorValue1 = 0; // value read from the Accelerometer's first pin
int sensorValue2 = 0; // value read from the Accelerometer's second pin
int outputValue1 = 0; // value output to the PWM (analog out)
int outputValue2 = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
tft.initR();
tft.fillScreen(BLACK);
tft.writecommand(ST7735_DISPON);
tft.fillScreen(BLACK);
}
void loop() {
 
  char stR[10];
 sprintf( stR, "%d", sensorValue1);
  char Str[10];
 sprintf( Str, "%d", sensorValue2);
 
 
 tft.drawString(45,80, Str, WHITE,3);
  tft.drawString(45,110, stR, WHITE,3);
  
  delay(2);
  tft.fillRect(40,0, 60, 150, BLACK);
  
  // read the both analog in values:
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
// map it to the range of the analog out:

// change the analog out value:
analogWrite(analogOutPin1, outputValue1);
analogWrite(analogOutPin2, outputValue2);
// print the results to the serial monitor:
Serial.print("accelerometer X = " );
Serial.print(sensorValue1);
Serial.print("\t accelerometer Y = " );
Serial.print(sensorValue2);
Serial.print("\t output 1 = ");
Serial.print(outputValue1);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(0);
}

You could format the number into the string as a fixed width.
It depends on what you want the output to look like.

i.e.

sprintf(str, "%8d", num);

You could also zero fill instead of spaces by using:

sprintf(str, "%08d", num);

These will work as long as all of your digits are the same exact width in pixels.
By formatting as a fixed width string, it will ensure that the entire region used for digits will be overwitten when you output the string so you don't have to erase anything.
It also ensures that each of the digits always align in the same location.
i.e. the least significant "ones" digit will always be in the same location and will not bounce around depending on the overall value.

--- bill

Thanks for the response Bill!

Okay I gave that a shot and this is what happened....So say the 2 values I want to display are 3 digits long and centered on screen. If I try the "%8d" It moves that 3 digit value left a digit and down maybe 3 pixels. Still overwrites.. If I try the "%08d" the value is still moved but I get 5 zeros starting where the value should be.

I read this on :PHP: sprintf - Manual

Warning
Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results

Could this be my issue?

This is the LCD BTW.

I can't follow what you are getting on the display; however,
if you only want a field width of only 3 characters then use 3 not 8.
See the printf() documentations for the full description of the format string.
The number in the format string before the 'd' is the character field width. "8" means create a field width of exactly 8 characters.
The character is used to pad the unused character positions when the number is less than the field width.
When '0' is specified before the number it specifies to fill with zeros instead of spaces.
The number will be aligned to the right of the field.

Remember overwriting the display field using a fixed width string of characters will only work if each of the characters is exactly the same rendered pixel width.
i.e. each character: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and must all be rendered the same number of horizontal pixels.

Assuming they are all the same width, then you can simply re-write the string. The old characters will be overwritten and you don't have to do any sort of clearing of the field which causes flickering. Each update of writing the numbers will update the full field area on the display since it will exactly overwrite the previous characters.

BTW, this assumes that when drawing on the screen that the background of the font characters rendered as well vs being transparent. Some text rendering code simply OR's in the new pixels rather than draw the full character foreground and background pixels.
If you have this type of text rendering, then you will have no choice but to clear the area on each update.

But if you do have to clear the area where the characters are, you should do just before you update the characters, not after.
i.e.
clear area1
draw number in area 1
clear area 2
draw number in area 2.

This will minimize the flicker as much as possible.

--- bill

The warning message doesn't apply as that is for w_char character sets which use more than one byte per character. The arduino tools use regular ASCII which is all 1 byte per character.

Okay this is what I got to work. I found a new #include File that works for me that already existed in libraries called TFT.h .... WOW much better!!! by the way thank you!!!!

#include <TFT.h>

#include <SPI.h>
#include <stdlib.h>

int h =0;
int t =0;
#define cs 10
int
dc = 9;
#define rst 8
int O3 = 2;
#define O4 3
int  I0 = A0;
int I1 = A1;
  
char d[] = "1234";
char tempPrintout[6];
char humPrintout[6];
TFT TFTscreen = TFT(cs,dc,rst);
String stringVal ="";

const int analogInPin1 = I1; // Analog input pin that the Accelerometer's
//first pin is attached to
const int analogInPin2 = I0; // Analog input pin that the Accelerometer's
//second pin is attached to
const int analogOutPin1= O3; // Analog output pin that the LED is attached

const int analogOutPin2= O4; // Analog output pin that the LED is attached
int VAL = 0;
int sensorValue1 = 0; // value read from the Accelerometer's first pin
int sensorValue2 = 0; // value read from the Accelerometer's second pin
int outputValue1 = 0; // value output to the PWM (analog out)
int outputValue2 = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(115200);
// Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("AXIS (X)",0,0);

// write the text to the top left corner of the screen
TFTscreen.text("AXIS (Y)",0,60);
// ste the font size very large for the loop
TFTscreen.setTextSize(3);
}
void loop() {
 
 h = sensorValue1;
t = sensorValue2;

char stR[10];
 sprintf( stR, "%d", sensorValue1);
  char Str[10];
 sprintf( Str, "%d", sensorValue2);

String tempVal = String(t,5);
String humVal = String(h, 0);
// String sensorVal = String(1.234);

// convert the reading to a char array
tempVal.toCharArray(tempPrintout, 6);
humVal.toCharArray(humPrintout, 6);

// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(stR, 0, 25);
TFTscreen.text(Str, 0, 85);
// wait for a moment
delay(100);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(stR, 0, 25);
TFTscreen.text(Str, 0, 85);


  
  // read the both analog in values:
sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
// map it to the range of the analog out:

// change the analog out value:
analogWrite(analogOutPin1, outputValue1);
analogWrite(analogOutPin2, outputValue2);
// print the results to the serial monitor:
Serial.print("accelerometer X = " );
Serial.print(sensorValue1);
Serial.print("\t accelerometer Y = " );
Serial.print(sensorValue2);
Serial.print("\t output 1 = ");
Serial.print(outputValue1);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(0);
}

Maybe you can help with this.

I am trying now to draw a line.... In TFT folder is a utility folder that has Adafruit_GFX with a drawFastHLine(uint16_t x, uint16_t y, uint16_t w, uint16_t color);. Now on the last file I had I saw you say tft.drawline, now on TFT I have to use TFTscreen.Text(uint8_t, uint16_t x, uint16_t y);. So I see I have to write TFT to call the function.

So for the AdafruitGFX, what do I write before drawFastHLine(); how do I find what to write? I dont see in the beginning of the .h or .cpp what to do or say. I tried GFX, Adafruit_ GFX, Adafruit GFX, gfx, and TFT and it still says "_____" not declared.

on a second note their's TFTscreen=(cs,dc,rst);. If I write TFTscreen.stroke(255,255,255); thats the color white, (0,0,0) is black. Is there a chart somewhere what what equals what color?

Thanks!!!

These libraries use C++ objects.
Any API call you do references the object so the call is done as:

objectname.functionname(...);

The name of object is whatever you declare it to be.
In your first code you called the object "tft"

ST7735 tft = ST7735(cs, dc, rst);

In your second example code you called object "TFTscreen"

TFT TFTscreen = TFT(cs,dc,rst);

You can call the object anything you want but that will determine what you use to make the API calls.
To see the supported functions in the API for a library class see the documentation for the library.
If the library writer was lazy and didn't write it, then you will have to look in the .h file for the library.
When looking in the .h file look at the public functions as those will be ones you can call when using an object that is declared for that class.
If the class includes other classes, you will then have to look in the header for that class to see what functions are provided by the inherited class.

--- bill

Okay well I kinda got ya but I still cant get it. I now see the #include <Adafruit_GFX> so I have it set as Adafruit_GFX GFX = Adafruit_GFX( What goes here? )

I tried Adafruit_GFX(w,h); as it says as constructor but I get error : cannot declare variable 'GFX' to be of abstract type 'Adafruit_GFX

in the header file it says this part under public. You have this file its under Arduino/Libraries/TFT/src/utility If you have V1.6.3

class Adafruit_GFX : public Print {
 public:

  Adafruit_GFX(int16_t w, int16_t h); // Constructor

  // This MUST be defined by the subclass
  virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;

  

  // These MAY be overridden by the subclass to provide device-specific
  // optimized code.  Otherwise 'generic' versions are used.
  virtual void 
	drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, 
		uint16_t color),
  	drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
	drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),

Huh?
I have no idea where you are going or what you are asking.

Okay sweet I got it thank you lol nvm..... the Adafruit_GFX I thought needed a new Name.Function call with another command like TFT(cs,ds,rst); but I couldnt find what. I then understood that file is called through the TFT header so I then though DUH I need to use TFTscreen

Okay well back to the "%3d" that still wont work and I dont uunderstand how you think it would? you say if their the same width, say 5 pixels at size 1, A displayed 3 would get covered and overwritten by an 8 but an 8 to a 1 won't? or say an 8 to 0 youll still have an 8?

o an one more thing... I take my readings from the accelerometer map(Name, sensor tilted left, sensor titled right, -90, 90) to get degrees of tilt. Now if I put in -900, 900 I get a tenths value thats handy. How do I put a decimal in a number? I tried using a float instead of integer and just got a 6 digit number, also tried "%f" and I just get a ?, and lastly I tried the "%e+2" and nothing works....

nissan20det:
Okay well back to the "%3d" that still wont work and I dont uunderstand how you think it would? you say if their the same width, say 5 pixels at size 1, A displayed 3 would get covered and overwritten by an 8 but an 8 to a 1 won't? or say an 8 to 0 youll still have an 8?

Like I said, it depends on how they render characters. If they render/draw all the pixels of the character, then a character will be completely overwritten when a new character is drawn on top it.
The pixels of the glyph are the foreground color and the other pixels are the background color.
i.e. if the font is 5x7 pixels, all 35 pixels get drawn not just the pixels of the actual character glyph.
However, if they only draw the glyph pixels, the new character is OR'd into the existing pixels and so you get an overstrike effect.

Which way it works depends on how the code renders the font.
It is very easy to tell which way it works. Just print 2 characters at the same position.
The graphic library I wrote does both. By default it overwrites but the user can ask for overstrike instead.

nissan20det:
o an one more thing... I take my readings from the accelerometer map(Name, sensor tilted left, sensor titled right, -90, 90) to get degrees of tilt. Now if I put in -900, 900 I get a tenths value thats handy. How do I put a decimal in a number? I tried using a float instead of integer and just got a 6 digit number, also tried "%f" and I just get a ?, and lastly I tried the "%e+2" and nothing works....

The Arduino IDE uses a linker option that disables printf() floating point support.
This was done to save code space. If it is enabled you use about 2k of code whether you are using floating point support in the printf code or not.
This linker option was hard coded in IDE prior to 1.5x but now it is in the platform.txt file so you can patch it to always enable floating point support if you want.

Change this:

compiler.c.elf.flags=-Os -Wl,--gc-sections

to this:

compiler.c.elf.flags=-Os -Wl,--gc-sections -Wl,-u,vfprintf -lprintf_flt

Alternatively, you can do some simple integer and modulo math to create separate numbers for the integer quotient and a 1 digit mantissa and then print them using something like "%3d.%1d" The %3d is for the two digits and the minus sign and the %1 is for the 1 digit fractional part.
You divide the value by 10 to get the integer portion and modulo by 10 to get the 1 digit fractional portion.

i.e

whole = value / 10;
fract = value %10;

sprintf(buf, "%3d.%1d", whole, fract);

--- bill