# Gear indicator problem

**URL:** <https://forum.arduino.cc/t/gear-indicator-problem/377683>\
**Category:** Programming\
**Created:** [April 6, 2016, 10:36am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683 "2016-04-06T10:36:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 6, 2016, 10:36am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/1 "2016-04-06T10:36:13Z")

</div>

Hello to forum,this is my first project ,i have a problem and i cant find the solution,i am trying to put a gear indicator to a motorcycle v strom,the good thing is that has GPS(gear position sensor)with a multimeter i read the voltage to the gears,so for 1= 1,44..2=1,83..3=2,56...4=3,29..5=4,14..6=4,60..N=5v i try to simulate it with a potensiometer,the parts i am using is the arduino nano,a seven segment display anode and a74hc595 shift,the voltage range to the pot is from 0-4,78v iam using the math eg.vout=1,44×1024/5=294,912 i think this is wrong,because when i write the code i have no problem to upload it but the display when i turn the pot nothings hapens,how can i found the right vout;i mean the 0-1024 how can i calculate it; thanks to all and i am sory for my english

Int latch pin=A1;  
Int data pin=A2;  
Int clockn pin=A3;  
Const in analog pin=A4;  
Int sensor value=0-1024  
Void loop(){  
Sensor value=analog read=A4;

0//  
If sensor value (\<294,912)  
.......

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 6, 2016, 10:45am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/2 "2016-04-06T10:45:25Z")

</div>

```cpp
Sensor value=analog read=A4;

```

That is not how to use analogRead()

Please post your complete program using code tags. If you don't know what they are then you did not [read this before posting a programming question](http://forum.arduino.cc/index.php?topic=97455.0)

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 6, 2016, 11:38am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/3 "2016-04-06T11:38:56Z")

</div>

Hi,  
This may help with using analog inputs.

> **[Read Analog Voltage | Arduino Documentation](https://docs.arduino.cc/built-in-examples/basics/ReadAnalogVoltage)**
>
> Reads an analog input and prints the voltage to the Serial Monitor.

Tom.. 🙂

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 6, 2016, 1:18pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/4 "2016-04-06T13:18:01Z")

</div>

> TomGeorge:  
> Hi,  
> This may help with using analog inputs.
> 
> [http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage](http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage)
> 
> Tom.. 🙂

> UKHeliBob:
> 
> ```arduino
> Sensor value=analog read=A4;
> 
> ```
> 
> That is not how to use analogRead()
> 
> Please post your complete program using code tags. If you don't know what they are then you did not [read this before posting a programming question](http://forum.arduino.cc/index.php?topic=97455.0)

Ok i am trying to understand all this information but i am not a programmer i am electrician and this is little dificult for me,thank you

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 6, 2016, 9:32pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/5 "2016-04-06T21:32:47Z")

</div>

Hi,  
The link I posted shows how to measure voltage, in your case 0 to 5V will be okay directly to the anaol input.  
The example sketch shows you, if you ready the comments in the sketch, how to read and store a value from the analog input.

Tom.... 🙂

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 9:02am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/6 "2016-04-09T09:02:29Z")

</div>

hello again,i build a code but i still have problem,i wrote a code only for two gears ,i turn the pot but nothing happens

int latchPin = A1;//5 595  
int dataPin =A2;//3 595  
int clockPin =A3;//6 595  
int sensorValue = analogRead(A4);

void setup(){  
pinMode(latchPin, OUTPUT);  
pinMode(dataPin, OUTPUT);  
pinMode(clockPin, OUTPUT);

pinMode(A4, INPUT);  
}  
void loop(){  
int sensorValue = analogRead(A4);  
float voltage = sensorValue \* (5.0 / 1023.0);

if (sensorValue \<500)  
{

digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 18);  
digitalWrite(latchPin,HIGH);

}  
else if (sensorValue \<700)  
{

digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 25);  
digitalWrite(latchPin,HIGH);

} }

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 9, 2016, 9:20am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/7 "2016-04-09T09:20:33Z")

</div>

How is the pot wired ?  
What do you see if you print sensorValue and voltage ?  
Can you please remind me what sort of display the shift register is driving ?

---

<div class="post-metadata">

