serial reception for a prescribed amount of time

Greetings everyone, I want a program for which I want to receive serially for a particular amount of time and for the rest of the time it should not receive (if it is received and stored in buffer it should be discarded before entering the reception time).

int reading = 0;
long interval = 5000;
long previousMillis = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
 unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval)
{
   if(Serial.available())
{
reading = Serial.read();
Serial.flush();
}
previousMillis = currentMillis;
}
}

Does this code operate the way I described? or the below one??

int reading = 0, count = 0;
long interval = 5000;
long previousMillis = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
 unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval)
{
   Serial.flush();
count = 1;
previousMillis = currentMillis;
}

if(count == 1)
{
if(Serial.available())
{
reading = Serial.read();
}
}
}

in second code I feel I'm missing continuous reception.
can anyone write a code for continuous reception for a time of "internal" after which count has to be zero(count = 0)

please help :slight_smile:

can anyone write a code for continuous reception for a time of "internal" after which count has to be zero(count = 0)

It's not our practice to write code for poster because this gets abused too often by students who are too lazy to do their own homework and come here asking us to do it for them so they can go out and party. We know you are not one of those, right ? XD

We'll be happy to give you suggestions and point you to tutorials but asking us to write your code is crossing the line. We are here to help people learn , not to do their homework. I am sure you want to learn.

Please see this link, it says it waits for transmission to complete if we are sending any, and clears the buffered data came from other source.

If I'm wrong please enlighten me. :slight_smile: :slight_smile:

Yes, pardon me about it. =(

This is a part of a huge program which I'm working on based on Swarm robotics.
In my program the count will become zero at some point which is independent of "interval" so till then it must receive.
So i cant make count = 0.

Till count becomes equal to zero it has to receive.

I'm using arduino software version 0023

I'm using arduino software version 0023

Why would you be doing that ?

why Sir? Any problem with it? :roll_eyes:

Well not specifically, but as pointed out the function of Serial.flush() is different. The question is WHY are you using 0023 ?
You answered with a question "WHY ?" so you didn't answer my question. Is there a reason why you are avoiding my question ?

Well I have not updated the software to latest version, but currently I'm doing so. :blush:

I have a doubt regarding the Serial.flush() function. Is it different for "arduino software version 0023" and Arduino IDE 1.0.5 ??

YES, ALMOST OPPOSITE if you want to compare the two. Click on the link for the flush tutorial in the previous post.