String always are same!

Hi,

I have code for reading strings from Serial. My problem is when i first time sent servo:randomint normal work, but when I sent second time string* does not change the value.

string* is str in code;

void loop()
{
  if(Serial.available())
  {
      do
      {
        c=Serial.read();
        matrix[x]=c;
        String s = String(matrix[x] ,DEC);
        x++;
        delay(1);
      }
      while(c!='\n');
      matrix[x-1]='\0';
      str=matrix;
      
      if(str.startsWith("servo:"))
      {
        String serv = str.substring(5);
         s.write(serv.toInt()); 
         serv = "";
      }
       Serial.println("test: " +str);
      
  }
}

Reading stuff from the serial port when there's nothing there to read is rarely a good idea.

(That was just a snippet of my full answer)

        String s = String(matrix[x] ,DEC);

What the heck are you pissing away resources like this for?

You need to post ALL of your code. There is no evidence that you ever reset x, so the next string does not go at the beginning of matrix, so the next str processing just extracts the same data.

There is NO reason to be using Strings. All the functionality that Strings offer are because strings off the same functionality.

AWOL:
Reading stuff from the serial port when there’s nothing there to read is rarely a good idea.

(That was just a snippet of my full answer)

Why?


PaulS:

        String s = String(matrix[x] ,DEC);

What the heck are you pissing away resources like this for?

You need to post ALL of your code. There is no evidence that you ever reset x, so the next string does not go at the beginning of matrix, so the next str processing just extracts the same data.

There is NO reason to be using Strings. All the functionality that Strings offer are because strings off the same functionality.

OK, it’s all code.

#include <Servo.h>

String str;
char c;
char matrix[20];
int x=0;
boolean conf = false;
boolean control = false;
Servo s;
Servo m;

void setup()
{
  Serial.begin(9600);
  s.attach(8);
  m.attach(9);
}

void loop()
{
  if(Serial.available())
  {
      do
      {
        c=Serial.read();
        matrix[x]=c;
        String s = String(matrix[x] ,DEC);
        x++;
        delay(1);
      }
      while(c!='\n');
      matrix[x-1]='\0';
      str=matrix;
      
      if(str.startsWith("servo:"))
      {
        String serv = str.substring(5);
         s.write(serv.toInt()); 
         serv = "";
      }
       Serial.println("test: " +str);
      
  }
}

Why?

Because you are ASSuming that you actually read something.

So, where DO you reset x to 0?

Rettiwer: My problem is when i first time sent servo:randomint normal work, but when I sent second time string* does not change the value.

That is because you don't reset the index 'x' to 0 after you use the value in 'matrix'. You add new characters to the END of 'matrix' (eventually causing a crash) but when you convert 'matrix' to a string it always uses the first string in 'matrix'.

If s is defined within the do-while loop statement block, how is the statement:

s.write(serv.toInt());

to write s going to work when it is no longer in scope?

If s is defined within the do-while loop statement block, how is the statement:

s.write(serv.toInt());

to write s going to work when it is no longer in scope?

Here's a hint. The String class does not have a write() method.

I have code for reading strings from Serial.

You don't say just what you are trying to accomplish. If you want to control a servo, there is some fairly simple code available.

Here's a hint. The String class does not have a write() method.

Here's another hint: Even if it did, it wouldn't work.

econjack: Here's another hint: Even if it did, it wouldn't work.

If the code compiled, and OP indicated that it did, it is clear that there is another variable, s, of the correct type.