Stop reading data

I am reading potentiometer input using following code:

#define BUFFERSIZE 25
int buffer[BUFFERSIZE];
int idx = 0;
int average;
int oldtime = 0;
int timer = 200;

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

void loop()
{

buffer[idx++] = analogRead(A0);
idx %= BUFFERSIZE ;

if (millis()>(oldtime+timer))
{
	average = 0;
       for (int i =0;i<BUFFERSIZE;i++)
	    average+=buffer[i];
       average/=BUFFERSIZE;  
       Serial.print(average);             
       Serial.println("\"");
      oldtime=millis();
}
}

for some reason, the program stops showing data in serial monitor after a certain time. any help to solve the problem would be highly appreciated.

Your time variables must be of the unsigned long type not int

This code

if (millis()>(oldtime+timer))

should read

if (millis()-oldtime>timer)

To deal with rollover correctly

This
idx %= BUFFERSIZE ; should be

if (idx == BUFFERSIZE -1)idx =0;

Which I think is your bug

Mark

Thanks for the reply. SOLVED.

  1. Why read more than bufsize samples?

  2. What was the solution?

Feed back to those how give there time of free is nice and alot of people do read the questions that have already been answered and would like to know what worked!

Mark