Read input from Serial

I am rather new to Arduino and found a lot of tutorials which is cool.

I am also using the Serial to read some data I can send from the PC.
All the examples are something like this:

void readInput(){
   while(Serial.available() == 0){
     //DO nothing untill someone enters something in the serial
  } 
...
}

The problem with this should be rather obvious... It is blocking the controller until data is entered.

I have far more experience with development than with electronics so I was wondering could multithreading solve this problem? does the Arduino even support multithreading?

Is there maybe a library available that does this for me?

I was also thinking on using a push button I should press and then it will listen to the Serial and block the controller.

I have far more experience with development than with electronics so I was wondering could multithreading solve this problem? does the Arduino even support multithreading?

Yes, it could. No, it doesn't.

You don't have to block waiting for input, though. You can read and store data as it arrives. When the end-of-packet marker arrives, use the stored data, then reset the index and start collecting data again.

It is more common to use if(Serial.available() > 0) in the loop() function to detect bytes coming into the serial buffer. Then read the bytes into a buffer and process them. What kind of data is the Arduino receiving (string, integers, float values)? Is the data of fixed length or is it in a delimited packet?

Thanks.

Yes the if(Serial.available() > 0) worked for me.
In my case I only have to get a single char from the Serial so I now have following code which works fine:

void loop(){
   //some code
   getInput();
   //other code;
}

void getInput(){
  Serial.flush();
  if(Serial.available() > 0){
      answer = Serial.read();
      Serial.println(answer);
      delay(5);
  }
}

I am controlling an RGB led and I specify the colour in the Serial so I am providing R,G or B and then I read the potmeter for the value.
Before with the while() for reading the input I only was able to read the potmeter once.
Now with the proper read code I just specify the colour in the serial and can turn the potmeter to adjust the colour.

It’s working just fine now :slight_smile:
If I have 3 potmeter I don’t need to serial anymore but for now I need the serial.

  Serial.flush();

There is no point in doing that. It flushes the output buffer, and there appears to be no good reason for you to do that.

passero:
All the examples are something like this:

I'm surprised at that, and frankly I doubt it's true. That's not at all a common construct, and if all your examples show that then you're probably looking in the wrong place for your examples.

I bought the "Arduino workshop" book which I like to be honest.
On page 106 "Project #13" it shows to use that construct which blocks all other operations.

It uses the same construct over and over in the book.
This is my first book and I am learning everything from that...

I just did a quick search online and you are right... Most online resources use the if().
I just got stuck with a bad habit from the book...

I just got stuck with a bad habit from the book...

It's not necessarily a bad habit. It depends on whether the code needs to block until there is input to process, or not. In many cases, it does not. But, in those cases where it does, the book's approach is correct.