How to delete the first character of a string

Hi all,

I have been slowly trying to understand arduino, i am making a project with code from different examples. and it works as expected but the only thing i cant work out is how to remove the first character in a string.

so the codes works like this i think,

  1. Data is received on the serial.
  2. Separated into A,B,C,D,E,F strings based on the starting character.
  3. Prints to LCD that rotates through the different sensors .

The problem im trying to fix is when it prints to lcd it leaves the first character on the screen (attached photo)

hopefully someone can point me in the right direction
Thanks in advance.


#include <LiquidCrystal.h>
#define SERIAL_BAUDRATE 115200 //change baudrate here accordingly

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // interfacing pins of LCD and Arduino
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//STRING FOR SERIAL DATA INPUT
String string_A = "";
String string_B = "";
String string_C = "";
String string_D = "";
String string_E = "";
String string_F = "";
String string_DATA ="";

int Display = 0;

//Counter to change positions of pages

int page_counter=1 ;       //To move beetwen pages

//Variables for auto scroll
unsigned long previousMillis = 0;
unsigned long interval = 1000; //Desired wait time 10 seconds

void setup() {
  lcd.begin(16, 2);
  Serial.begin(SERIAL_BAUDRATE);
}



void loop() {
  
  if (Serial.available() > 0) {
                // read the incoming byte:
               string_DATA = Serial.readString();
                if(string_DATA.startsWith("A",0)){
                  if(string_DATA != string_A){
                    string_A = string_DATA;
                  }
                 }
                  else if(string_DATA.startsWith("B",0)){
                          if(string_DATA != string_B){
                            string_B = string_DATA;
                          }
                  }
                  else if(string_DATA.startsWith("C",0)){
                          if(string_DATA != string_C){
                            string_C = string_DATA;
                          }
                  }
                  else if(string_DATA.startsWith("D",0)){
                          if(string_DATA != string_D){
                            string_D = string_DATA;
                          }
                  }
                  else if(string_DATA.startsWith("E",0)){
                          if(string_DATA != string_E){
                            string_E = string_DATA;
                          }
                  }
                  else if(string_DATA.startsWith("F",0)){
                          if(string_DATA != string_F){
                            string_F = string_DATA;
                          }
                  }
                  

                  
  }
//------- Switch function to write and show what you want---// 
  switch (page_counter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(3,0);
      lcd.print("Sensor 1");      
      lcd.setCursor(3,1);
      lcd.print(string_A);
    }
    break;

    case 2: { //Design of page 2 
     lcd.setCursor(3,0);
     lcd.print("Sensor 2");      
     lcd.setCursor(3,1);
     lcd.print(string_B);
       
    }
    break;

    case 3: {   //Design of page 3 
      lcd.setCursor(3,0);
      lcd.print("Sensor 3");
     lcd.setCursor(3,1);
     lcd.print(string_C);
    }
    break;
        case 4:{     //Design of home page 4
      lcd.setCursor(3,0);
      lcd.print("Sensor 4");      
      lcd.setCursor(3,1);
      lcd.print(string_D);
    }
    break;

    case 5: { //Design of page 5 
      lcd.setCursor(3,0);
      lcd.print("Sensor 5");     
     lcd.setCursor(3,1);
     lcd.print(string_E);
    
       
    }
    break;

    case 6: {   //Design of page 6 
      lcd.setCursor(3,0);
      lcd.print("Sensor 6");     
     lcd.setCursor(3,1);
     lcd.print(string_F);
    }
    break;
    
  }//switch end
  
//-----------Auto scroll function---------------//

     unsigned long currentMillis = millis();            //call current millis
     if (currentMillis - previousMillis > interval) {  //If interval is reached, scroll page
     previousMillis = currentMillis;                   //replace previous millis with current millis as new start point
     lcd.clear();                                      //lcd clear if page is changed.
     if (page_counter <6){                             //Page counter never higher than 6 (total of pages)
     page_counter = page_counter +1;                   //Go to next page
     }
     else{
      page_counter=1;                                  //if counter higher than 6 (last page) return to page 1
     }
     } 
  
     

}

Can't you just overprint with a space?

i have just tried that and it flashes between off and what you see in this photo, is this the correct way to do it?

//------- Switch function to write and show what you want---// 
  switch (page_counter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(3,0);
      lcd.print("Sensor 1");      
      lcd.setCursor(3,1);
      lcd.print(string_A);
      lcd.setCursor(3,1);
      lcd.print(" ");
    }
    break;

Position the cursor and print spaces then reposition the cursor and print the data

You appear to be doing it the other way round

