Arduino Gear Position Sensor

Hi everyone,

I'm new to Arduino and require a few tips on how to code for the following situation:

Using a potentiometer attached to the gear selector lever of a 2009 KTM EXC 450, I would like to determine the gear engaged.

For every up shift the potentiometer shows a positive deflection, and vice versa for a downshift. The problem comes, however, when differentiating between Neutral gear position and the standard '0' deflection as the lever returns to its normal position.

I have tried using an if loop within a case structure to no avail.

Any ideas would be much appreciated.

Here is a sample of my case structure:

Switch (gear) {

Case 1:
if ((voltage >= 2.5) && (gear < 6)) { //Change loop to (gear = value) && (voltage = value) [same kick deflection]
gear = gear + 1;
}
else if ((voltage <= -2.5) && (gear > 1)) {
gear = gear - 1;
}
break;

Case 2:

if ((voltage <= 1.25) && (gear = 1)) {
gear = N
}
break;
}

Sounds tricky without more inputs. Do you assume that the bike is in neutral when you start the arduino?

I assume that you have some time limit between checks of the pot too to avoid recording multiple gear changes for a single move of the gearshift.

I'd be surprised if you get this to be particularly accurate.

The program is supposed to read the last stored gear from the EEPROM and begin its loop from that point.

My main concern is with the actual shift sequence code, and how to get it running without error.

else if ((voltage <= -2.5) && (gear > 1)) {

This (-2.5) could be an error. I haven't check thoroughly though. Please post all the code. I think its better to somehow have a permanent linkage between the pot and the shift system than saving the last position. What will happen if the gear gets shifted while Arduino is off? Can that be possible??? Conversely, Arduino will look for gear position at start up by reading the pot position regardless of changes while sleeping.

On a closer look there are more problems with the code.

1- You haven't post the complete code. That makes it harder to analyze.
2- I assume since gear is a counter it was defined as an int type variable. Then it can only take integer values. The statement gear=N; is just not possible as 'N' is a char type variable.
3- You should have alert messages at the bottom telling you of possible errors.
4- From:
if ((voltage >= 2.5) && (gear < 6)) { //Change loop to (gear = value) && (voltage = value) [same kick deflection]
gear = gear + 1;
}
else if ((voltage <= -2.5) && (gear > 1)) {
gear = gear - 1;
}
Problems:
-The case voltage ==2.5 is true in both situations. You have to use the "=" in one of them only.
-Arduino can't read negative voltages. The condition voltage<=-2.5 is not possible and therefore always false.
-Unless you have made the conversion from the int value read by Arduino which must be within the interval (0-1023) you can't compare voltage to 2.5. I don't know, you did not presented that part in the posted code.

Shoud be

if(voltage>=2.5&&gear<6)
{
gear=gear+1;
}
else// Not needed
{
//Nothing. No else required.
}

if (voltage<2.5&&gear>1)
{
gear=gear-1;
}

Another way:

if(voltage>=2.5)
{
gear++;// Same thing as gear=gear+1;
}
else
{
gear--;
}
I think you don't need to check for gear also. If properly done you can't have voltages and gears differing.
Anyways another way to go is:
gear=constrain(gear,1,6); //Check the constrain() function on the Reference at the top menu of this page.

Thank you arduinoadrian,

Unfortunately I've only begun with the loop for the gear sequence. I figured this is the hardest bit and my plan was to add the inputs and EEPROM code only after getting a successful loop.

I appreciate your assistance.

I wrote a code that may help you:
This code allows to identify various voltages steps applied to one of Arduino analog inputs. Gear shifts are used as example. Using permanent resistors as voltage dividers and keys it could be used to implement a one line keyboard without using digital inputs. I'm using a 5K potentiometer which wiper is connected to analog input A0 and the extremes to ground and Arduino 5V Power Supply. Rotating the pot simulates the shifts. The gear engaged can be viewed in the serial monitor.
Careful with that bike though. I ride mine every day and have to be dodging the cars constantly in this awful traffic. :0
Hope it helps you and others interested.
I'd like to see someone implementing the one line keyboard. I just don't have the time to do it.

Sorry, I forgot break;. Since the conditions are mutually exclusive and the pot can't be at 2 different positions at the same time or change significantly while the loop is checking, adding a break; once the position have been found will help increase efficiency and speed of the Arduino loop, particularly when low gears are engaged. It will simply stop searching when the position have been found.
Like this:
Gear=i;
break;

Many thanks for the assistance. I'm really grateful for the help..!!