# Newbie in need of simple guidance -- If else, maybe??

Hello all!

I am in the middle of a small automotive project and have some code working but not the way I would like it. When using the IF statements it is not working how I would expect.

Objective:
Take a voltage reading from Pin A0
Define that voltage in a range (Gear_Position)
Output a letter to Oled display showing that letter

I know I am missing something simple here but I cannot seem to find a good example on the internet or any blogs/ reading. I have tried using if statements, and If else statements but they always have some glitch.

Any guidance would be much appreciated (I have a version working with Case statements but It leaves room for error in my opinion)

Should this be aIf else style of coding or am I way off with this?

-------------------(data from multimeter in car)------------------

GP0- P- < .95v = 195 raw
GP1- R- 1.32v ±.25 = 269 raw
GP2- N- 1.7v ±.25 = 348 raw
GP3- 1- 2.21v±.25 = 448 raw
GP4- 2- 2.8v ±.25 = 573 raw
GP5- 3- 3.2v ±.25 = 655 raw

-----------------------------------------------Code----------------------

// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:

display.setTextSize(0);
display.setTextColor(WHITE);
display.setCursor(10,100);
display.print(voltage); display.println(“VIn”);
display.print(sensorValue); display.println(“RAW”);
display.print(Gear_Position); display.println(“RAW”);

if (sensorValue < 220 ){ Gear_Position = 0}
else if (sensorValue > 239 || sensorValue < 320){ Gear_Position = 1;}
else if (sensorValue > 360 || sensorValue < 420){ Gear_Position = 2;}
else if (sensorValue > 450 || sensorValue < 500){ Gear_Position = 3;}
else if (sensorValue > 500 || sensorValue < 580){ Gear_Position = 4;}
else if (sensorValue > 600 ){ Gear_Position = 5;}

//int range = map(Gear_Position, sensorMin, sensorMax, 0, 5);

// do something different depending on the range value:
switch (Gear_Position) {
case 0: // Park

display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“P”);
display.setTextSize(2);
display.setTextColor(WHITE);
//display.setCursor(6,105);
//display.println(“Park”);

break;
case 1: // Reverse
display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“R”);

break;
case 2: // Neutral
display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“N”);
break;
case 3: // 1st gear
display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“1”);
break;

case 4: // 2nd gear
display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“2”);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(3,105);
//display.println(" Bye Bye");
delay (10);
break;

case 5: // 3rd gear
display.setTextSize(8);
display.setTextColor(WHITE);
display.setCursor(12,40);
display.println(“3”);
break;
}
delay(100); // delay in between reads for stability

display.display();

Use and (&&) instead of or (||).

``````else if  (sensorValue > 239 && sensorValue < 320){ Gear_Position = 1;}
else if  (sensorValue > 360 && sensorValue < 420){ Gear_Position = 2;}
else if  (sensorValue > 450 && sensorValue < 500){ Gear_Position = 3;}
else if  (sensorValue > 500 && sensorValue < 580){ Gear_Position = 4;}
``````

What happens if sensorValue is in the gaps (like 321 to 359 or 421 to 449 or …)?

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Go for the slots between the gears.

232
309
398
510
596

char sym = ‘P’;
if (raw>232) sym=‘R’;
if (raw>309) sym=‘N’;
if (raw> 398) sym=‘1’;
if (raw> 510) sym=‘2’;
if (raw> 596) sym=‘3’;

-jim lee

Thanks guys. I'll clean up the code and try those things.

This

``````if  (sensorValue < 220 ){ Gear_Position = 0}
else if  (sensorValue > 239 || sensorValue < 320){ Gear_Position = 1;}
else if  (sensorValue > 360  || sensorValue < 420){ Gear_Position = 2;}
else if  (sensorValue > 450  || sensorValue < 500){ Gear_Position = 3;}
else if  (sensorValue > 500  || sensorValue < 580){ Gear_Position = 4;}
else if  (sensorValue > 600 ){ Gear_Position = 5;}
``````

can almost certainly be simplified to this

``````if  (sensorValue < 220 ) {
Gear_Position = 0;
}
else if  ( sensorValue < 320) {
Gear_Position = 1;
}
else if  (sensorValue < 420) {
Gear_Position = 2;
}
else if  (sensorValue < 500) {
Gear_Position = 3;
}
else if  (sensorValue < 580) {
Gear_Position = 4;
}
else {
Gear_Position = 5;
}
``````

And if the increment between the ADC readings is even it may be as simple as (numbers probably need to be tweaked)

``````Gear_Position = (sensorValue - 200) / 100;
``````

…R