Converted string to char* wont show in serial.print

Hi,

Using a ph stamp I can print out readings from a string to serial //Serial.println()

But I'm also trying to print the readings to a TFT screen. The library to print to the screen requires the following to print to a TFT, my.GLCD(char* , int, int); The 2 integers are the X and Y co-ordinates on the screen.

In order to convert the string to char* I used

char* buf;
string.toCharArray(buf,len);

When I try to print the buf to the Serial Com or the TFT I get blank characters, (the screen keeps scrolling)
My buffer is called "lolz"

Here's my code

String inputstring = "";                                                       
String sensorstring = "";                                                      
boolean input_stringcomplete = false;                                          
boolean sensor_stringcomplete = false;         

#include <ITDB02_Graph16.h>
#include <ITDB02_Touch.h>
#include <DS1307.h>

//Display Setup
ITDB02        myGLCD(38,39,40,41,ITDB32S);
ITDB02_Touch  myTouch(6,5,4,3,2);

 extern uint8_t SmallFont[];
extern uint8_t BigFont[];
//a string to hold incoming data from the PC
//a string to hold the data from the Atlas Scientific product
//have we received all the data from the PC
//have we received all the data from the Atlas Scientific
//product
 void setup(){                                                                
     Serial.begin(38400);                                                      
     Serial3.begin(38400);                                                     
     inputstring.reserve(5);                                                   
     sensorstring.reserve(30);      

// Initial setup
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);

  myTouch.InitTouch(LANDSCAPE);
  myTouch.setPrecision(PREC_MEDIUM);     
     



  
 }
//set up the hardware
//set baud rate for the hardware serial port_0 to 38400
//set baud rate for software serial port_3 to 38400
//set aside some bytes for receiving data from the PC
//set aside some bytes for receiving data from Atlas Scientific
//product
void serialEvent() {                                                         
               char inchar = (char)Serial.read();                               
               inputstring += inchar;                                           
               if(inchar == '\r') {input_stringcomplete = true;}                
              }  
//if the hardware serial port_0 receives
//a char
//get the char we just received
//add it to the inputString
//if the incoming character is a <CR>,
//set the flag              
void serialEvent3(){                                                          
              char inchar = (char)Serial3.read();                              
              sensorstring += inchar;                                          
              if(inchar == '\r') {sensor_stringcomplete = true;}               
             }
//if the hardware serial port_3 receives
//a char
//get the char we just received
//add it to the inputString
//if the incoming character is a <CR>,
//set the flag 
void loop(){                                                                  
     
  if (input_stringcomplete){                                                   
      Serial3.print(inputstring);                                              
      inputstring = "";                                                        
      input_stringcomplete = false;                                           
      }
//here we go...
//if a string from the PC has been received in its entierty
//send that string to the Atlas Scientific product
//clear the string:
//reset the flag used to tell if we have received a completed
//string from the PC 
char* lolz;
sensorstring.toCharArray(lolz,10);

 myGLCD.setColor(255,0,0);
 if (sensor_stringcomplete){                                                   
      myGLCD.print(lolz,180,180);
Serial.println(lolz);       
      sensorstring = "";                                                       
      sensor_stringcomplete = false;                                           
      }
 }
//if a string from the Atlas Scientific product has been
//received in its entierty
//send that string to to the PC's serial monitor
//clear the string:
//reset the flag used to tell if we have received a
//completed string from the Atlas Scientific product

When I use Serial.print(sensorstring); it displays the value in the serial com. But I cant use a string to print to TFT.

Thanks in advance.

lolz should be declared as an array, not a pointer.

Ok,

Managed to get the value displayed in Serial.print to show on my TFT using the following code

/*
This software was made to demonstrate how to quickly get your Atlas Scientific product running on the Arduino platform.
An Arduino MEGA 2560 board was used to test this code.
This code was written in the Arudino 1.0 IDE
Modify the code to fit your system.
**Type in a command in the serial monitor and the Atlas Scientific product will respond.**
**The data from the Atlas Scientific product will come out on the serial monitor.**
Code efficacy was NOT considered, this is a demo only.
The TX3 line goes to the RX pin of your product.
The RX3 line goes to the TX pin of your product.
Make sure you also connect to power and GND pins to power and a common ground.
Open TOOLS > serial monitor, set the serial monitor to the correct serial port and set the baud rate to 38400.
Remember, select carriage return from the drop down menu next to the baud rate selection; not "both NL & CR".
*/

#include <ITDB02_Graph16.h>

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product

ITDB02        myGLCD(38,39,40,41,ITDB32S);

extern uint8_t SmallFont[];
extern uint8_t BigFont[];

  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port_0 to 38400
     Serial3.begin(38400);                                                     //set baud rate for software serial port_3 to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
   
   myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);  
   }
 
 
 
   void serialEvent() {                                                         //if the hardware serial port_0 receives a char              
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              }  


  void serialEvent3(){                                                         //if the hardware serial port_3 receives a char 
              char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }



 void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty 
      Serial3.print(inputstring);                                              //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                                  //Print to PC's serial monitor
      char lolz[30];
      sensorstring.toCharArray(lolz,30);
      int phread = atoi(lolz);
      char phprint[30];
      itoa (phread,phprint,30);
      myGLCD.setColor(255,0,0);                                                 //Print to TFT
      myGLCD.print(phprint,180,180);
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

