Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
unsigned char CountValues[256];
unsigned char CountValues[1024];
unsigned int CountValues[1024];
unsigned char CountValues[256]; // You first reset the counter values for (int i=0;i<256;i++) CountValues[i] = 0; // Now fill in 100 ADC readings for (int i=0;i<100;i++) { unsigned char AdcReading = GetAdcReadingFromCurrentSensor(); CountValues[AdcReading] += 1; }
CountValues[0]==2; CountValues[1]==4; CountValues[2]==4; .... CountValues[100]=10; ... CountValues[253]=5; CountValues[254]=3; CountValues[255]=2;
unsigned char CountValues[256]; // asuume this is filled in. unsigned char ToBeRemoved; int i; // Start removing values from the top ToBeRemoved = 5; i = 255; while (ToBeRemoved) { if (CountValues[i]) { // My cursor is on value "i"; It's count is NOT zero so there's something to be removed! CountValues[i] -= 1; // Remove the value from the array ToBeRemoved -= 1; // Mark the value removed }; else { // My cursor is on value "i"; While doing ADC I've seen no [i] value so the counter is 0; Or i've // removed all values! Move on to the next value i -= 1; } }
CountValues[0]==0; CountValues[1]==1; CountValues[2]==4; .... CountValues[100]=10; ... CountValues[253]=5; CountValues[254]=0; CountValues[255]=0;
int Summ = 0; for (int i=0;i<256;i++) // You need to go over the whole array! Summ += i * CountValues[i]; Average = Summ / 90; // Fixed value; It's the number of sampled values (from your "sample" loop) minus the number of elements you removed.