# How do I create a counter to compare with two sets of data

Hi everyone, I am new here to seek help with my code. I am a student from UCD, because of the coronavirus, I was not able to get help from my professor or TA.
I am using the light sensor on the circuit playground to collect data. But I am not sure how do I make a counter for comparing with two sets of data.

float threshold;
float peak;
float trough;
float pulse;

float temp;
bool dir;
int counter;
float increase;

void setup() {
while (!Serial);
Serial.begin (115200);
Serial.println(“Circuitplayground Pulse sensor test”);
CircuitPlayground.begin();
CircuitPlayground.setBrightness(255);
CircuitPlayground.setPixelColor(1, 0, 255, 0);
counter=0;
threshold=240;
temp=0;
increase=0;
}

void loop() {

Serial.println(CircuitPlayground.lightSensor());
delay(20);
pulse = CircuitPlayground.lightSensor();
peak=threshold+10;
trough=threshold-10;

if(pulse>threshold){
CircuitPlayground.setPixelColor(5, 0, 0, 255);
} else {
CircuitPlayground.setPixelColor(5, 0, 0, 0);
}

if (pulse > peak){
threshold=threshold+10;
} else if (pulse < trough){
threshold=threshold-10;
}

if (pulse>increase) {
counter=counter+1;
increase = increase+pulse;
}

if (counter==5){
temp=increase;
increase=0;
}

if (counter==10) {
if (temp<increase) {
dir=true;
} else {
dir=false;
}
counter=0;
}

if (dir==true && pulse > peak){
threshold=threshold+10;
} else if (dir==false && pulse < trough ){
threshold=threshold-10;
}

}

des178Final3.ino (1.32 KB)

Does your program compile with no errors and does it run and are the results what you expect?

Paul

not clear what your code does or is trying to do

what is the data you're trying to collect and what do you want to do with it?

gcjr:
not clear what your code does or is trying to do

what is the data you're trying to collect and what do you want to do with it?

Therefore, in my loop, I wrote pulse = CircuitPlayground.lightSensor(); What I am trying to do is to make a counter to collect the data of pulse, in order to determine the pulse goes up or down. So my counter should be collect 5 numbers from the pulse (the valuable temp represent the set of number), and then collect another 5 numbers(the valuable increase represent this set of number), and finally, compare these two sets of number. if the second set data larger than the first one, the direction is true and that means the pulse is going up. If not, then is false, the direction is going down.

But I am not sure how to make a counter to reach my hypothesis.

gcjr:
not clear what your code does or is trying to do

what is the data you’re trying to collect and what do you want to do with it?

Hello, What I am doing is similar to this project: https://www.instructables.com/id/NeoPixel-LED-Heart-Sensor-Jacket/

I am using the light sensor to collect people’s pulse, I want to compare 2 sets of data to determine if the pulse goes up or down. Therefore, I am trying to make a counter of 5 as the first set of data, and then count another 5 numbers as my second set of numbers. if the second set data larger than the first one, the direction is true and that means the pulse is going up. If not, then is false, the direction is going down.

jakehuang:
if the second set data larger than the first one, the direction is true and that means the pulse is going up. If not, then is false, the direction is going down.

what makes a set larger? the sum of the numbers?

gcjr:
what makes a set larger? the sum of the numbers?

Yes

still a bit unclear what you’re trying to do (especially since you suggest the values are a pulse. why after 5 samples?)

it isn’t difficult to keep track of a count and sum your input. when the count reaches 5, compare you sum to a previous sum and decider what to do. then reset the count and sum to zero.

you could compare each sample to a running average, An += K *(Sn - An-1), to indicate a rising/falling value

gcjr:
still a bit unclear what you’re trying to do (especially since you suggest the values are a pulse. why after 5 samples?)

it isn’t difficult to keep track of a count and sum your input. when the count reaches 5, compare you sum to a previous sum and decider what to do. then reset the count and sum to zero.

you could compare each sample to a running average, An += K *(Sn - An-1), to indicate a rising/falling value

I have my code updated, could you take a look:

if (CircuitPlayground.lightSensor()) { // this means when the sensor read data,
counter++; // the counter begin to count
increase = increase+pulse; // increase is the to add the pulse together
Serial.println(increase);
delay(1000);
if (counter==5){ //when the counter equal 5
temp=increase; // increase equal the temp which is the first set data
increase=0; // and then I want clear the first set of data

} else if (counter==10) { // the counter is sill going on untill 10
if (temp<increase) { //comaring temp with the increase which is the second set of data
dir=true;
} else {
dir=false;
}
counter=0;
}
}

I think the counter is working now, but I am now sure how to two set of data are comparing.

i see comparisons. what exactly are your 2 sets of data?

your code doesn't show how pulse gets set. is this code complete?

don't you need to reset increase when the count is 10?

i understand there's a startup problem, temp has no value the first time count is 5, but why are you comparing every other group of 5? why does your counter go to 10? if temp is initialized to zero, don't make a comparison until it is non-zero.

it might be easier to debug if you simulate data. you could just increment/decrement the value of a simulated value of pulse. you could and some noise using random()