read several characters in arduino mega

i have my mega board. i want to read for characters . whereas only 'one' character is read. All others are not.
here is the program:
#include <SPI.h>

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

DDRA = 0xff;
}
void loop()
{
char one = Serial.read();
char two = Serial.read();
char three = Serial.read();
char four = Serial.read();

switch(one)
{
case '1':
PORTA = 0xFF; //THESE VALUES CAN BE VARIED AS YOU WANT
break;
case '2':
PORTA = 0x00;
break;

}
switch(two)
{
case '1':
PORTA = 0xFF; //THESE VALUES CAN BE VARIED AS YOU WANT
break;
case '2':
PORTA = 0x00;
break;

}
switch(three)
{
case '1':
PORTA = 0xFF; //THESE VALUES CAN BE VARIED AS YOU WANT
break;
case '2':
PORTA = 0x00;
break;

}
switch(four)
{
case '1':
PORTA = 0xFF; //THESE VALUES CAN BE VARIED AS YOU WANT
break;
case '2':
PORTA = 0x00;
break;

}

}

You need to check if there are characters to read first.

if (Serial.available>3){ // 4 character ready?
char one = Serial.read();
char two = Serial.read();
char three = Serial.read();
char four = Serial.read();

then go into switch:case statememts.

Oops - syntax not quite correct:
Serial.available() >3

regards
vanashree

i have my code here where i am trying to read the variable value but it shows nothing in pin13. as i try to blink led 13 once the data is recvd..it compiles but doesnot go high low as it should upon reception of code i want to check wheather i am recvng propoer code and are there any mistakes in this program?

int led = 13;
void setup()
{
char myarray[4] = {'A','B','C','D'}; // char of strings which i want to recv
char x;
int led = 13;
pinMode(led, OUTPUT);
}
void loop()
{
char x;
int led = 13;
pinMode(led, OUTPUT);
  while (Serial.available() > 0)
{
if (Serial.read() == 'A') // here i want to say that if my string starts with A it is recvd n stored in x
{
char x = Serial.read();

}
else if (Serial.read() == 'B') // here i want to say that if my string starts with B it is recvd n stored in y
{
char y = Serial.read();
}
else if (Serial.read() == 'C') // here i want to say that if my string starts with C it is recvd n stored in w
{
char w = Serial.read();
}
else if (Serial.read() == 'D') // here i want to say that if my string starts with D it is recvd n stored in z
{
char z = Serial.read();
}
if (char x = '1');
  {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
}

}

ACTUALLY,
IF there can be some way where i can read my x ,y,w,z, values? i tried doing serial.print(x);
the code is here:

void setup()
{
char myarray[4] = {'A','B','C','D'};
char x;
}
void loop()
{

  while (Serial.available() > 0)
{
if (Serial.read() == 'A')
{
char x = Serial.read();

}
else if (Serial.read() == 'B')
{
char y = Serial.read();
}
else if (Serial.read() == 'C')
{
char w = Serial.read();
}
else if (Serial.read() == 'D')
{
char z = Serial.read();
}
char x;
Serial.print(x);
}

}

i just want to read what x has exactly?
help me to do so kindly suggst somehing and just make this prog running
:cold_sweat:

  while (Serial.available() > 0)
{
if (Serial.read() == 'A')
{
char x = Serial.read();

What are the chances that "x" is not -1, if the character that you knew was there was 'A'?

i didnt get u??
u said that:
What are the chances that "x" is not -1, if the character that you knew was there was 'A'?
can u elaborate?
i am sending A from serial monitor so from where -1 comes?
if trying to say something else kindly elaborate since i am a learner in arduino.

vanashree:
i didnt get u??

if trying to say something else kindly elaborate since i am a learner in arduino.

You are sending 1 character but you are trying to receive 2 characters by using Serial.read() twice.

Each time you call Serial.read() it removes a character from the input buffer. It returns -1 if the buffer is empty.

...R

vanashree:
i didnt get u??
u said that:
What are the chances that "x" is not -1, if the character that you knew was there was 'A'?
can u elaborate?
i am sending A from serial monitor so from where -1 comes?
if trying to say something else kindly elaborate since i am a learner in arduino.

try this.

You will take one char from the serial buffer (if available) and if it matches any of your four letters or '1' it will behave accordingly.

int led = 13;
void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}
void loop()
{
  if  (Serial.available())  // is there anything in the serial buffer?
  {
    char myChar = Serial.read();
    if (myChar == 'A') // is the char you received an 'A' ??
    {
      Serial.println(myChar);
    }
    else if (myChar == 'B') // here i want to say that if my string starts with B it is recvd n stored in y
    {
      Serial.println(myChar);
    }
    else if (myChar == 'C') // here i want to say that if my string starts with C it is recvd n stored in w
    {
      Serial.println(myChar);
    }
    else if (myChar == 'D') // here i want to say that if my string starts with D it is recvd n stored in z
    {
      Serial.println(myChar);
    }
    if (myChar = '1');
    {
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);               // wait for a second
      digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    }
  }
}

A simple example of capturing characters sent from the serial monitor and operating the arduino LED.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

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

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

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

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

thanks i got it workig.i useda another logic n have attached my prog.
thanks a lot now its working.i just have one smaall isssue.and that is in last few if elese where on recving 1 or 2 or 3 in my 7th strin i want to make my respective pin high onlly for 2 seconds and then back to low until i recieve next codde.
i have posted my problem in new topic already.

full_prog_without_print.ino (11 KB)