The problem is that I lose the decimal values using this code so instead of reading a ph of 7.42 It only reads 7 on the TFT.

Any ideas?

itoa will stop converting when it encounters a non-numeric character (like a period). May that is it? If the device is returning the ph value as 7.42, you will need to parse the string with a period and itoa convert the value after the decimal point also.

Could you explain how that would be done?

Sorry if I sound arrogant but I'm new to programming altogether, doing this for a science project.

Thanks.

itoa will stop converting when it encounters a non-numeric character (like a period).

itoa will never encounter a non-numeric character like a period, because it is converting an integer to ASCII.

I renamed the variables. Considering the value is returned as x.xx or xx.xx, then this should do it:

int phint = atoi(lolz);
int phfract;

if(lolz[1] == '.') phfract = atoi(&lolz[2]);
else phfract = atoi(&lolz[3]);

@AWOL: My bad. That was supposed to be atoi(). The coffee is not working yet! :wink:

but a period is available in ASCII isn't it? Why would I be losing the period and the numbers to follow?

Thanks, will give it a shot and report back

There is no decimal point in integers. The period is an invalid character (non-numeric).

Using

 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
      Serial.println(sensorstring);                                                  //Print to PC's serial monitor
      char lolz[30];
      sensorstring.toCharArray(lolz,30);
      int phint = atoi(lolz);
      int phfract;
      if(lolz[1] == '.') phfract = atoi(&lolz[2]);
      else phfract = atoi(&lolz[3]);
      char phprint[30];
      itoa (phfract,phprint,30);
      myGLCD.setColor(255,0,0);                                                 //Print to TFT
      myGLCD.print(phprint,180,180);
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product

I get 0s on the TFT instead of 7.42 or 7.xx

Maybe the problem occurs at the toCharArray?

You can check lolz the same way you checked sensorstring.

sensorstring.toCharArray(lolz,30);
Serial.println(lolz);
// rest of your code

Or use sensorstring instead of lolz if it is printing the correct value.

edit: I see you are only printing phfract. It will be a combination of phint and phfract that will be correct.

Serial.print("ph = ");
Serial.print(phint,DEC);
Serial.print(".");
Serial.println(phfract,DEC);

Awesome! Almost there

Its now printing x.x to both serial and TFT, it needs to be x.xx though....

what can we change to get it to do both decimals?

Thanks again!

if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
                                                        
      char lolz[30];
      sensorstring.toCharArray(lolz,30);
      int phint = atoi(lolz);
     
      int phfract;
      if(lolz[1] == '.') phfract = atoi(&lolz[2]);
      else phfract = atoi(&lolz[3]);
      
      char intprint[30];
      char fractprint[30];
      
      itoa (phint,intprint,30);
      itoa (phfract,fractprint,30);
      
      Serial.print(intprint);
      Serial.print(",");
      Serial.println(fractprint);
      
      myGLCD.setColor(255,0,0);                                                 //Print to TFT
      myGLCD.print(intprint,180,180);
      myGLCD.print(".",195,180);
      myGLCD.print(fractprint,203,180);
      
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product

My bad. I forgot the leading zero on the decimal fraction. I usually use sprintf.

Serial.print("ph = ");
Serial.print(phint,DEC);
Serial.print(".");
if(phfract < 10) Serial.print("0");
Serial.println(phfract,DEC);

Does that do it?

If I add ,DEC to Serial.print I get a error: call of overloaded 'print(char [30], int)' is ambiguous.

Further testing now actually gives me X.XX but the fraction part does not read what its suppose to.

Its reads stuff like:
7,j2
7,a3
7,11
7,1j

etc...

using code

if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been recived in its entierty 
                                                        
      char lolz[30];
      sensorstring.toCharArray(lolz,30);
      int phint = atoi(lolz);
     
      int phfract;
      if(lolz[1] == '.') phfract = atoi(&lolz[2]);
      else phfract = atoi(&lolz[3]);
      
      char intprint[30];
      char fractprint[30];
      
      itoa (phint,intprint,30);
      itoa (phfract,fractprint,30);
      
      Serial.print(intprint);
      Serial.print(",");
      if(phfract < 10) Serial.print("0");
      Serial.println(fractprint);
      
      myGLCD.setColor(255,0,0);                                                 //Print to TFT
      myGLCD.print(intprint,180,180);
      myGLCD.print(".",195,180);
      myGLCD.print(fractprint,203,180);
      
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flage used to tell if we have recived a completed string from the Atlas Scientific product
      }
 }

Is there a reason you want the numbers printed in base 30? Wouldn't base 10 be easier to read?

      itoa (phint,intprint,30);
      itoa (phfract,fractprint,30);

Like this:

      itoa (phint,intprint,10);
      itoa (phfract,fractprint,10);

Sir,

I would not have been able to do this by myself. You're a champ! :wink:
Functioning perfectly!

Thank you for being patient and your time.