**Author:** ![anilkunchalaece](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anilkunchalaece](https://forum.arduino.cc/u/anilkunchalaece)\
**Post date:** [April 9, 2016, 9:33am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/8 "2016-04-09T09:33:28Z")

</div>

```cpp
int latchPin = A1;//5 595
int dataPin =A2;//3 595
int clockPin =A3;//6 595
int sensorValue = analogRead(A4);

```

calling `analogRead(A4)` is useless.. here.. Just leave it as `int sensorValue = 0`

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 9:35am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/9 "2016-04-09T09:35:19Z")

</div>

the pot is 10k and the voltage is from 0-4,57v  
the midle pin on the pot is the output and goes to A4 and the other two are  
5v and ground  
the display is a seven segment anode

---

<div class="post-metadata">

**Author:** ![MalharD](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MalharD](https://forum.arduino.cc/u/MalharD)\
**Post date:** [April 9, 2016, 9:38am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/10 "2016-04-09T09:38:43Z")

</div>

Can you post a photo of your circuit ?  
The code is correct,I added some bits to it. Use Serial.println() to get debug data on your pc.

```cpp
int latchPin = A1;//5 595
int dataPin = A2; //3 595
int clockPin = A3; //6 595
int sensorValue = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(A4, INPUT);
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(A4);
  float voltage = sensorValue * (5.0 / 1023.0);
  
  if  (sensorValue <= 340 )
  { Serial.println("Gear 1");
    //The 74HC595 Stuff
  }
    else if  (sensorValue > 340 && sensorValue <= 400)
    { Serial.println("Gear 2");
      //The 74HC595 Stuff
    }
    else if  (sensorValue > 400 && sensorValue <= 600)
    { Serial.println("Gear 3");
      //The 74HC595 Stuff
    }
    else if  (sensorValue > 600 && sensorValue <= 720)
    { Serial.println("Gear 4");
      //The 74HC595 Stuff
    }
    else if  (sensorValue > 720 && sensorValue <= 880)
    { Serial.println("Gear 5");
      //The 74HC595 Stuff
    }
    else if  (sensorValue > 880 && sensorValue <= 1000)
    { Serial.println("Gear 6");
      //The 74HC595 Stuff
    }
  }

```

---

<div class="post-metadata">

**Author:** ![anilkunchalaece](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@anilkunchalaece](https://forum.arduino.cc/u/anilkunchalaece)\
**Post date:** [April 9, 2016, 9:43am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/11 "2016-04-09T09:43:22Z")

</div>

```cpp
shiftOut(dataPin,clockPin,MSBFIRST, 25);
shiftOut(dataPin,clockPin,MSBFIRST, 18);

```

it is a byte shift.. so whats the maximum value it can shift?

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 9:57am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/12 "2016-04-09T09:57:01Z")

</div>

> MalharD:  
> Can you post a photo of your circuit ?  
> The code is correct,I added some bits to it. Use Serial.println() to get debug data on your pc.
> 
> ```arduino
> int latchPin = A1;//5 595
> 
> ```

int dataPin = A2; //3 595  
int clockPin = A3; //6 595  
int sensorValue = 0;

void setup() {  
&nbsp; pinMode(latchPin, OUTPUT);  
&nbsp; pinMode(dataPin, OUTPUT);  
&nbsp; pinMode(clockPin, OUTPUT);  
&nbsp; pinMode(A4, INPUT);  
&nbsp; Serial.begin(9600);  
}  
void loop() {  
&nbsp; int sensorValue = analogRead(A4);  
&nbsp; float voltage = sensorValue \* (5.0 / 1023.0);  
&nbsp;  
&nbsp; if&nbsp; (sensorValue \<= 340 )  
&nbsp; { Serial.println("Gear 1");  
&nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; }  
&nbsp; &nbsp; else if&nbsp; (sensorValue \> 340 && sensorValue \<= 400)  
&nbsp; &nbsp; { Serial.println("Gear 2");  
&nbsp; &nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; &nbsp; }  
&nbsp; &nbsp; else if&nbsp; (sensorValue \> 400 && sensorValue \<= 600)  
&nbsp; &nbsp; { Serial.println("Gear 3");  
&nbsp; &nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; &nbsp; }  
&nbsp; &nbsp; else if&nbsp; (sensorValue \> 600 && sensorValue \<= 720)  
&nbsp; &nbsp; { Serial.println("Gear 4");  
&nbsp; &nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; &nbsp; }  
&nbsp; &nbsp; else if&nbsp; (sensorValue \> 720 && sensorValue \<= 880)  
&nbsp; &nbsp; { Serial.println("Gear 5");  
&nbsp; &nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; &nbsp; }  
&nbsp; &nbsp; else if&nbsp; (sensorValue \> 880 && sensorValue \<= 1000)  
&nbsp; &nbsp; { Serial.println("Gear 6");  
&nbsp; &nbsp; &nbsp; //The 74HC595 Stuff  
&nbsp; &nbsp; }  
&nbsp; }

> ```arduino
> 
> ```

the code is correct i see to serial monitor from one to six ,and now i must replace the serial.printIn with digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 18);  
digitalWrite(latchPin,HIGH);

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 10:15am UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/13 "2016-04-09T10:15:06Z")

