Reputation: 746
I have data from a sensor which gives me the distance of 2 types of trains (small and big) per clock cycle. I want to register the number of times the distance is measured then store this value in an array, and sort this array to find how many big trains and small trains have passed.
For example, I will receive from the sensor for train 1 there values (500,500,500,500,500, 502, 523, 500, 500). Because the train is 500 mm away from the sensor for 10 clock ticks. And for train 2 (small train) (500, 500, 500) because the train passes by the sensor for 3 clock ticks.
By knowing how many sensor pulses I got, I can determine the train length.
But how can I sort it properly? The train's pulses are not equal
i.e a big train might have 10 values or 11 values.
This is my following attemps to store the tick counts. Which does not work and I am not able to sort this.
int i=0;
int counter=0;
int train[6];
if(adc_result > 5)
{
counter++;
}
train[i] = counter;
sprintf(result, "%d", train[i]);
USARTWriteChar(result);
USARTWriteChar("\r\n");
i++;
Upvotes: 0
Views: 195
Reputation: 23226
Here is an answer with as much specificity as your question:
#include <windows.h>
#include <ansi_c.h>
#include <stdio.h>
//Assumption: short train is <= 5 clock cycles long (simulated as value of 30)
//Assumption: long train is > 10 clock cycles long (simulated as value of 50)
int randomGenerator(int min, int max);
static uint32_t temper(uint32_t x);
uint32_t lcg64_temper(uint64_t *seed);
int trainType[2]={0,0};; //[0] counts short, [1] counts long
int main(void)
{
int i=0, type;
while(i++ < 1000)
{
//simulated train generator
type = randomGenerator(0,100);//out of a 1000 cycles will hit 30 and 50 a few times.
//0==no train has occured, do nothing
//1==short train has occured, increment short train count
//2==long train has occured, increment long train count
switch(type) {
case 30://short train
trainType[0]++;
break;
case 50: //long train
trainType[1]++;
default: //all other numbers - ignore
break;
}
}
return 0;
}
int randomGenerator(int min, int max)
{
int random=0, trying=0;
uint64_t lSeed;
trying = 1;
while(trying)
{
srand(clock());
random = (rand()/32767.0)*(max+1);
((random >= min)) ? (trying = 0) : (trying = 1);
}
return random;
}
Upvotes: 1