Code for interrupt input

I am using Duemilanove. I need help to write a code that will get the number of non zero values and show that number as a serial output. i.e. I have values like 0,0,0,0,5,6,1,0,0,7,1,0,0,0 and on like that. I need to get the number of non-zero outputs here over 1 minute interval and that number needs to be displayed on serial port. I appreciate any help with the code.

Give this a try:-

int count =0;
int num;
long time;
void setup(){
  Serial.begin(9600);
 time=millis() + 600000; // set up 1 min delay
}

void loop(){
  if(time < millis()) {
    num = getValue();  // what ever function is getting the numbers
    if(num !=0) count++;
  }
  else
  { Serial.println(count);
    count = 0;
    time=millis() + 600000; // set up 1 min delay
  }
}

you need to write the function getValue() to return your numbers

Thanks for the reply. I will try that.

I actually need to count the numbers that are not 1023. Anything between 0-1022 should be counted and serial.print should show that number. I have the following now. And I am only getting 0s on the serial print. Please take a look:

int count =0;
int num;
long time;
void setup(){
Serial.begin(9600);
time=millis() + 600000; // set up 1 min delay
}

void loop(){
if(time < millis()) {
num = analogRead(0); // what ever function is getting the numbers
if(num !=1023) count++;
}
else
{ Serial.println(count);
count = 0;
time=millis() + 600000; // set up 1 min delay
}
}

Are you sure that you are getting some numbers that are not 1023?
That is what are you applying to the analogue input to generate the numbers.

Do a test and put Serial.println(num); just before the if statement, you will see lots of numbers streaming out, are they all 1023?

I have the following code now and I do not see any output at all. But when I try just simple serial.print without the time function that you have, I see values like 1023, 920,840, 1023,532 and such like. Is the if statement for time in void loop() okay?

int count =0;
int num;
long time;
void setup(){
Serial.begin(9600);
time=millis() + 600000; // set up 1 min delay
}

void loop(){
if(time < millis()) {
num = analogRead(0); // what ever function is getting the numbers
//if(num !=1023){
Serial.println(num);
}
}

600000 milliseconds is 10 minutes, not 1 minute.

Wouldn't you want to read a value first, and then wait?

I need to read the values over 1 minute and then suppose I got 10 values in that 1 minute, I need to get the count of values that are not 1024 in those 10 values. That count has to be displayed on serial.print.

My question was this. It appears that you wait one minute, and then read a value, wait another minute, and read another value.

I would expect that you would want to read a value, wait, read, wait, etc.

In any case, I think you are waiting 10 minutes to read a value, rather than counting the number of non-1023 values in 1 minute.

Sorry about that < and > got mixed up and had one to many zeros try this:-

int count =0;
int num;
long time;
void setup(){
  Serial.begin(9600);
 time=millis() + 60000; // set up 1 min delay
}

void loop(){
  if(time > millis()) {
    num = getValue();  // what ever function is getting the numbers
    if(num !=1023) count++;
  }
  else
  { Serial.println(count);
    count = 0;
    time=millis() + 60000; // set up 1 min delay
  }
}

Thanks a lot for the reply. I appreciate your help. But this new code did not work, I do not see anything on my serial port. I have the following now:

int count =0;
int num;
long time;
void setup(){
Serial.begin(9600);
time=millis() + 60000; // set up 1 min delay
}

void loop(){
if(time > millis()) {
num = analogRead(0); // what ever function is getting the numbers
if(num !=1023) count++;
}
else
{ Serial.println(count);
count = 0;
time=millis() + 60000; // set up 1 min delay
}
}

I see nothing obviously wrong with the code. I'd try adding a Serial.print call at the beginning of loop, just to verify that it is being called, and that the communication with the Serial Monitor window is working correctly.

When you know that it is, remove it, and, instead, print the value read from the sensor. That way, you'll know that the sensor is being read.

See the comments:

int count =0;
int num;
long time;
void setup(){
Serial.begin(9600);
time=millis() + 60000; // set up 1 min delay
}

void loop(){
if(time > millis()) {
num = analogRead(0); // what ever function is getting the numbers
Serial.println(num); // this is showing me the serial out
if(num !=1023) count++;
}
else
{ Serial.println(count); //this is not showing anything
count = 0;
time=millis() + 60000; // set up 1 min delay
}
}

Serial.println(num); // this is showing me the serial out

Does this mean that you are seeing reasonable values in the Serial Monitor window?

Are you waiting one minute for the count to appear? Adding some Serial.print() statements with identifying information might be useful.

If nothing else works, try printing time and millis() values. When you open the Serial Monitor window, the Arduino resets. Since millis() is returning the number of milliseconds since the reset occurred, the if test should be true, since time is roughly 60000, and millis returns roughly 0. The value of time stays constant, but the output of the millis function keeps increasing. After 60 seconds, though, millis should be returning a value that is greater than time, and the if should be false, so the else block should be executed.

Therefore, it would be interesting to know what value time has and what value millis returns.

I have this now and it is showing output for the "test". Not the one I need. Can you please write out the part you are suggesting a change on the code?

int count =0;
int num;
long time;
void setup(){
Serial.begin(9600);
time=millis() + 60000; // set up 1 min delay
}

void loop(){
if(time > millis()) {
num = analogRead(0); // what ever function is getting the numbers
Serial.print(num);
Serial.println(" test");
delay(500);
if(num !=1023) count++;
}
else
{ Serial.println(count);
count = 0;
time=millis() + 60000; // set up 1 min delay
}
}

void loop()
{
   Serial.print("time = ");
   Serial.println(time);

   Serial.print("millis() returned ");
   Serial.println(millis());

   if(time > millis())
  {
      num = analogRead(0);
      Serial.print("Sensor reading: ");
      Serial.println(num); 
      if(num !=1023) count++;

      delay(500);   // Hang on half a second, to reduce the
                          // number of readings - remove this later
  }
  else
  {
      Serial.print("Number of bad readings: ");
      Serial.println(count);
      count = 0;
      time=millis() + 60000; // set up 1 min delay
  }
}

I got the following output:

time = 60000
millis() returned 59487
Sensor reading: 1023
time = 60000
millis() returned 60048
Number of bad readings: 33
time = 120085
millis() returned 60119
Sensor reading: 514
time = 120085
millis() returned 60680
Sensor reading: 1023
time = 120085
millis() returned 61242
Sensor reading: 1023
time = 120085
millis() returned 61805
Sensor reading: 509
time = 120085
millis() returned 62367
Sensor reading: 1023
time = 120085
millis() returned 62929
Sensor reading: 1023
time = 120085
millis() returned 63492
Sensor reading: 633

I believe I got it. The bad reading number is what I need, am I correct?

Got it. Thanks Mike and Paul for your help.