Hi guys, Ive been working on a simple gear indicator for a manual H pattern type gearbox.
It uses 2 potentiometers to provide the Arduino with 0-5v on 2 of the analouge pins, to measure the x and y axis of the gear lever. The approprate digit is then displayed on a 7 segment.
Everything was going smoothly until I added the code for reverse, and now it displays r in both reverse and neutral (neutral should display n)
Its probably just a simple mistake somewhere, but i cant seem to figure it out.
//Gear indicator for 5 speed H pattern transmission (reverse next to 4th)
//Only 2 inputs needed!
//1 poti for x axis (left right movement)
//1 poti for y axis movement (up down (or forward backward :s ))
int a = 2; //For displaying segment "a"
int b = 3; //For displaying segment "b"
int c = 4; //For displaying segment "c"
int d = 5; //For displaying segment "d"
int e = 6; //For displaying segment "e"
int f = 7; //For displaying segment "f"
int g = 8; //For displaying segment "g"
int unsigned n; //neutral
int unsigned r; //reverse
// The following values are altered for calibration
const unsigned int Up = 3;//voltage Y Axis between 3rd and neutral gearstick position
const unsigned int Down = 2;//Vlotage Y Axis between 4th and neutral gearstick position
const unsigned int Left = 2;//voltage X Axis between 1st and 3rd gearstick position
const unsigned int Right = 3;//Voltage x Axis between 3rd and 5th gearstick position
void setup()
{
pinMode(A0, INPUT); //x axis input
pinMode(A1, INPUT); //y axis input
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G
}
//Segment combinations for displaying each gear
void displayGear(int gear)
{
if(gear !=1 && gear !=4 && gear !=n && gear !=r)
digitalWrite(a,LOW);
if(gear !=5 && gear !=n && gear !=r)
digitalWrite(b,LOW);
if(gear !=2 && gear !=r)
digitalWrite(c,LOW);
if(gear !=1 && gear!=4 && gear !=n && gear !=r)
digitalWrite(d,LOW);
if(gear!=1 && gear!=3 && gear!=4 && gear!=5)
digitalWrite(e,LOW);
if(gear !=1 && gear !=2 && gear!=3 && gear !=n && gear !=r)
digitalWrite(f,LOW);
if (gear !=1)
digitalWrite(g,LOW);
}
//to turn unused segments off
void turnOff()
{
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
void loop(){
// read the input on analog pin 0:
int xAxis = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float xPos = xAxis * (5.0 / 1023.0);
// repeat for y axis
int yAxis = analogRead(A1);
float yPos = yAxis * (5.0 / 1023.0);
if (yPos < Up && yPos > Down){
//Neutral
displayGear(n);
}
else if (xPos < Left && yPos > Up){
//1st gear
displayGear(1);
}
else if (xPos < Left && yPos < Down){
//2nd gear
displayGear(2);
}
else if (xPos > Left && xPos < Right && yPos > Up){
//3rd gear
displayGear(3);
}
else if (xPos > Left && xPos < Right && yPos < Down){
//4th gear
displayGear(4);
}
else if (xPos > Right && yPos > Up){
//5th gear
displayGear(5);
}
else if (xPos > Right && yPos < Down){
//Reverse gear
displayGear(r);
}
//Turn unused segments off
turnOff();
}
Im new to this and this is only my second Arduino project. Please let me know if I have done anything wrong with the code, if the layout is correct, and if there are any improvements that could be made ect.