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.