Go Down

Topic: Read data from serial and wait for two bytes to be recieved (Read 1 time) previous topic - next topic


I am relatively new to programming the Arduino, and I've seen example programs which use
Code: [Select]
Serial.available() > 0 and read the byte in the buffer, but I'm looking for some example code that waits for a pair of bytes, and sets the value of two variables, each variable for each byte received.

Since I am pretty new to programming the Arduino, so I'd be very grateful to anybody who would be willing to create some example code which does what I described above.


First of all have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

If you can, it is easiest if you receive the entire message before trying to figure anything out.

However you have not given us any information about the type or length of the data you are trying to receive.

Two or three hours spent thinking and reading documentation solves most programming problems.


If it helps, the type of data I'm trying to receive is some unsigned 8 bit raw byte data.


Also, keep in mind that I opened this thread looking for somebody to reply with some example code that receives two bytes, not for somebody to tell me that I asked the question wrong and then refer me to some code that doesn't execute the criteria I listed. :(


Well, bear in mind that this forum is for paid assistance or finding someone to work on a project with you. There are other fora for general help. That said: this should work

Code: [Select]

while (Serial.available < 2)

int b1 = Serial.read();
int b2 = Serial.read();

There are "better" ways to do it but this is straightforward and easy to follow.
Electronics and firmware/software design and assistance. No project too small


There are "better" ways to do it but this is straightforward and easy to follow.
So is
Code: [Select]
   if(Serial.available() > 1)
      v1 = Serial.read();
      v2 = Serial.read();

without the need for blocking code.
The art of getting good answers lies in asking good questions.

Go Up