Processing-Value: char to char*

Hi,

I’ve a LCD and use the SLCD-library.
There are thounds of entries but all use other libraries and other lcds and nothtings seems to work for me :frowning:

I send values (atm chars) from Processing serial to Arduino.
My lcd-libraries needs const char* (a char*-array that consits of char-arrays???).
I tried a lot - what works to write with chars is this:

char a = ‘a’;
char b = ‘b’;
char c =‘c’;
char myStrings[2] = {a, b};
char myStrings2[2] = {b, c};
/Because strings are just char-arrays and terminated with a null character (ASCII code 0) the length of the array is [2]
see reference/string.html
/
char* mySuperstring[5] = {myStrings, myStrings2};

// im setup or print:

lcd.clear();
lcd.print(mySuperstring[1]);

My problem is now: The LCD prints the right const* but myString and myString2 are not only “ab” and “bc” they are “ab\0” and “bc \0” because they are terminated with the 0-character.

Now my LCD prints: bc?|||||

How can I avoid that?
Thx for your help

The variable mySuperstring is not a pointer to an array of characters. It is an array of pointers to array of characters.

Your LCD wants to print a string. myStrings and myStrings2 are NOT strings. They are arrays of characters.

The difference between a string and an array of characters is that a string is an array of characters that is NULL terminated.

A statement like this: char *someString = "AB"; results in 3 bytes being assigned values of 'A', 'B', and NULL.

Your statement: char myStrings2[2] = {b, c}; results in 2 bytes being assigned values of 'b' and 'c'.

When you pass mySuperstring[1] to lcd.print(), the value that is passed is the address where myStrings2 begins.

The lcd.print instruction then prints one character at a time, until a NULL is encountered. It prints myStrings[0], myStrings[1], myStrings[2], myStrings[3], myStrings[4], etc. until it encounters a NULL. Note that myStrings[2] and on are outside the range of your array, so you will get garbage being printed.

myString and myString2 are not only "ab" and "bc" they are "ab\0" and "bc \0" because they are terminated with the 0-character.

This is not true. The arrays are not sized to hold the terminating NULL, and nowhere do you actually assign NULL to a position in an array.

Hey PaulS,

thx for ur help. But I’m confusted. :-[

The reference says that:

Strings are represented as arrays of type char and are null-terminated.

Examples

All of the following are valid declarations for strings.

The arduino-reference

char Str1[15];
char Str2[8] = {‘a’, ‘r’, ‘d’, ‘u’, ‘i’, ‘n’, ‘o’};
char Str3[8] = {‘a’, ‘r’, ‘d’, ‘u’, ‘i’, ‘n’, ‘o’, ‘\0’};
char Str4 = “arduino”;
char Str5[8] = “arduino”;
char Str6[15] = “arduino”;

Possibilities for declaring strings

  • Declare an array of chars without initializing it as in Str1
  • Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
    (…)
    Null termination

Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.print()) to tell where the end of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren’t actually part of the string.

This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need to be eight characters, even though “arduino” is only seven - the last position is automatically filled with a null character. Str4 will be automatically sized to eight characters, one for the extra null. In Str3, we’ve explicitly included the null character (written ‘\0’) ourselves.

If I declare
char myStrings[1] = {‘a’, ‘b’};
I get: too many initilizers for ‘char[1]’

You are totally right, that my LED wants to print strings. I tried everything not to do it :wink:
But how can I built one string when only char(s) are allowed?

:o

The first line of the reference is perhaps just a little bit misleading. Strings are arrays of characters THAT are null terminated. Arrays of characters are not automatically null terminated.

When an array is declared, with a fixed size, each element in the array is set to 0 (NULL).

So

 char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
 char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};

both end up with a NULL in position 7. Str2 gets a NULL there because only 7 explicit initializers are present, so the default initializer (NULL) remains. Str3 gets a NULL there because it explicitly set position 7 to NULL.

The other examples use constant strings, which are implicitly NULL terminated. The size of Str4 is determined by the compiler. Str5 is explicitly sized large enough to hold the terminating NULL. Str6 ends up with several NULLs at the end, but only the first has any meaning.

But how can I built one string when only char(s) are allowed?

Follow the examples for Str2 or Str3, implicitly or explicitly providing the NULL in an array that is large enough to hold it.

