Go Down

Topic: How to create multiple serial.read character?? (Read 823 times) previous topic - next topic

Tom@Jerry

i have problem in create a multiple serial.read char in which, under serial.read character, there is another serial.read character.

\
Code: [Select]

void setup()
{
  Serial.begin(9600);
  Serial.println(" POWER ON ");
}

void loop()
{
  int inLetter;
  int inKEY;
 
  if(Serial.available())
  {
    inLetter = Serial.read();
    if (inLetter == 'X')
    {
      Serial.println("X");
     
      if(Serial.available())
      inKEY = Serial.read();
      {
      if(inKEY == 'A')
      {
        Serial.println("A");
      }
      if (inKEY == 'B')
      {
        Serial.println("B");
      }
      }
    }
     
      if (inLetter == 'Y')
      {
        Serial.println("Y");
      }
 
  }
}


my problem is to key in char "A" after enter char "X", still cant solve it... anyone have an idea to do so?? thanx in advance.

liudr

You should find a better translator. Try google translate. Many of us are not native English speakers so your poor English is really not helping us understand what you want.

I guessed what you want is to capture X and then A from serial port and do something when this happens.

You have a loop, the loop() function to loop around while you check for X. That works. You need another loop to check for A:
Code: [Select]
void loop()
{
char in_byte=0;

while (!serial.available()) //Wait for incoming character
{}

in_byte=Serial.read();
if (in_byte=='X')
{
  while (!serial.available()) //Wait for incoming character
  {}
  in_byte=Serial.read();
  if (in_byte=='A')
  {//Do your X then A action
  }
}
}

HazardsMind

I think I know what you want but just to be sure.
If X is pressed then you want to check if the A or B is then pressed.

OR

do you want to read in multiple characters at one time, A string of characters?

XA or XB

reading in a string of characters is easy, but if you want to read X in first then check if either A or B comes in next, then that might be a little tougher to do.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Tom@Jerry

i had tested ur coding, but only first loop success (the 'X'),...

Tom@Jerry

Quote
I think I know what you want but just to be sure.
If X is pressed then you want to check if the A or B is then pressed.


yep2... i want to pressed X, then i want to choose whether A or B... so its just like( key in char )under the( key in char ).

HazardsMind

Did you liudr's code? and did that didn't work?

what does it output to the serial monitor?
change this in his code.
char in_byte=0;  ===>  char in_byte= ' ';
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Tom@Jerry

Code: [Select]

void setup()
{
  Serial.begin(9600);
  Serial.println("POWER ON");
}

void loop()
{
char in_byte=' ';

while (!Serial.available()) //Wait for incoming character
{}

in_byte=Serial.read();
if (in_byte=='X')
{
  Serial.println("X");
  while (!Serial.available()) //Wait for incoming character
  {}
  in_byte=Serial.read();
  if (in_byte=='A')
  {//Do your X then A action
  Serial.println("A");
  }
}
}



still cannot, after i key in 'X' its shown the serial.print"X", but then, i key in 'A', nothing happen....

HazardsMind

Oh and one more thing, after you get a char in, you should clear it. It is not really necessary in your case, but it won't hurt it.

if (in_byte=='X')
{
  in_byte=' '; // clears for new char
  while (!serial.available()) //Wait for incoming character
  {}
  in_byte=Serial.read();
  if (in_byte=='A')
  {
   in_byte=' ';
//Do your X then A action
  }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Tom@Jerry

yeay!!!!..thanx HazardsMind... the coding is working...  :)

AWOL

Quote
after you get a char in, you should clear it.

Why?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

HazardsMind

@AWOL
I did say after, It is not really necessary in your case, but it won't hurt it.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

liudr

Make sure you have the right option as indicated in the red box.

I don't think clearing the in_byte has any effect to the program flow. Probably OP made the change of the option in the monitor dialog box so it now is sending characters without end of line characters.

Go Up