</div>

> anilkunchalaece:
> 
> ```arduino
> shiftOut(dataPin,clockPin,MSBFIRST, 25);
> 
> ```

shiftOut(dataPin,clockPin,MSBFIRST, 18);

> ```arduino
> 
> it is a byte shift.. so whats the maximum value it can shift?
> 
> ```

64,171,36,48,25,18,2

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 12:10pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/14 "2016-04-09T12:10:37Z")

</div>

can you tell me prease how you find this?? sensorValue \> 720 && sensorValue \<= 880 this is my problem

thanks for reply to all,this forum is the best and all of you also...

---

<div class="post-metadata">

**Author:** ![MalharD](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MalharD](https://forum.arduino.cc/u/MalharD)\
**Post date:** [April 9, 2016, 12:22pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/15 "2016-04-09T12:22:13Z")

</div>

> Miron27:  
> can you tell me prease how you find this?? sensorValue \> 720 && sensorValue \<= 880 this is my problem
> 
> thanks for reply to all,this forum is the best and all of you also...

Please explain. To find what ?  
The thresholds ?  
I found that using the voltage values provided by you.

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 12:26pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/16 "2016-04-09T12:26:55Z")

</div>

> MalharD:  
> Please explain. To find what ?  
> The thresholds ?  
> I found that using the voltage values provided by you.

what exactly math are you doing?because i make this one and i cant find 340

1,44\*(5.0/1023)=1,44\*0,0044=0,00636

---

<div class="post-metadata">

**Author:** ![MalharD](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@MalharD](https://forum.arduino.cc/u/MalharD)\
**Post date:** [April 9, 2016, 1:01pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/17 "2016-04-09T13:01:16Z")

</div>

Okay,look

1=\> 1,44 \* (1023/5)=294  
2=\> 1,83 \* (1023/5)=374  
3=\> 2,56 \* (1023/5)=523  
4=\> 3,29 \* (1023/5)=673  
5=\> 4,14 \* (1023/5)=847  
6=\> 4,60 \* (1023/5)=941

The GPS shoud indicate gear 1 if the value is nearly equal to 1,44v or 294 read by analogRead()

So I decided a lower limit approximately, below which the value of analogRead() cannot go and I decoded a upper limit above which the value of analogRead() cannot go.

So the if statements work like-  
If the value is above lower limit AND (&&-The Logical AND operator) below the upper limit,print the specific gear.

If you still can't get it,please ask again.

Cheers,  
-Malhar

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 1:14pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/18 "2016-04-09T13:14:09Z")

</div>

> MalharD:  
> Okay,look
> 
> 1=\> 1,44 \* (1023/5)=294  
> 2=\> 1,83 \* (1023/5)=374  
> 3=\> 2,56 \* (1023/5)=523  
> 4=\> 3,29 \* (1023/5)=673  
> 5=\> 4,14 \* (1023/5)=847  
> 6=\> 4,60 \* (1023/5)=941
> 
> The GPS shoud indicate gear 1 if the value is nearly equal to 1,44v or 294 read by analogRead()
> 
> So I decided a lower limit approximately, below which the value of analogRead() cannot go and I decoded a upper limit above which the value of analogRead() cannot go.
> 
> So the if statements work like-  
> If the value is above lower limit AND (&&-The Logical AND operator) below the upper limit,print the specific gear.
> 
> If you still can't get it,please ask again.
> 
> Cheers,  
> -Malhar

