how do i limit the serial.available() or serial.read() to four byte only

hello

how do i limit the serial.available() or serial.read() to four byte only im using cmd line of android to input data to arduino through bluetooth module and gsm module.. i have this condition that if the user inputted wrong password 3x consecutively a message will be send to certain number the password (char correct[4] = {'a','b','c','d'}:wink: and

void loop()
{
** while(Serial.available())**
** {**
** for(int i=0; i<4;i++) //While data is available read 4 bytes**
** {**
__ inputpw = Serial.read(); //Read 4 bytes into the array labled__
* }*
* for(int i=0; i<4;i++)*
* {*
if(inputpw_==correct*) //Compare each char received to each car in our password in order*
* {_
pass_correct = 1; //If we compare two chars and they match, set the pass_correct variable to true(1)
_ }
else*
* {_
pass_correct = 0; //if the two compared chars do NOT match, set pass_correct variable to false(0)
_ pass++;
Serial.println("Incorrect Password");
break;
}
}[/b]*
if "pass" reaches 3 the message will be send but if i input more than 4 letters or >4kb through the my android my code is getting messed up please help thanks in advance_

how do i limit the serial.available() or serial.read() to four byte only

You don't.
If you only want to read 4 bytes then only read 4 bytes. If you have more then either don't send them or read them until the buffer is empty.

while(Serial.available()) 
      {
        for(int i=0; i<4;i++)   //While data is available read 4 bytes
          {
             inputpw[i] = Serial.read();  //Read 4 bytes into the array labled 
           }

To me this says, "if there is any amount of serial data available, even one byte, then read 4 bytes into the array"

Incidentally, whilst your code looks very pretty mostly in italics it would look much nicer in code tags as above and the array index would not go missing from the code either.

newbieofarduino:
how do i limit the serial.available() or serial.read() to four byte only

That's a curious way to express it, but if you want to read four characters then wait until Serial.available() returns four and then call Serial.read() four times.

Note that many clients such as the Arduino IDE serial monitor will include end-of-line characters in the input and you would need to deal with those. A cleaner solution would be to read and buffer characters until end-of-line is received, and then process the text that was received.

if you want to read four characters then wait until Serial.available() returns four and then call Serial.read() four times.
A cleaner solution would be to read and buffer characters until end-of-line is received, and then process the text that was received.

can you give me sample codes for it thanks

newbieofarduino:
can you give me sample codes for it

What do you mean by 'it'?

Change this line:

while(Serial.available())

to

if (Serial.available()>3) // 4 or more bytes are ready to be read.

Note that many clients such as the Arduino IDE serial monitor will include end-of-line characters in the input

Only if you change the default settings.

i give up~

What are you stuck on ?

newbieofarduino:
i give up~

All the information you need is in this thread.

newbieofarduino:
i give up~

You have a lot of code stuff going on that may be causing confusion. Below is servo test code that shows a simple way the first four characters are captured from a character string sent from the serial monitor. The entire character string is captured into a String, then the String functions are used to extract the first (and second) four characters in the captured character String.

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
            
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
    servo1="";
    servo2="";
  } 
}