Go Down

Topic: Concatenate Serial.print() values (Read 34126 times) previous topic - next topic

nerdboy64

I bought an lcd from sparkfun and am using it to make a primitive game system. The thing is, the controller I'm using outputs a decimal number, and the lcd talks in hex. I managed to convert the numbers to hex all right, but my function creates two separate 'char' variables. Unfortunately, it won't work when I send this:
Code: [Select]
Serial.print('0x',BYTE);
Serial.print(myvar[0],BYTE);
Serial.print(myvar[1],BYTE);


So is there a way to concatenate the value you send into one string?

GrooveFlotilla

Quote
but my function creates two separate 'char' variables

So, write the two character into the appropriate places in a string.

Crudly:
Code: [Select]
char buffer [] ="0x..";
...
buffer [2] = msbInASCII;
buffer [3] = lsbInASCII;


nasty, dirty, but should work.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

scottchiefbaker

Alternately you could use sprintf;

Code: [Select]
char mystr[4];

sprintf(mystr,"0x%c%c",myvar[0],myvar[1]);


Groove's answer would be much faster and more efficient though.

nerdboy64

#3
Jun 17, 2010, 04:09 pm Last Edit: Jun 17, 2010, 04:11 pm by nerdboy64 Reason: 1
I already put the values into an array like Groove said, but my question was not how to contain the values together, but rather how to print all of those values in the same Serial.print() command. It's because my LCD doesn't understand if the values are sent separately, like did in my first example. I'm also sure it won't work if I do Serial.print(myvar[0-3], BYTE) because it would think I'm trying to say Serial.print(myvar[-3],BYTE).

GrooveFlotilla

#4
Jun 17, 2010, 04:48 pm Last Edit: Jun 17, 2010, 04:49 pm by GrooveFlotilla Reason: 1
Code: [Select]
Serial.print (buffer);
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

nerdboy64

#5
Jun 18, 2010, 04:24 pm Last Edit: Jun 18, 2010, 04:34 pm by nerdboy64 Reason: 1
Well, I tried the code and it didn't work. Can anyone give me an explanation as to why? I highlighted the stuff that is relevant to the question.

Code: [Select]
[glow]#define TO_HEX(x)  (((x) > 10) ? (x) - 10 + 'A' : (x) + '0')
byte xVal = 64;
byte yVal = 32;
char xValHex[4];
sprintf(xValHex,'0x%c%c', TO_HEX (xVal >> 4), TO_HEX (xVal & 15));
char yValHex[4];
sprintf(yValHex,'0x%c%c', TO_HEX (yVal >> 4), TO_HEX (yVal & 15));[/glow]

void setup(){
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x02,BYTE);//backlight
Serial.print(0x19,BYTE);//set to 25
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x03,BYTE);//circle
[glow]Serial.print(xValHex,BYTE);//x-coord
Serial.print(yValHex,BYTE);//y-coord[/glow]
Serial.print(0x0A,BYTE);//radius ten
Serial.print(0x01,BYTE);//draw


It gives me an error that says "constructor, destructor, or type conversion before '(' token."

GrooveFlotilla

#6
Jun 18, 2010, 04:53 pm Last Edit: Jun 18, 2010, 04:55 pm by GrooveFlotilla Reason: 1
Executable code has to be inside a function.
Those sprintfs are orphans.
(Does anyone know if the %#x template works with the arduino's sprintf?)
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

scottchiefbaker

It looks like arduino sprintf does work with hex values.

Code: [Select]
void setup() {
 Serial.begin(9600);
 randomSeed(analogRead(0));

}

void loop() {
 int num = random(0,99);
 char foo[10];

 sprintf(foo,"Int: %i Hex: %x",num,num);
 Serial.println(foo);
 
 delay(1000);
}


Output:

Int: 89 Hex: 59

Int: 93 Hex: 5d

Int: 56 Hex: 38

Int: 3 Hex: 3

Int: 86 Hex: 56

Int: 26 Hex: 1a

AWOL

Quote
It looks like arduino sprintf does work with hex values

Imagine that.  ::)
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

nerdboy64

Thank you for that. Unfortunately, my compiler is still being a pile of crap. This time, is says "invalid conversion from 'int' to 'cost char*.' Does anyone know how to fix this? Here's the code:

Code: [Select]
#define TO_HEX(x)  (((x) > 10) ? (x) - 10 + 'A' : (x) + '0')
byte xVal = 64;
byte yVal = 32;
char xValHex[4];
char yValHex[4];

void setup(){
sprintf(yValHex,'0x%c%c', TO_HEX (yVal >> 4), TO_HEX (yVal & 15));
sprintf(xValHex,'0x%c%c', TO_HEX (xVal >> 4), TO_HEX (xVal & 15));
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x02,BYTE);//backlight
Serial.print(0x19,BYTE);//set to 25
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x03,BYTE);//circle
Serial.print(xValHex,BYTE);//x-coord
Serial.print(yValHex,BYTE);//y-coord
Serial.print(0x0A,BYTE);//radius ten
Serial.print(0x01,BYTE);//draw
}
void loop(){

}

AWOL

I think the pile of crap is not the compiler.
Code: [Select]
Serial.print(xValHex,BYTE);//x-coord
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

nerdboy64

All right. I knew I made a mistake somewhere, but I couldn't figure out what it was. I'm still somewhat new to Arduino and this project is slightly more advanced than I'm used to. Anyway, I can't seem to get it to work, so I need some non-crappy code that will get this thing up-and-running. Can anyone help... again? The code is the same as my previous post.

AWOL

"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

nerdboy64

Would that still print the same thing as the original code? I'm pretty sure it needs to because all the example code had the "BYTE" format on it.

AWOL

Code: [Select]
sprintf(yValHex,[glow]"[/glow]0x%c%c[glow]"[/glow], TO_HEX (yVal >> 4), TO_HEX (yVal & 15));
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up