Code to take and store N samples in a X second interval

I could use some help taking my code to the next level. I have coded a block where the user specifies, they want N samples in a X second interval. But I cannot figure out how to make the N dynamic/different than just one value. For example, the code below works only when N is 5.

I can very seldomly publish data but when I do, I'd like to publish statistics like min, max, average ..etc. of the data between publishing intervals. So I need to build a population of samples.

Does anyone know how to do this better?

Void function(int Interval, Scan, Nsamples){
	
while (Signal == 1 && PublishSignal == 0){
	
	//at start of interval determine when to record measurements
	while(ACC == 1){
	Sample1Collect = 1;
	SampleStepSize = round(Interval/Nsamples);
	Sample2Collect = Sample1Collect+SampleStepSize;
	Sample3Collect = Sample2Collect+SampleStepSize;
	Sample4Collect = Sample3Collect+SampleStepSize;
	Sample5Collect = Interval;
	}
	
	//count up until interval is reached and then publish results
	if (Signal == 0){
		ACC = 0;
	}
	else if (Signal ==1 && ACC < Interval) {
		ACC = Interval+Scan;
	}
	else if (Signal == 1 && ACC >=Interval) {
		ACC = 0;
		PublishSignal = 1;
	}
	else {
		ACC = 0;
    }
	
	//record measurements
	if(ACC==Sample1Collect){
		Sample1 = measurement;
	}
	else if(ACC==Sample2Collect){
		Sample2 = measurement;
	}
	else if(ACC==Sample3Collect){
		Sample3 = measurement;
	}
	else if(ACC==Sample4Collect){
		Sample4 = measurement;
	}
	else if(ACC==Sample5Collect){
		Sample5 = measurement;
	}
	else{
	//do nothing
	}
	
	scanmili = scan*1000;
	delay(scanmili)
}
}

I'd like to go ahead and address the fact that the code uses a loop that is reliant upon a delay(miliscan) for timekeeping. This is not optimum, but because my interval timeframe is typically going to be about an hour and I am talking maybe 20 samples (3 minute scan) the delay introduced by the actual execution of the code will be minimal. But if someone has a clean and simple solution to this I would be happy to hear.

Study the Blink Without Delay example in the IDE to learn how to time sample intervals.

If you want to handle a variable number of samples,
put the samples in an array:

void function(unsigned IntervalSeconds, unsigned Nsamples)
{
  unsigned long IntervalMilliseconds = IntervalSeconds * 1000ul;
  unsigned long SampleInterval = IntervalMilliseconds / Nsamples;
  int Samples[Nsamples];

  //record measurements
  for (int i = 0; i < Nsamples; i++)
  {
    Samples[i] = measurement;
    delay(SampleInterval);
  }
}
1 Like

Does that compile? What are these alleged samples, how are they taken? Your code doesn’t seem to actually do anything.

Could you present a full working sketch that shows what you having trouble with?

a7

This is excellent and what I was looking for! I will try to put it to good use.

One comment if I specify (IntervalSeconds, Nsamples) as (60s, 2) I would like to take a sample at the beginning of the interval and at the end. In this case my SampleInterval would also equal 60s or SampleInterval=IntervalSeconds/(N-1)

This seems to be the case for Nsamples>1.

@alto777 you have pointed out an error in my example code. I had strange variable names that weren't easy to follow in my original code so while I was making this post I made some quick edits so that the variable names were in sync with their functionality. During this process I made a mistake.

No worries. I make better mistakes. I just didn’t know how to fix something that didn’t do anything!

But @johnwasser has a better crystal ball imagination. :expressionless:

a7

1 Like