Gear indicator problem

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)

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

Hi, This may help with using analog inputs.

http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage

Tom.. :)

TomGeorge: Hi, This may help with using analog inputs.

http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage

Tom.. :)

UKHeliBob: 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

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

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.... :)

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);

} }

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 ?

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

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

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.

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
    }
  }
shiftOut(dataPin,clockPin,MSBFIRST, 25);
shiftOut(dataPin,clockPin,MSBFIRST, 18);

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

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.

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
    }
  }

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);

anilkunchalaece: shiftOut(dataPin,clockPin,MSBFIRST, 25); shiftOut(dataPin,clockPin,MSBFIRST, 18);

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

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

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…

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.

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

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

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?

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

Thanks, it will make it easier to read. Tom… :slight_smile:

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);
    }
  }