Hey, thx. That works (I defined the right length of the array) but nevertheless i’m stucked.

I try to send values from processing to my Ardunio and to display them with my LCD and my SLCD library.
If I send something like

port.write(50);

from Processing to the Serial-port I can’t print out the result with

byte inByte = Serial.read();
Serial.println(inByte);

but I can print Serial.print(inByte, DEC)
and get a clean “50”.
I can check if the incoming value is 50 as well (if (inByte==50) but I never get so far to put it on the screen :frowning:

I tried a lot without success. My last helpless try was to put the incoming value as char in an array and than I tried to simulate a String. Doesn’t work :frowning:

Here comes the code: Arduino
(…)

void loop() {
if (Serial.available() > 0){
inChar = Serial.read();
// Serial.println(inChar);
delay(300);
char inData[2] = {inChar};
char* tong = {inData , “teststring” };
//Serial.println (“intdata” + intData*);

lcd.print(tong[0], 0, 0);
delay(100);
else {
lcd.clear();
lcd.print(0, 0, “hello”);
delay(100);
}
}
[/sup]
Processing
if(keyPressed) {
if (key == ‘b’ || key == ‘B’) {

// port.write(’\0’);
port.write(nr);
println(nr);
delay(50);
nr ++;
}*
Probably there is a much easier way but I have no idea what I can google for :frowning:

This statement:

port.write(50);

is sending one byte, containing the value 50, to the serial port.

This statement:

byte inByte = Serial.read();

reads that byte, containing the value 50, and stores it in inByte.

This statement:

Serial.println(inByte);

invokes the Print::println function for an unsigned character, with a default value for base of 0, which results in a call to the Print::print function for an unsigned long with a base of 0, which results in a call to the Print::write function, which knows that the value is an ASCII value, causing a '2' to appear.

This statement:

Serial.println(inByte, DEC);

invokes the Print::println function for an unsigned character, with a base of 10, which results in a call to the Print::print function for an unsigned long with a base of 10, which results in a call to the Print::printNumber function for an unsigned long, which converts the number to a string, and calls the Print::print function for each character in the string, causing '5' and '0' to appear.

If you want '5' '0' to appear on the LCD, when Processing sends the value 50, you, too, need to convert the value to a string. The itoa function deserves a close look.

hey PaulS,

thx! I hadn’t so much time the last days but finally it’s weekend.
I haven’t a deep understanding for what you’ve written about Print::functions but I’ve done it!

It was easier than expected:

void loop() {
  if (Serial.available() > 0){ 
      char buf[10];

    byte LD = Serial.read();  
    lcd.clear(); 

    lcd.print("Value:  ",0,0); 
    lcd.print(itoa(LD, buf, 10)); 
    delay (500); 
    
      }



  else {
    textTest();
   lcd.clear(); 
    lcd.print(0, 0, "no");
    delay(500);
  }
}

Processing sends just:

port.write(11);  //or any other value.

Thx again.

Ok, cancel my last post: I haven't done it. If I send values above 250 or something I got other values in arduino (like 44 when I send port.write(300)?!).

Thats probably has a relation to ASCII? But I am not sure how I can solve that. I tried to figure out what arudiono gets with int/byte/char = Serial.read(); but the Serial.println-function just gives my strange unreadable feedback :(

Puuhhh ? and I thought the LCD is a good and easy way to start. Thx for any reply!!!

Cheers,

Did you mean values above 255?

mmm. possible that it is 255. If I count up a number in processing it stopps showing the right result on the screen when the number is between 250 and 260.

You know how I solve it, right? :-?

Cheers

You know how I solve it, right?

You are using the write() function which is expecting a number of data types. If the data type of the variable you are trying to write is a byte, there is a problem. The range of values that can be stored in a byte is 0 to 255. So, when you try to stuff 300 in a byte, it overflows, dropping by 256, which results in 44 being sent.

If you need to send values larger than 255, you have two choices. Send the integer value as 2 bytes, using bit shifting, or send the value as a string. You can convert the value to a string using the str() function, and then port.write() that string.

On the Arduino side, you would need to read two bytes and re-assemble the integer, or read the whole series of characters, and convert that array back to an integer, using atoi().

If you go with the string approach, consider adding start and end of packet markers to make reading the string on the Arduino easier. Otherwise, you need to make sure you always send (and receive) a fixed number of characters.