Arduino Forum

Forum 2005-2010 (read only) => Software => Troubleshooting => Topic started by: nerdboy64 on Jun 16, 2010, 03:30 pm

Title: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 16, 2010, 03:30 pm
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?
Title: Re: Concatenate Serial.print() values
Post by: GrooveFlotilla on Jun 16, 2010, 04:04 pm
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.
Title: Re: Concatenate Serial.print() values
Post by: scottchiefbaker on Jun 17, 2010, 07:10 am
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.
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 17, 2010, 04:09 pm
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).
Title: Re: Concatenate Serial.print() values
Post by: GrooveFlotilla on Jun 17, 2010, 04:48 pm
Code: [Select]
Serial.print (buffer);
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 18, 2010, 04:24 pm
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."
Title: Re: Concatenate Serial.print() values
Post by: GrooveFlotilla on Jun 18, 2010, 04:53 pm
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?)
Title: Re: Concatenate Serial.print() values
Post by: scottchiefbaker on Jun 18, 2010, 05:12 pm
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
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 18, 2010, 08:51 pm
Quote
It looks like arduino sprintf does work with hex values

Imagine that.  ::)
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 19, 2010, 05:24 pm
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(){

}
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 19, 2010, 06:04 pm
I think the pile of crap is not the compiler.
Code: [Select]
Serial.print(xValHex,BYTE);//x-coord
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 19, 2010, 06:37 pm
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.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 19, 2010, 06:50 pm
Code: [Select]
Serial.print(xValHex);//
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 22, 2010, 05:40 pm
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.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 22, 2010, 07:13 pm
Code: [Select]
sprintf(yValHex,[glow]"[/glow]0x%c%c[glow]"[/glow], TO_HEX (yVal >> 4), TO_HEX (yVal & 15));
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 26, 2010, 04:39 pm
I deleted the bad bit of code, but it still gave me the same error. In fact, the line it highlighted was not the Serial.print(), but the one with the sprintf(). Anyway, I am bothering you all once again to see if the great thinkers of Arduino can help out.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 26, 2010, 04:48 pm
Was that the sprintf with the single quotes?
If not, could you repost?
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 27, 2010, 12:19 am
The code is the same as the last time I posted my whole program, except the 'BYTE' Serial.print(xValHex,BYTE) and Serial.print(yValHex,BYTE) has been deleted.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 27, 2010, 12:23 am
If you won't repost, I'll assume the single quotes are still there.
Did you read reply #14?
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 28, 2010, 04:49 pm
Yes, the single quotes are still there. I assume that I need to chenge them to double quotes?
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jun 28, 2010, 07:18 pm
Yes, you do.
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jun 29, 2010, 01:33 am
Thanks! For once the compiler didn't come up with an error. Now I can finally begin the real programming. :)
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 04, 2010, 05:34 pm
As my mom might have said, "Don't count your chickens before they hatch." I tried out the code and, even though there was no error detected by the compiler, nothing appeared on the screen of the LCD. Any suggestions?
Title: Re: Concatenate Serial.print() values
Post by: drhex on Jul 04, 2010, 09:08 pm
Code: [Select]
char xValHex[4];
char yValHex[4];


Make those 5 bytes long, so they'll have room for the terminating zero-byte.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jul 05, 2010, 12:10 am
Did your mom ever tell you that it's useful if you post your code, 'cos very few of us are psychic?
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 07, 2010, 04:45 pm
Here's my code: (After I took drhex's advice)

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

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.begin(9600);
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x02,BYTE);//backlight
Serial.print(0x19,BYTE);//set to 25
}
void loop(){
Serial.print(0x7C,BYTE);//cmd
Serial.print(0x03,BYTE);//circle
Serial.print(xValHex);//x-coord
Serial.print(yValHex);//y-coord
Serial.print(0x0A,BYTE);//radius ten
Serial.print(0x01,BYTE);//draw
}
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 11, 2010, 04:21 pm
Hello? Anybody got some advice? I posted my code.
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jul 11, 2010, 05:02 pm
You're the one with the hardware - what steps have you taken to debug?

I'd be inclined to use the serial monitor rather than unproven LCD hardware, at least until you've proven that the messages you need are ebing sent and received, at the correct rate and in the expected format.
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 11, 2010, 05:48 pm
I tried the serial monitor and I think I've found the problem. When It sends the information that sets the backlight value, it comes up on the serial monitor as funky symbols, but when It sends the hex information, it comes up as readable letters. If I am correct (which I'm probably not) it's the result of the "BYTE" format being removed from the Serial.print() command. Any suggestions? (Honestly, I haven't done much to debug. I only checked the connections and ran it through the compiler a couple times.) In case you're wondering, my code is still the same.
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 17, 2010, 04:28 pm
Does anyone have some advice? I'm still confused!
Title: Re: Concatenate Serial.print() values
Post by: AWOL on Jul 17, 2010, 10:40 pm
Your "funky" symbols are unprintable ASCII codes below 0x20 (space character), like 0x02 and 0x19. If you want to see their actual values:
Code: [Select]
Serial.print(0x7C,HEX);//cmd
Serial.print(0x02,HEX);//backlight
Serial.print(0x19,HEX);//set to 25


Quote
, but when It sends the hex information, it comes up as readable letters

What "hex information"?
I'm still confused as to what the problem is.
Title: Re: Concatenate Serial.print() values
Post by: nerdboy64 on Jul 26, 2010, 04:13 am
Sorry I didn't get back to you until now. Anyway, I suppose it's all technically hex information. what I meant was when it sends the coordinates of the circle's center, it comes up as readable characters. And, even though I like to know what its sending, it should be the weird symbols and not the readable ones.