Offline
Newbie
Karma: 0
Posts: 25
|
 |
« on: December 07, 2012, 11:47:38 am » |
i have problem in create a multiple serial.read char in which, under serial.read character, there is another serial.read character. \ 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
Faraday Member
Karma: 35
Posts: 5920
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #1 on: December 07, 2012, 12:04:39 pm » |
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: 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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #2 on: December 07, 2012, 12:15:00 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #3 on: December 07, 2012, 12:15:39 pm » |
i had tested ur coding, but only first loop success (the 'X'),...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #4 on: December 07, 2012, 12:18:34 pm » |
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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #5 on: December 07, 2012, 12:24:57 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #6 on: December 07, 2012, 12:28:30 pm » |
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
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: December 07, 2012, 12:31:25 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 25
|
 |
« Reply #8 on: December 07, 2012, 12:36:49 pm » |
yeay!!!!..thanx HazardsMind... the coding is working...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19035
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: December 07, 2012, 12:45:09 pm » |
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.
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1563
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #10 on: December 07, 2012, 12:49:48 pm » |
@AWOL I did say after, It is not really necessary in your case, but it won't hurt it.
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5920
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #11 on: December 07, 2012, 12:56:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|