You can use .remove() to modify the String before printing. For example

 case 4:{     //Design of home page 4
      lcd.setCursor(3,0);
      lcd.print("Sensor 4");      
      lcd.setCursor(3,1);
      string_D.remove(0,1);//remove 1 character starting at index 0
      lcd.print(string_D);

cattledog:
You can use .remove() to modify the String before printing. For example

case 4:{     //Design of home page 4

lcd.setCursor(3,0);
     lcd.print(“Sensor 4”);      
     lcd.setCursor(3,1);
     string_D.remove(0,1);//remove 1 character starting at index 0
     lcd.print(string_D);

Thank you,i tried your code and it worked but it would clear the data it next time the lcd rotated through the screen, but placing it earlier in the code it works successfully.

#include <LiquidCrystal.h>
#define SERIAL_BAUDRATE 115200 //change baudrate here accordingly

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // interfacing pins of LCD and Arduino
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//STRING FOR SERIAL DATA INPUT
String string_A = "";
String string_B = "";
String string_C = "";
String string_D = "";
String string_E = "";
String string_F = "";
String string_DATA ="";

int Display = 0;

//Counter to change positions of pages

int page_counter=1 ;       //To move beetwen pages

//Variables for auto scroll
unsigned long previousMillis = 0;
unsigned long interval = 1000; //Desired wait time 10 seconds

void setup() {
  lcd.begin(16, 2);
  Serial.begin(SERIAL_BAUDRATE);
}



void loop() {
  
  if (Serial.available() > 0) {
                // read the incoming byte:
               string_DATA = Serial.readString();
                if(string_DATA.startsWith("A",0)){
                  if(string_DATA != string_A){
                    string_A = string_DATA;
                    string_A.remove(0,1);//remove 1 character starting at index 0
                  }
                 }
                  else if(string_DATA.startsWith("B",0)){
                          if(string_DATA != string_B){
                            string_B = string_DATA;
                            string_B.remove(0,1);//remove 1 character starting at index 0
                          }
                  }
                  else if(string_DATA.startsWith("C",0)){
                          if(string_DATA != string_C){
                            string_C = string_DATA;
                            string_C.remove(0,1);//remove 1 character starting at index 0
                          }
                  }
                  else if(string_DATA.startsWith("D",0)){
                          if(string_DATA != string_D){
                            string_D = string_DATA;
                            string_D.remove(0,1);//remove 1 character starting at index 0
                          }
                  }
                  else if(string_DATA.startsWith("E",0)){
                          if(string_DATA != string_E){
                            string_E = string_DATA;
                            string_E.remove(0,1);//remove 1 character starting at index 0
                          }
                  }
                  else if(string_DATA.startsWith("F",0)){
                          if(string_DATA != string_F){
                            string_F = string_DATA;
                            string_F.remove(0,1);//remove 1 character starting at index 0
                          }
                  }
                  

                  
  }
//------- Switch function to write and show what you want---// 
  switch (page_counter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(3,0);
      lcd.print("Sensor 1");      
      lcd.setCursor(3,1);
      lcd.print(string_A);
    }
    break;

    case 2: { //Design of page 2 
     lcd.setCursor(3,0);
     lcd.print("Sensor 2");      
     lcd.setCursor(3,1);
     lcd.print(string_B);
       
    }
    break;

    case 3: {   //Design of page 3 
      lcd.setCursor(3,0);
      lcd.print("Sensor 3");
     lcd.setCursor(3,1);
     lcd.print(string_C);
    }
    break;
        case 4:{     //Design of home page 4
      lcd.setCursor(3,0);
      lcd.print("Sensor 4");      
      lcd.setCursor(3,1);
      lcd.print(string_D);
    }
    break;

    case 5: { //Design of page 5 
      lcd.setCursor(3,0);
      lcd.print("Sensor 5");     
     lcd.setCursor(3,1);
     lcd.print(string_E);
    
       
    }
    break;

    case 6: {   //Design of page 6 
      lcd.setCursor(3,0);
      lcd.print("Sensor 6");     
     lcd.setCursor(3,1);
     lcd.print(string_F);
    }
    break;
    
  }//switch end
  
//-----------Auto scroll function---------------//

     unsigned long currentMillis = millis();            //call current millis
     if (currentMillis - previousMillis > interval) {  //If interval is reached, scroll page
     previousMillis = currentMillis;                   //replace previous millis with current millis as new start point
     lcd.clear();                                      //lcd clear if page is changed.
     if (page_counter <6){                             //Page counter never higher than 6 (total of pages)
     page_counter = page_counter +1;                   //Go to next page
     }
     else{
      page_counter=1;                                  //if counter higher than 6 (last page) return to page 1
     }
     } 
  
     

}//loop end

The traditional C solution: get a pointer to the string, advance it by 1 character, use it to display the remainder of the string.

The use of the String class is discouraged on AVR controllers, the memory amount and management discourages dynamic memory allocation.

DrDiettrich: The traditional C solution: get a pointer to the string, advance it by 1 character, use it to display the remainder of the string.

The use of the String class is discouraged on AVR controllers, the memory amount and management discourages dynamic memory allocation.

Since it's discouraged could you point me in a direction to better achieve the same result.

Since it's discouraged could you point me in a direction to better achieve the same result.

@DrDiettrich

The traditional C solution: get a pointer to the string, advance it by 1 character, use it to display the remainder of the string.

First, to eliminate Strings, you can review the excellent tutorial by Robin2 on non blocking methods to receive serial input data as a null terminated c string.

https://forum.arduino.cc/index.php?topic=396450.0

You will use those techniques to replace this

string_DATA = Serial.readString();

Once you have received the data as a null terminated character string, you declare a pointer to it. The char* to the received string needs to have the correct scope for how you are going to use it. You are not modifying the underlying string so I think it can go with the printing like this

if (receivedChars[0] == 'A')//does terminal input start with A
    {
      //create pointer and increment index for printing after first character
      char *stringA = receivedChars;
      stringA++;
      lcd.setCursor(3,0);
      lcd.print("Sensor 1");      
      lcd.setCursor(3,1);
      lcd.print(stringA);
      
    }

I'm not certain if you really need to test if new received data is different from the old data like you do with this code

if(string_DATA.startsWith("A",0)){
                if(string_DATA != string_A){
                    string_A = string_DATA;

but if you do, then you will need to use the strcmp() function. There will also need to be a saved copy the last receivedChars string as the receive buffer gets filled with new data.