Hi there.

I currently have a project in mind (motorcycle gear position indicator). The motorcycle does not have a gear position sensor, so I have used two interrupts to count pulses for the speedo sensor and crank sensor...then I am dividing speedo pulses by rev pulses to give me a (roughly) static ratio for each gear.

What I am struggling with a bit, is coming up with a way to "learn" each ratio and input that into a variable.

The following code is what I have got so far. What I'm trying to acheive is to sample the ratio several times(10), then calculate if each sample is within 10% to ensure I am reading the same ratio each time(clutch isn't pulled in or bike's not in neutral - hence the delays). If all samples are within tolerance, then set the read ratio to currentRatio variable.

I know, this is NOT pretty code! I'm trying to get away from any user interaction when learning gears, as it is obviously a bit of a safety risk.

Any help appreciated.

```
float sampleRatios()
{
float currentRatio;
boolean breakLoop = false;
for(; !breakLoop;)
{
float testRatio1 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio2 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio3 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio4 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio5 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio6 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio7 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio8 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio9 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
float testRatio10 = speedoPulse / tachoPulse; //Calculates the current gear ratio
tachoPulse = 0;
speedoPulse = 0;
delay(500);
noInterrupts();
if (((testRatio1 <= (testRatio2 * 1.1)) && (testRatio1 >= (testRatio2 * 0.9)))) { //If all values are within ~10%, average the values
if (((testRatio2 <= (testRatio3 * 1.1)) && (testRatio2 >= (testRatio3 * 0.9)))) { //and place into variable currentRatio, then return
if (((testRatio3 <= (testRatio4 * 1.1)) && (testRatio3 >= (testRatio4 * 0.9)))) { //that value to program
if (((testRatio4 <= (testRatio5 * 1.1)) && (testRatio4 >= (testRatio5 * 0.9)))) {
if (((testRatio5 <= (testRatio6 * 1.1)) && (testRatio5 >= (testRatio6 * 0.9)))) {
if (((testRatio6 <= (testRatio7 * 1.1)) && (testRatio6 >= (testRatio7 * 0.9)))) {
if (((testRatio7 <= (testRatio8 * 1.1)) && (testRatio7 >= (testRatio8 * 0.9)))) {
if (((testRatio8 <= (testRatio9 * 1.1)) && (testRatio8 >= (testRatio9 * 0.9)))) {
if (((testRatio9 <= (testRatio10 * 1.1)) && (testRatio9 >= (testRatio10 * 0.9)))) {
currentRatio = (testRatio1 + testRatio2 + testRatio3 + testRatio4 + testRatio5 + testRatio6 + testRatio7 + testRatio8 + testRatio9 + testRatio10) / 10;
}}}}}}}}}
breakLoop = true;
}
return(currentRatio);
}
```