Go Down

Topic: loop to compare data from sensor reading (Read 1 time)previous topic - next topic

selarmoon

Aug 22, 2012, 08:09 pm
Hi
i have 1 variable called "ms" which is a data that i get from sensor reading. So ms will always read the data that got sensor
If i want to set the condition to check first that if the second data that I received is less than the first data , if it less than the first data keep it, else throw it away. Then start to compare the latest data that coming with the older data that pass the condition.
For example

ms = 23,20,10,22,9,7,6,5,8,2
first: compare 23 with 20 >> " keep 20
So ms collect: 23, 20
Then: compare 20 with 1o >> ' keep 10
So ms now have: 23,20,10
Then: compare next coming data 10 with 22 >> fail condition throw away this data
So ms have:23,20,10
Then : compare 10 with 9 >> pass condition keep 9
So finally ms have : 23,20,10,9

if ms have 4 data sum it and calculate average and then continue doing this and the first
How do I loop to check the data? it's kinda basic skill but i still confused

Really Appreciate for any help and suggestion, Thank you

PaulS

#1
Aug 22, 2012, 08:20 pm
Quote
i have 1 variable called "ms" which is a data that i get from sensor reading. So ms will always read the data that got sensor
If i want to set the condition to check first that if the second data that I received is less than the first data , if it less than the first data keep it, else throw it away. Then start to compare the latest data that coming with the older data that pass the condition.
For example

ms = 23,20,10,22,9,7,6,5,8,2

ms can't be a variable, and hold all those values. It can be an array, and hold all of them.

Show us the code that you have.

As you say, what you want to do is not that difficult, but we need to see some context.

selarmoon

#2
Aug 22, 2012, 08:25 pm
Here is my snippet
Code: [Select]
`unsigned int overflow_counter;unsigned int time = 0;volatile double ms;char value ;volatile double var = 0;char incomingByte;volatile double timesa[10];unsigned int x = 0;volatile double sum;volatile double average;void t_count() {   timesa[0] = timesa[1];   timesa[1] = timesa[2];   timesa[2] = timesa[3];   timesa[3] = ms;    x++; }void CountTime(){  pinMode( 8, INPUT );  TCCR1A = 0x00;  TCNT1 = 0x0000;    TCCR1B = 0x45;   TIMSK1 = 0x21; }void setup(){  Serial.begin(38400);  CountTime();}void loop(){  if (Serial.available())   {    incomingByte = Serial.read();  // will not be -1    if(incomingByte == 'a')    {    var = 1;    }    else if(incomingByte == 'b')    {    var = 0;    }  }    if (x==4)  {  sum = 0;  sum = timesa[0] + timesa[1] + timesa[2] + timesa[3];  average = sum/10;   Serial.print(average);   Serial.println(",");  x = 0;    }}   ISR(TIMER1_OVF_vect){  overflow_counter++;}ISR(TIMER1_CAPT_vect){ if (var == 1)  {  if (TCNT1 > 65535)  {    time = 65535 + overflow_counter;    TCNT1 = 0x0000;    ms = time*0.0520833;       t_count();        delayMicroseconds(10);    TIFR1 = 0 ;    TIMSK1 = 0x21;  }  else   {    TIMSK1 &= ~(1<<ICIE1);    time = ICR1;    TCNT1 = 0x0000;    ms = time*0.0520833;    t_count();     delayMicroseconds(10);    TIFR1 = 0 ;    TIMSK1 = 0x21;  }  }}`

selarmoon

#3
Aug 22, 2012, 08:28 pm
I just want to add the condition to check the data before adding it

PeterH

#4
Aug 23, 2012, 12:53 am
If you want to keep all the samples and then average them at the end, you could keep them in the array. Each time you get a new value, compare it with the last value in the array to decide whether to append it to the array.

Code: [Select]
`int values[4];int valueCount = 0;...if(newval < values[valueCount]){   value[valueCount++] = newval;}`

If all you're doing is averaging the values, you could do this as they are received:
Code: [Select]
`if(newval < lastval){    sum += newval;    count++;    lastval = newval;}`

Incidentally, there's a typo in your average calculation - you should divide the sum by the number of values i.e. four not ten..

selarmoon

#5
Aug 23, 2012, 06:51 am
Thank you very much PeterH , I will try this after i get home  XD

Go Up