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?
Thanks in advance
-------------------(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----------------------
int sensorValue = analogRead(A0);
// 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();