Continuous changing buffer for serial input method problems

Hey guys,

im working on a project where two arduino boards are communicating using morse codes. I got the morse code working (using led as a transmitter and two light to frequency sensors at the receiver). Basically the receiver constantly receives separate chars and thus you can compare the input with a serial input.
My goal is now to store these chars in a buffer, and when a certain combination (text) is received I want “some” action to occur (like turning a led on, when the text “led on” is received). This buffer is initially empty, and is filled when data is received. Every time a char is added, all content of the buffer is shifted to the left:

example buffer (size 3):

aaa + b = aa
aa + b = a b
a b + b = bb

Well, i wrote this script as a testing environment (thus without the morse thing) and it works as intended. But now i want to make a separate method to add a character to the buffer. (so i can include this easily in my morse script)

//add char "ch" to string buffer" text" with "maximum" size

String addCharacter(String text, int maximum, char ch) {
  if (text.length()>maximum-1) {
    for(int i=0; i<text.length(); i++) {
      if(i>=text.length()-1) {
           text[i]= ch;
        }
        else{
        text[i]=text[i+1];
        }
      }
    }
    else {
    text += ch;
    }
    return text;
}

then inside my script i should add this:

text=addCharacter(text, maximum, ch);

Now the weird thing is: If i use this method it doesnt work, but if i remove the method, and directly implement this in the void loop it does work.
The part where the buffer is empty works, but the part where the individual characters are shifted left shows strange phenomena where characters change value.

example 1: (code in script)

a
aa
aa
a b
bb

example 2: (using method) (- indicates a space)
a
aa
aa-
a–


can anyone help me? Is there something wrong with overwriting a String? Thnx in advance

If i use this method it doesnt work

Not surprising, really. What you say you want to do (continuous changing buffer) and the code you wrote (using a String instance) are nowhere near the same thing.

Putting each { on a new line, and using Tools + Auto Format will make the code more readable (to me, at least).

Anyway, we can't see how you have called the function (or not called it), so we can't help you with it.

ok here is the whole code. I didnt think it was required

String alpha = "aa";  //fake serial input
String beta = "bb";   //fake serial input
boolean henk = 1;     //var that determines which input is used 1=alpha 0=beta
int sym = 0;          //var indicating which symbol is processed from fake serial input
String text;          //buffer var
int maximum = 3;      //buffer size
int redPin = 11;      // Red color of RGB LED connected to digital pin 11
int greenPin = 12;    // Green color of RGB LED connected to digital pin 12
int bluePin = 13;     // blue color of RGB LED connected to digital pin 13
char ch;              //character of fake serial input

void setup()                 
{
  //Setup the serial port for 9600
  Serial.begin(9600);
  pinMode(redPin, OUTPUT); // initialize the pin as an output
  pinMode(greenPin, OUTPUT);   // initialize the pin as an output
  pinMode(bluePin, OUTPUT); // initialize the pin as an output
}


//add text to buffer
String addCharacter(String text, int maximum, char ch) {
  if (text.length()>maximum-1) 
  {
    for(int i=0; i<text.length(); i++) 
    {
      if(i>=text.length()-1) 
      {
        text[i]= ch;
      }
      else{
        text[i]=text[i+1];
      }
    }
  }
  else 
  {
    text += ch;
  }
  return text;
}


void loop()
{   
  //Fake serial input with 4 second delay. beta and alpha are exchanged each loop
  if(henk==1) 
  {
    ch=alpha.charAt(sym);
  }
  else 
  {
    ch=beta.charAt(sym);
  }
  sym++;
  if (henk == 1 && sym>alpha.length()) 
  {
    sym = 0;
    delay(4000);
    henk = 0;
  } 
  else if (henk == 0 && sym>beta.length()) 
  {
    sym = 0;
    delay(4000);
    henk = 1;
  }
  // end of fake serial input

  //add serial input to buffer
  text=addCharacter(text, maximum, ch);

  //print buffer
  Serial.println(text);

  //check for content, if match turn led on
  if (text.startsWith(alpha,text.length()-alpha.length())) 
  {
    analogWrite(redPin, 255);
    //Serial.println("ON");
  }

  //check for content, if match turn led off
  if (text.startsWith(beta,text.length()-beta.length())) 
  {
    analogWrite(redPin, 0);
    //Serial.println("OFF");
  }
}

edit: with continuous i mean that when a new character is received

boolean henk = 1;     //var that determines which input is used 1=alpha 0=beta

Nonsense. Boolean variables should be assigned values of true or false ONLY.

  if(henk==1) 
  {
    ch=alpha.charAt(sym);
  }
  else 
  {
    ch=beta.charAt(sym);
  }

\ So, what's in ch? Why don't you KNOW?

  //add serial input to buffer
  text=addCharacter(text, maximum, ch);

What is in text before you diddle with it? Why don't you KNOW?

PaulS: boolean henk = 1;     //var that determines which input is used 1=alpha 0=beta

Nonsense. Boolean variables should be assigned values of true or false ONLY.

Nonsense. ... says you.

Booleans are not single bit variables, if they were I'd agree.

hmm i found the problem i think. sym is increased too high and thus referring to a non existing char:

apha.charAt(sym);

inserting this in a method leads to wrong input. while using code directly just adds a space in between. Not really sure why though.

  if (henk == true && sym>alpha.length()-1) 
  {
    sym = 0;
    delay(4000);
    henk = false;
  } 
  else if (henk == false && sym>beta.length()-1) 
  {
    sym = 0;
    delay(4000);
    henk = true;
  }

so now it works