i found the problem before five seconts before you reply,and this the final code

int latchPin = A1;//5 595  
int dataPin = A2; //3 595  
int clockPin = A3; //6 595  
int sensorValue = 0;

void setup() {  
pinMode(latchPin, OUTPUT);  
pinMode(dataPin, OUTPUT);  
pinMode(clockPin, OUTPUT);  
pinMode(A4, INPUT);

}  
void loop() {  
int sensorValue = analogRead(A4);  
float voltage = sensorValue \* (5.0 / 1023.0);

//1  
if (sensorValue \<= 200 )  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 121);  
digitalWrite(latchPin,HIGH);

}  
//2  
else if (sensorValue \> 300 && sensorValue \<= 375)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 36);  
digitalWrite(latchPin,HIGH);  
}  
//3  
else if (sensorValue \> 500 && sensorValue \<= 522)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 48);  
digitalWrite(latchPin,HIGH);  
}  
//4  
else if (sensorValue \> 600 && sensorValue \<= 671)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 25);  
digitalWrite(latchPin,HIGH);  
}  
//5  
else if (sensorValue \> 800 && sensorValue \<= 844)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 18);  
digitalWrite(latchPin,HIGH);  
}  
//6  
else if (sensorValue \> 900 && sensorValue \<= 950)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 2);  
digitalWrite(latchPin,HIGH);  
}  
//N  
else if (sensorValue \> 950 && sensorValue \<= 1020)  
{ digitalWrite(latchPin,LOW);  
shiftOut(dataPin,clockPin,MSBFIRST, 63);  
digitalWrite(latchPin,HIGH);  
}  
}

so if anyone have a vstrome and he want to put a gear indicator using the motorcycle GPS this is it..thanks all of you for replys......

and something last how can i make the N in the seven segment to light random all led exept the center?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 9, 2016, 1:19pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/19 "2016-04-09T13:19:20Z")

</div>

Hi,  
Good to hear you have got it working, now...  
Can you please post a copy of your sketch, using code tags?  
They are made with the \</\> icon in the reply Menu.  
See section 7 [http://forum.arduino.cc/index.php/topic,148850.0.html](http://forum.arduino.cc/index.php/topic,148850.0.html)

Thanks, it will make it easier to read. Tom.. 🙂

---

<div class="post-metadata">

**Author:** ![Miron27](https://avatars.discourse-cdn.com/v4/letter/m/c37758/32.png) [@Miron27](https://forum.arduino.cc/u/Miron27)\
**Post date:** [April 9, 2016, 1:21pm UTC](https://forum.arduino.cc/t/gear-indicator-problem/377683/20 "2016-04-09T13:21:44Z")

</div>

```cpp
int latchPin = A1;//5 595
int dataPin = A2; //3 595
int clockPin = A3; //6 595
int sensorValue = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(A4, INPUT);
  
}
void loop() {
  int sensorValue = analogRead(A4);
  float voltage = sensorValue * (5.0 / 1023.0);

  //1
  if  (sensorValue <= 200 )
  { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 121);
  digitalWrite(latchPin,HIGH);

  }
  //2
  else if  (sensorValue > 300 && sensorValue <= 375)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 36);
  digitalWrite(latchPin,HIGH);
    } 
    //3
    else if  (sensorValue > 500 && sensorValue <= 522)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 48);
  digitalWrite(latchPin,HIGH);
    }
    //4
    else if  (sensorValue > 600 && sensorValue <= 671)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 25);
  digitalWrite(latchPin,HIGH);
    }
    //5
    else if  (sensorValue > 800 && sensorValue <= 844)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 18);
  digitalWrite(latchPin,HIGH);
    }
    //6
    else if  (sensorValue > 900 && sensorValue <= 950)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 2);
  digitalWrite(latchPin,HIGH);
    }
    //N
    else if  (sensorValue > 1000 && sensorValue <= 1020)
    { digitalWrite(latchPin,LOW);
  shiftOut(dataPin,clockPin,MSBFIRST, 63);
  digitalWrite(latchPin,HIGH);
    }
  }

```

[Next page](https://forum.arduino.cc/t/gear-indicator-problem/377683.md?page=2)
