reading ONLY data after "#"

So,I want my code to only start reading data after it recieves # and till it gets to newline character.
My current code reads everything and stops at newline character.How do i make it to only start reading after #. My code is:

void loop() {
   char message[20];
   int charsRead;

   if (Serial1.available() > 0) { 
      charsRead = Serial1.readBytesUntil('\n', message, sizeof(message) - 1); 
      message[charsRead] = '\0';

So,I want my code to only start reading data after it recieves # and till it gets to newline character.

Does that make sense? How will it know that is receives a # unless it reads everything?

Saving the data only after receiving a # makes sense.

Have a look at the 3rd example in Serial Input Basics. You can change the start-marker to be a '#' character and the end-marker to be a '\n' character.

...R

I'm confused,yes that's what I was looking for but I don't understand the code,and what exactly does boolean stand for?

A boolean variable is a logical variable, true or false.

Why would you wonder for more than 15 seconds what a Boolean is? Have you never heard of google? Seems like you could track that one down pretty quick. We like to help but you have to at least appear to be trying at least a little.

Can anyone shorthen this code for me?

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '#';
    char endMarker = ':';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Shorten it? Why?

Can anyone shorthen this code for me?

Other than removing some Serial.print() statements that are there for debugging, no. It is as short as possible to achieve the required results.

Nikola19992:
Can anyone shorthen this code for me?

Whaddya mean - shorten my baby?

...R

So,it can't be any simpler than this?

Nikola19992:
So,it can't be any simpler than this?

How do you suggest making it simpler?

Nikola19992:
So,it can't be any simpler than this?

What could be simpler than this?

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

Treat the rest of it as a black box.

...R

So,can you guys help me? I'm confused.This code is not working and cannot be compiled

void setup() {
    Serial1.begin(9600);
    Serial1.print("Serial is working");
}

void loop() {
   char message[20];
   int charsRead;

    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '#';
    char endMarker = '\n';
    char rc;const byte numChars = 32;
    char receivedChars[numChars];

    boolean newData = false;

 

   if (Serial1.available() > 0) { 
      charsRead = Serial1.readBytesUntil('\n', message, sizeof(message) - 1); 
      message[charsRead] = '\0';                      
   
   if (strcmp(message, "+CMTI:")  == 0){ //CHECKS IF IT HAS SMS
    while (Serial1.available() > 0 && newData == false) {
        rc = Serial1.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}
Serial1.print("AT+CMGR=1");  //READS SMS NUMBER 1.And that SMS content should be compared to HELLO 
if (newData == true) {
           if (strcmp(newData, "HELLO")  == 0){
            Serial1.print("Arduino has recieved SMS that it was waiting for");
        newData = false;
    }
Serial1.print("AT+CMGD=1,4"): //DELETES ALL SMS SO NEXT ONE CAN BE NUMBER 1
}
}
}

This code is not working and cannot be compiled

If it can't be compiled, it is useless to talk about it working.

The compiler did not just say "Nope, not going to compile that crap". It was VERY specific about line numbers, errors, etc.

You would do yourself a world of good if you put every { on a line BY ITSELF, and used Tools + Auto Format.

Nikola19992:
So,can you guys help me? I'm confused.This code is not working and cannot be compiled

Why have you completely screwed up my working example?

Start with the example and the ONLY changes being those for the marker characters. Get that working.

Then, without mucking up the example code add the extra stuff you need as another function.

...R

That’s why I’m here,I need help with that code,so will you help me or what?

I got that first program working,that starts reading after # till it reaches newline character.So I am trying to make my arduino first see if it has SMS,Then send AT command for reading that sms.And THEN read it from # to \n (SMS begins with # and ends with \n).And after that use that SMS in strcmp

Nikola19992:
That's why I'm here,I need help with that code,so will you help me or what?

Not until you do something about the suggestions that have already been made.

Nikola19992:
I got that first program working,that starts reading after # till it reaches newline character.So I am trying to make my arduino first see if it has SMS,Then send AT command for reading that sms.And THEN read it from # to \n (SMS begins with # and ends with \n).And after that use that SMS in strcmp

If you want help please post the working program that YOU have uploaded to your Arduino. And post an example of the message it receives. And tell us what you want to find in the message.

I don't understand what you mean by "send AT command for reading that sms". If you already have the message why would you need to send an AT command?

...R