1
\$\begingroup\$

I want to make a arduino program which can wait in the loop, until I input some sentience and enter the 'Enter' in the keyboard, so the program can then read all the chars in the serial port and print them out

char line[80];
void setup() {                
  Serial.begin(9600);
}

void loop()  { 
  char c, *p;
  do {
    if (Serial.available() > 0) {
      //Serial.println("s");
      c = Serial.read();
      Serial.println(c);
      if ( c == '\n' || c == '\r' || c=='\0') {
    Serial.println("break");
        break;
      }
    }
  }while(c != '\n' && c!= '\r');    
}

I use while (Serial.available() > 0) in the hope the program could wait until I enter in the keyboard, but the wield problem is the serial doesn't recognize the '\n' or '\r', so it would print the char each time I enter but never print the 'break'.

How can I do it

\$\endgroup\$
3
\$\begingroup\$

First, 'Serial.println("loop")' is going to execute every time loop() is called, whether there are characters available or not. That would show you that the loop is executing but it's likely to obscure the output when you get any.

Second, the while() loop isn't going to wait for you to finish typing; it will only collect the character (or maybe, two) you just typed and then exit, printing out the partial sentence each time.

Third, '*line += (char)Serial.read()' doesn't does something quite different from what I think you intended. If you're trying to put characters into an array, you need either an index or a pointer into the array which you increment after storing each character.

Here's an example (there are other ways to do it to avoid where some duplicate code, but this way is closer to your original program structure):

char line[80];
int count = 0;

void setup() {                
   Serial.begin(9600);
   Serial.println("loop");  // prompt for a sentence
}

void loop() {

   // loop() will called faster than you can type so there's
   // no need for the next block to loop; you need only test
   // once for the presence of one or more characters.
   if (Serial.available() > 0) {

      line[count] = (char)Serial.read(); // store the char
      if (line[count++] == '\r'){        // if its a CR,
         line[count] = '\0';             //  zero-terminate it
         Serial.println(line);           //  and print it.

         Serial.println("loop");         // re-prompt
         count = 0;                      //  and reset the index.
      }
   }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It's worth noting that if you enter more then 80 chars without pressing "enter", this program will start writing to memory outside of the array line. It may work, or it may clobber whatever variables/program-opcodes the compiler placed in the memory immediately after the line variable. \$\endgroup\$ – Connor Wolf Jun 6 '12 at 4:10
  • \$\begingroup\$ Good point. Though, as the OP seemed to be a first exercise in serial i/o, it seemed counter-productive to add much more than fixing what was already there. I often take shortcuts when I'm writing experimental sequences that I wouldn't for production code code. Sure you can get bitten, but you can get yourself in a twist just as easily by trying to do too much in an experiment. (The aphorism, "When you’re up to your *ss in alligators, it’s hard to remember that you came to drain the swamp", comes to mind....) \$\endgroup\$ – JRobert Jun 6 '12 at 13:55
  • \$\begingroup\$ the wield problem is the serial can't recognize the '/n' or '/r' \$\endgroup\$ – user824624 Jun 8 '12 at 18:51
  • \$\begingroup\$ Are you putting in '/n' or '\n'? Note the backslash (instead of forwardslash). Literals always use a backslash. \$\endgroup\$ – Shamtam Jun 9 '12 at 5:28
  • \$\begingroup\$ The code as written should detect the Enter key and it assumes the Enter key sends CR only. If your terminal program sends CR & LF, the code will have to read and discard one more character each time it gets a CR. \$\endgroup\$ – JRobert Jun 9 '12 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.