[SOLVED] Serial working with a little problem

Hello :slight_smile:
I’m happily taking my project forward thanks to you all and the newcomers guides at the begining of the forum (The "Serial Communication Basics was greeeeat!!!) Now I have a steady and perfect communication between three Arduinos.

The only part that is stuck, is a section in the sketch where I need that something happens if a certain data arrives.

My project consist in one Arduino (sender) with two buttons (page 1 and page 2) in a tft shield… that send two commands (toPage1, toPage2) via serial to the others (receivers) that have the same sketch uploaded.

If I open the serial monitor of each receiver, they show that received data is accurate‚Ķ but where I say ‚Äúif receiving this‚Ķdo that‚Ķ‚ÄĚ is not working.

I‚Äôve tried the ‚Äúif (receivedChars‚Ķ‚ÄĚ in all ways my limited knowledge could imagine ("", (), =, ==) compared with researched codes and also I tried to modify the functions at the bottom of the sketch with no results‚Ķ time to ask for help :frowning:

I attached the code due to size.

Thanks in advance

Sketch_08-31-D.ino (7.85 KB)

Use the strcmp() function to compare strings, the function will return a value of 0 when the strings are identical.

  if (receivedChars == "toPage2") {

should be written as

  if (strcmp(receivedChars, "toPage2") == 0) {

Also, when using the recvWithEndMarker() function from the Serial Basics examples, you normally want to test for the newData flag to be true before using the contents of receivedChars, and then set it to false so you only process the data once.

For reliable serial communication, you might consider using this Arduino library.

As for your code:

What is this doing in the global scope? Did you mean to initialize a function with this (there is no function name associated with this)?

{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width());
    pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
    pixelnull_x = map(p.y, TSNULL_LEFT, TSNULL_RT, 0, tft.width());
    pixelnull_y = map(p.x, TSNULL_TOP, TSNULL_BOT, 0, tft.height());
  }
  return pressed;
}

What is this for?

int ;

Your main problem:
I see no attempt to identify what you actually received in the serial packet. You say you tried several things, but I see no such attempt in your code. [Edit] Now I see where you were trying to do the comparison.

That being said, it would be better to send a single char that specifies the page number to go to, so that you can use char comparison instead of string comparison.

Power_Broker:
As for your code:

What is this doing in the global scope? Did you mean to initialize a function with this (there is no function name associated with this)?

{

TSPoint p = ts.getPoint();
 pinMode(YP, OUTPUT);      //restore shared pins
 pinMode(XM, OUTPUT);
 digitalWrite(YP, HIGH);   //because TFT control pins
 digitalWrite(XM, HIGH);
 bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
 if (pressed) {
   pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width());
   pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
   pixelnull_x = map(p.y, TSNULL_LEFT, TSNULL_RT, 0, tft.width());
   pixelnull_y = map(p.x, TSNULL_TOP, TSNULL_BOT, 0, tft.height());
 }
 return pressed;
}

Too many blank lines in the code, check a couple of lines above that for the function declaration

bool Touch_getXY(void)

{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width());
    pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
    pixelnull_x = map(p.y, TSNULL_LEFT, TSNULL_RT, 0, tft.width());
    pixelnull_y = map(p.x, TSNULL_TOP, TSNULL_BOT, 0, tft.height());
  }
  return pressed;
}

david_2018: Use the strcmp() function to compare strings, the function will return a value of 0 when the strings are identical.

  if (receivedChars == "toPage2") {

should be written as

  if (strcmp(receivedChars, "toPage2") == 0) {

Also, when using the recvWithEndMarker() function from the Serial Basics examples, you normally want to test for the newData flag to be true before using the contents of receivedChars, and then set it to false so you only process the data once.

The problem with this is that receivedChars is an array, not a string...

Otherwise it will work.

david_2018:
Too many blank lines in the code, check a couple of lines above that for the function declaration

bool Touch_getXY(void)

{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);  //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width());
    pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
    pixelnull_x = map(p.y, TSNULL_LEFT, TSNULL_RT, 0, tft.width());
    pixelnull_y = map(p.x, TSNULL_TOP, TSNULL_BOT, 0, tft.height());
  }
  return pressed;
}

OP, you should really learn to write more organized code :slight_smile:

Power_Broker:
There is a problem with this is that receivedChars is an array, not a string…

Sorry for my lack of correct terminology , I usually think of string and null-terminated character array as the same thing, as opposed to String.

Guys... the strcmp() function worked like a charm!!! Everything is perfect now.

Power_Broker: OP, you should really learn to write more organized code :)

Yeap... I'm really making the effort :)

Thanks everyone took the time to help.

david_2018: Sorry for my lack of correct terminology , I usually think of string and null-terminated character array as the same thing, as opposed to String.

They definitely are different in C++, but I just did some extra research and realized that strcmp() uses C strings, which, are null-terminated character arrays and that's why it works in this situation.