Pages: [1]   Go Down
Author Topic: How to create multiple serial.read character??  (Read 656 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

\
Code:
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.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
  }
}
}
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ).
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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= ' ';
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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....
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
  }
}
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeay!!!!..thanx HazardsMind... the coding is working...  smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25752
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3417
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@AWOL
I did say after, It is not really necessary in your case, but it won't hurt it.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* arduino serial monitor.png (35.4 KB, 409x361 - viewed 21 times.)
Logged


Pages: [1]   Go Up
Jump to: