making Serial.read () to read strings

I have no idea on how to make Serial.read() to read a bunch of characters like "1234A" at once..It only reads one character at a time..

You can't. You read them one by one and store them in a null terminated array of char. This is known as a string (lower case 's') and not a String (upper case 'S') which is something different.

Mark

Serial read only reads on byte at a time. That is the way it works. The message that is sent goes into a buffer. The serial read reads one byte of the buffer on each call to serial read, serial available tells you how many bytes are in the buffer to be read. use a loop to read a byte and add it to the string, repeat for available bytes.

http://www.gammon.com.au/serial

Simple example the String function collecting a string of characters sent from the serial monitor into a String (not to be confused with a c-string null terminated character array).

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial test 0021"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

This is what i did... It gets denied every time like when i enter A it gets denied ,next time when i enter B,both AB comes together but gets denied right away..It doesnot wait until all the characters are entered

String readString;
const int ledPin=10;
void setup(){
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  
}
void loop(){
  while (Serial.available()){
    delay(50);
    char c = Serial.read();
    readString +=c;
    if (readString.length()>0){
      Serial.println(readString);
      
      
      if (readString == "1ABCD")
     { digitalWrite(ledPin, HIGH);
      Serial.println("Access Granted");
    }
      
      else 
      {digitalWrite(ledPin, LOW);
      Serial.println("Access DENIED");
    }
  }
  }}

http://www.gammon.com.au/serial

Read it. Go on, you know you want to. :)

Using the String class to buffer your received data is a bad idea, and using delays to control the speed of reading characters is a bad idea.

Follow the link Nick Gammon posted to see how to do it properly.

Thisproject: This is what i did... It gets denied every time like when i enter A it gets denied ,next time when i enter B,both AB comes together but gets denied right away..It doesnot wait until all the characters are entered

That's right, the code does not wait. That's why that is not a good method.

This is what i did... It gets denied every time like when i enter A it gets denied ,next time when i enter B,both AB comes together but gets denied right away..It doesnot wait until all the characters are entered

You need to explain what you are talking about when you say "denied". Also you need to think about how the arduino is to know when you are finished sending a data packet. Most data transmissions have a "delimiter" at the end to indicate the end of the data packet so the arduino can start processing he data. The below uses a , to indicate the end of the data packet.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Nick, I tried to read your code but i dont think i understood the code that you have on your website..zoomkat,I was just referring to what was on the serial. print.ln statement which said "Acess denied " on the code that i have it here... The whole purpose is that it should read the passcode which i enter through the keypad which is connected to the transmitting arduino and if the code matches (suppose 1234D) the LED should go ON

It doesnot wait until all the characters are entered

How would it know ?

What is it about Nick's code that you don't understand ?

It reads each character available and adds it to an array until either the maximum number of characters has been read or the character is a newline which indicates that the input is complete. Once that happens it puts a terminating null character at the end of the string so that it can be used by other functions.

Where do i put my passcode suppose (1234D) and test it out? He has a section that says // do other stuff here like testing digital input (button presses) …where the strings are saved ? please help me out …Is it like if {input_line== “1234D”) do something }

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  // do other stuff here like testing digital input (button presses) ...

}  // end of loop
if {strcmp (input_line, "1234D") == 0 )

could you check this code which i took from nick’s website …It still not doing the right thing…sorry i am just a beginner…need to learn a lot …

// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
const int ledPin =10;

void setup ()
{
  Serial.begin(9600);
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data
  

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  if (strcmp(input_line,"1342D")==1)
  {digitalWrite (ledPin,HIGH);
  Serial.println("Acess granted");}
  
  else 
   
  {digitalWrite (ledPin,LOW);
  Serial.println("Acess Denied");}
  // do other stuff here like testing digital input (button presses) ...

}  // end of loop
if (strcmp(input_line,"1342D")==1)

When I wrote "== 0" I really meant it.

This seems to be working right…but the LED doesnot go ON

// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
const int ledPin =10;

void setup ()
{
  Serial.begin(9600);
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data
  

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  if (strcmp(input_line,"1342D")==0)
  {digitalWrite (ledPin,HIGH);
  //Serial.println("Acess granted");}
  
 /// else 
   
 // {digitalWrite (ledPin,LOW);
 // Serial.println("Acess Denied");}
  // do other stuff here like testing digital input (button presses) ...

} } // end of loop}}

Are you sure that the serial input has a newline ('\n') as its terminating character ?

if (strcmp(input_line,"1342D")==0)
  {digitalWrite (ledPin,HIGH);
  //Serial.println("Acess granted");}

Please don't put { and } on the same lines as code. It looks messy and in this case you have accidentally commented-out the closing }.

Then you no doubt got a compiler error and threw an extra } onto the last line of code to make it go away.

In fact, what is this?

} } // end of loop}}

Confusing+.

There is no error but the LED on the receiving arduino doesnot go ON…

// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;
const int ledPin =10;

void setup ()
{
  Serial.begin(9600);
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (char * data)
  {
  // for now just display it
  Serial.println (data);
  }  // end of process_data
  

void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;

  if (Serial.available () > 0) 
    {
    char inByte = Serial.read ();

    switch (inByte)
      {

      case '\n':   // end of text
        input_line [input_pos] = 0;  // terminating null byte
        
        // terminator reached! process input_line here ...
        process_data (input_line);
        
        // reset buffer for next time
        input_pos = 0;  
        break;
  
      case '\r':   // discard carriage return
        break;
  
      default:
        // keep adding if not full ... allow for terminating null byte
        if (input_pos < (MAX_INPUT - 1))
          input_line [input_pos++] = inByte;
        break;

      }  // end of switch

  }  // end of incoming data

  if (strcmp(input_line,"1342D")==0)
  {digitalWrite (ledPin,HIGH);
}
  
 }