Reading more than 1 byte.

I need to read at ardino more than 1 byte. Now i use smth. like so:

int val = Serial.Read();

if(val == 5)
{
DoWork();
}

and i want to make val more than 1 symbol, or, better - a string. How could i do that?
Arduino Mega, USB connected

Use Serial.available() to determine when enough data has been collected. then read each byte until your data block is full.

void loop(){

  int i_Val;
  
  if( Serial.available() >= sizeof( int ) ){
  
    char *c_Data = &i_Val;

    c_Data[0] = Serial.read();
    c_Data[1] = Serial.read();

    //Data is now in i_Val

    if( i_Val == 5 ) DoWork();
  }
}

A template version of the code can make it easily handle blocks of any size.

If you’re trying to read a string from Serial Monitor, you should do something like this:

while(Serial.available()){
  int val = Serial.read();
  inString += (char)val;
}
if(inString == "randomness"){ 
  DoWork();
  inString = "";
}

Declare inString as global variable. I also made a program that reads the string from Serial Monitor in this format: 123,189,34! (red,green,blue!) to set colors of the common cathode RGB LED.

#include <RGBled.h>
RGBled rgb(0, 9, 10, 11);
String inString = "";    // string to hold input
int currentColor = 0;
int red, green, blue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int inChar;

  if (Serial.available() > 0) {
    inChar = Serial.read();
  }

  if (isDigit(inChar)) {
    // convert the incoming byte to a char 
    // and add it to the string:
    inString += (char)inChar; 
  }

  // if you get a comma, convert to a number,
  // set the appropriate color, and increment
  // the color counter:
  if (inChar == ',') {
    // do something different for each value of currentColor:
    switch (currentColor) {
    case 0:    // 0 = red
      red = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    case 1:    // 1 = green:
      green = inString.toInt();
      // clear the string for new input:
      inString = ""; 
      break;
    }
    currentColor++;
  }
  // if you get a newline, you know you've got
  // the last color, i.e. blue:
  if (inChar == '!') {
    blue = inString.toInt();

    red = constrain(red, 0, 255);
    green = constrain(green, 0, 255);
    blue = constrain(blue, 0, 255);
    rgb.customRGB(red,green,blue);

    // clear the string for new input:
    inString = ""; 
    // reset the color counter:
    currentColor = 0;

  }

}

Thanks all.

Who's idea actually worked?