Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« on: January 18, 2008, 03:21:50 am » |
-Hello! I am sorry for this question, but I simply cannot find a solution. Is there a simple way to mimic this java code? int myVar = 2; System.out.println("My variable is: "+myVar);
Serial.println("My variable is: "......?); Yet again, sorry.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #1 on: January 18, 2008, 04:07:40 am » |
The traditional way to format strings in C is sprintf. An example below:
#include <stdio.h>
#define MAX_OUT_CHARS 16 //max nbr of characters to be sent on any one serial command
char buffer[MAX_OUT_CHARS + 1]; //buffer used to format a line (+1 is for trailing 0)
sprintf(buffer,"My variable is: %d",myVar); Serial.print(buffer);
But there are lots of reasons to avoid it. It links in a huge amount of code (almost 2k as I recall), its not very intuitive to use, and if you overflow the buffer it creates elusive bugs.
I usually do the following:
Serial.print("My variable is: "); Serial.println(myVar);
In my current app, I added the following function to format data for my specific requirement.
void formatPrint( char *leftStr, int MyVar1, char *sepStr, int MyVar2, char *rightStr){ Serial.print (leftStr); Serial.print (MyVar1); Serial.print (sepStr); Serial.print (MyVar2); Serial.println (rightStr); }
Usage: formatPrint(“My var 1 is: “, 100, “, and var 2 is: “, 200, “.”);
|
|
|
|
« Last Edit: January 18, 2008, 04:09:37 am by mem »
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #2 on: January 18, 2008, 04:48:00 am » |
//SOLVED
char string[5] = "val"; int i=50;
//:build string+int string[3] = i;
|
|
|
|
« Last Edit: January 18, 2008, 05:15:08 am by AlphaBeta »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #3 on: January 18, 2008, 05:38:46 am » |
That only works if your string never changes in length and the integer value is between 0 and 9, is that what you wanted?
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #4 on: January 24, 2008, 09:12:55 pm » |
That only works if your string never changes in length and the integer value is between 0 and 9, is that what you wanted? No, not what I wanted, but it was what I needed, for now, anyway. Any ideas?
|
|
|
|
|
Logged
|
|
|
|
|
Forum Administrator
Cambridge, MA
Offline
Faraday Member
Karma: 8
Posts: 3532
|
 |
« Reply #5 on: January 24, 2008, 10:41:51 pm » |
You can always just do:
Serial.print("my variable is:"); Serial.println(var);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 507
Arduino rocks
|
 |
« Reply #6 on: January 24, 2008, 11:47:36 pm » |
he's right, thats what i do....
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #7 on: January 25, 2008, 07:04:25 am » |
Any ideas? any reason why one of the three suggestions in reply#1 are not suitable?
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Jr. Member
Karma: 0
Posts: 62
|
 |
« Reply #8 on: January 25, 2008, 06:17:44 pm » |
Here's another vote for Serial.print("My variable is: "); Serial.println(myVar); It's hard to get easier than that. Regards, David
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #9 on: January 29, 2008, 03:35:47 am » |
There is a perfectly resonable reason  for why sending my String and variable in to different method calls won't work. I wrote the reason in #2 but i see I deleted it. Sorry for that. And I also see that I used Serial.print() as an axample. I thought that it was easier to get a 'problem solved' when using a method we're all familiar with. I do not use the serial class. This is my function: void sendToSerProxy(char *s) { while (*s) { printByte(*s++); } printByte(0); } This is why I need the string and var inside the same char array. Of course I know about doing it in separate method calls. : 
|
|
|
|
« Last Edit: January 29, 2008, 03:42:29 am by AlphaBeta »
|
Logged
|
|
|
|
|
Forum Administrator
Cambridge, MA
Offline
Faraday Member
Karma: 8
Posts: 3532
|
 |
« Reply #10 on: January 30, 2008, 06:28:00 pm » |
You're probably best off doing something like this then:
Serial.print("My variable is: "); Serial.print(myVar); Serial.print(0, BYTE);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 177
Arduino rocks
|
 |
« Reply #11 on: January 31, 2009, 04:08:22 pm » |
it's easy to send different data formats to the serial port, just send them one by one. But it's not feasible if you want to put everything to a string then send to a LCD module. I found if you want to append text to an existing string you have to use strcat(string,value) but how can i convert an integer to a string before appending it?
I tried the itoa function but it seems the arduino freezes with this instruction, even if it gets compiled. And I don't know anything about C++
In VB all of this would require this thing...
temperature = temperature + str$(value)
|
|
|
|
|
Logged
|
|
|
|
|
|