read and print string with serial

hi, i've compiled and sended this code but don't work :roll_eyes: ..why?? :confused:
It's a simple code but don't work.. help me pleasee :stuck_out_tongue_closed_eyes:

char inData[20];
char inChar=-1;
int length=0;

void setup() {
Serial.begin(9600);
Serial.write("Power On");
}

void loop()
{
if(Serial.available() > 0)
{
length=Serial.available();
for(int j=0; j=length-1;j++)
{
inChar = Serial.read();
inData[j] = inChar;
}
}

for(int i=0;i<20;i++)
{
Serial.write(""+inData*);*

  • }*
    }

Read about the function Serial.readBytesUntil(), since that waits for the user to press Enter before sending the data. As a code fragment:

   int bytesRead;

   if (Serial.available() > 0) {
      bytesRead = Serial.readBytesUntil('\n', inData, sizeof(inData) - 1); // Read until newline char
      inData[bytesRead] = '\0';    // Now it's a string...
      // ...whatever...
   }

You need to get rid of the redefinition of inData[] in loop(), since it's already globally defined above.

Also, as a new poster to this Forum, you owe it to yourself to read the posting guidelines by Nick Gammon which appear at the top of this Forum, especially the use of code tags and using Ctrl-T in the IDE to format your code in a standard manner. The guidelines will help us help you.

Take a look at this code. I have made plenty comments on it, which I hope may help clarify your thoughts a bit. Any doubts, just let me know. I also recommend, as pointed before by our fellows, that you take a look at the guidelines for posting code. It makes easier for everyone to copy and take a look at your code in each one's favorite editor and compiler. (You'll see a "select" link in the box, which will highlight the entire code, making it much easier to select and copy).

String inData = ""; // String to hold input data.

void setup () // This code runs only once.
{
    Serial.begin (9600); // Begins serial communication at 9600 bauds.
    while (!Serial) // While serial is not ready.
    {
	; // Waiting for serial to be ready.
    }
    Serial.println ("Power on!"); // Outputs "Power on!" to the serial port.
}

void loop () // This code keeps being executed infinitely.
{
    while (Serial.available () > 0) // (If)While there is data comming from the serial port...
    {
	int inChar = Serial.read (); // Read data from the serial port into inChar.
	inData += (char) inChar; // Concatenates the contents of inChar as char to the end of inData.
	delay (10); // Waits for 10 milliseconds between each byte read, so that we don't double read the incoming byte and allow the hardware some time to flush it.
	if ((char) inChar == '\n') // It the current read character is a new line...
	{
	    Serial.print ("Echo: "); // Outputs "Echo: " to the Serial port, without a new line.
	    Serial.println (inData); // Outputs the whole string stored in inData, followed by a new line.
	    inData = ""; // "Reset" inData.
	}
    }
    // Code inside loop ended... Starting it all over again...
}