I can't understand why one works but not the other

I tried to use “save for forum” but have no idea what name it saves under or where it’s saved. This works:

char message[200];

void setup()
{
Serial.begin(9600); //Set up serial wireless
Serial1.begin(9600); //Set up serial wireless
Serial.flush();
}

void loop()
{
int i=0;
if (Serial1.available() >= 0) // is there data from other unit?
{
while( Serial1.available() && i< 99)
{
message[i++] = Serial1.read(); // read rest of message into string
}
message[i++]=’\0’; // end with new line
Serial.print(message);
}
}

This prints the line:

Temperature: xxFHumidity: xx% or
Temperature: -xxFHumidity: xx%

This doesn’t:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

char * message ;
char * Tmessage;
char * Hmessage;
char * temp;
LiquidCrystal_I2C lcd(0x27,20,4); //set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
Serial.begin(9600); //Set up serial wireless
Serial1.begin(9600); //Set up APC220 wireless
Serial.flush();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
int m = 0;
int T = 0;
int H = 0;
if (Serial1.available() >= 0) // is there data from other unit?
{
while( Serial1.available() && m < 99)
message[m++] = Serial1.read(); // read rest of message into string;
}
message[m++] = ‘\0’;
Serial.print(message);
m = 0;
while ( temp[0] != ‘F’)
{
temp [0] = message[m++];
Tmessage[T++] = temp[0];
}
while( temp[0] != ‘%’)
{
temp [0] = message[m++];
Hmessage[H++] = temp[0];
}
Tmessage[T++] = ‘\0’;
Hmessage[T++] = ‘\0’;
Serial.println(Tmessage);
Serial.println(Hmessage);
lcd.println(Tmessage);
lcd.println(Hmessage);
}
}

void loop()
{
delay (100);
}

All that prints are two garbage characters. I’m trying to separate a received string of the format
Temperature: xxFHumidity: xx% or
Temperature: -xxFHumidity: xx%

to 2 strings

Temperature: (-)xxF
Humidity: xx%

Instead all I get is two garbage characters total

Jim

I tried to use “save for forum” but have no idea what name it saves under or where it’s saved. This works:

I think you meant the Edit menu option “Copy for Forum” in which case, it is in your clipboard. Regardless, the preferred method is to paste your code and wrap it with [ code ] tags [ /code ] instead. (It may be helpful if you go back and edit your existing post to add them.)

What you are trying to do here:

char * message ;
char * Tmessage;
char * Hmessage;
char * temp;

?

char * message ;

This defines that message is a pointer. But, it does not point to any memory. Therefore, you can NOT do this:

        message[i++] = Serial1.read(); // read rest of message into string

The second version does not check for new characters from serial in loop(), it checks only once on initialization in setup(). Wherever message points to an empty string is is created (if the receive buffer is empty at setup). Change your char * something to char something to reserve memory for the strings or you will overwrite memory which will lead to unpredictable results.

OK,I still can’t "copy for forum, but I’ve got this sketch:

char Imessage[60];
char Tmessage[30];
//char Hmessage[30];
char temp[1];

void setup()
{
Serial.begin(9600); //Set up serial
Serial1.begin(9600); //Set up APC220 wireless
Serial.flush();
}

void loop()
{
int i=0;
int t=0;
if (Serial1.available() >= 0) // is there data from other unit?
{
while( Serial1.available() && i< 99)
{
Imessage[i++] = Serial1.read(); // read rest of message into string
}
Imessage[i++]=’\0’; // end with new line
i = 0;
while (i < 60)
{
Tmessage[t++] = Imessage[i++];
if (Tmessage[t-1] == “F”)
{
break;
}
}
Serial.print(Tmessage);
delay (10000);
}
}

The received message, Imessage, is received correctly. The problem is that the if conditions gives an error
“can’t give comparison between pointer and integer” . "If I remove this line, printing Tmessage give the whole string.
What I’m trying to do is separate out the temperature part of the message to print separately. I’m a newbie to
pointers and obviously don’t know what I’m doing.

Jim

The quotes around that F, should be single, not double.

I’ve been working on this, and finally started from scratch. I now got it working.

//com 11
char letter;
char Tmessage[30];
char Hmessage[30];
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); //set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
Serial.begin(9600); //Set up serial
Serial1.begin(9600); //Set up APC220 wireless
Serial.flush();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
}

void loop()
{
int i = 0;
int h = 0;
int t = 0;
int count = 1;
if (Serial1.available() >= 0) // is there data from other unit?
{
while( Serial1.available() && i < 40)
{
letter = Serial1.read(); // read rest of message into string
// Serial.print(letter);
if (count == 1)
{
Tmessage[t++] = letter;
if (letter == ‘F’)
{
count = 2;
}
}
else
{
Hmessage[h++] = letter;
}
}
}
lcd.setCursor(0, 0);
lcd.println(Tmessage);
lcd.setCursor(0, 1);
lcd.println(Hmessage);
delay(10000);
}

only one problem. both lines end with || ||| ie.

Temperature: 71F|| |||
Humidity: 21%|| |||

Where are these lines coming from? If I Serial.print or Serial.println, they don’t show up.

Jim

You need to null terminate your Tmessage and Hmessage strings before you display them.

I added

Tmessage[t] = '\0'; Hmessage[t] = '\0';

before the lcd.println commands, didn't eliminate the lines! What is the correct code?

Jim

Looks ok, although this:

Hmessage[t] = '\0';

should probably be:

Hmessage[h] = '\0';

I'd suggest you send the letters you get to serial to see what the radio is sending you, it looks like those characters are part of what is being sent, given that they are so repeatable.

Your sender really needs to send an end of packet marker, and you really need to read all the data until the end of packet marker arrives. So far, you've apparently been lucky that the entire packet arrives, and that only one packet arrives, per pass through loop. Adding, and using, and end of packet marker would allow you to get rid of the delay().

lcd.println(Tmessage);

should be

lcd.print(Tmessage);

the " ln " in the first one adds a CR LF which is needed for serial display's.

robtillart said

robtillaart: lcd.println(Tmessage);

should be

lcd.print(Tmessage);

the " ln " in the first one adds a CR LF which is needed for serial display's.

Right on! You win the prize!

Jim