Measure rotation of bike's handlebar

Hi! I am developing a project where there is a game and it is controlled by the real bike through the use of sensors. I am using a potentiometer for the handlebar but when turning the handlebar, the output in the game is not exact on the turn in the bike's handlebar.

I don't know if the sensor I used is the problem or the code below. Please help.
Is there any other sensor as alternative to this?

int sensorVal = 0;
int smoothedVal= 0;
int samples = 5;

handlerot = analogRead(pot1);
pot1_val = map(handlerot, 0, 1023, -20, 20);
smoothedVal = smoothedVal + ((pot1_val - smoothedVal) / samples);
delay(100);

The code snippet isn't enough to know whether the smoothing is correct. Post all of your code.

Pete

Here is the code for reading potentiometer and send it to the pc via Serial.

//---variables for potentiometer---//
#define pot1 A1  //handlebar
int handlerot, pot1_val = 0;

int sensorVal = 0;
int smoothedVal= 0;
int samples = 5;


void setup(){
  
  Serial.begin(9600);

}

void potValue()
{  
  handlerot = analogRead(pot1);
  pot1_val = map(handlerot, 0, 1023, -20, 20);
  smoothedVal = smoothedVal + ((pot1_val - smoothedVal) / samples);
  delay(100);
}

void showValue()
{
  potValue();
    Serial.print(smoothedVal);
    Serial.println();

}

void loop()
{ 
  showValue();  
}

The attached picture is the serial monitor showing that the value is smooth.

pot value.jpg

The attached picture is the serial monitor showing that the value is smooth.

Then it would seem that the problem is the other application that you are sending the data to.

Try replacing

handlerot = analogRead(pot1);
pot1_val = map(handlerot, 0, 1023, -20, 20);
smoothedVal = smoothedVal + ((pot1_val - smoothedVal) / samples);

with

smoothedVal = averageReading(pot1);

and add this function

int averageReading(int pin)
{
int tot=0;
int n;
for(n=0;n<32;n++)
  tot=tot + analogRead(pin);
tot=tot/32;
return map(tot,0,1023,-20,20);
}

If I use sensors like gyro or accelerometer, Is there a possibility to make the turn more accurate?
I will just use one axis (Y-axis).

KenF:
Try replacing

handlerot = analogRead(pot1);

pot1_val = map(handlerot, 0, 1023, -20, 20);
smoothedVal = smoothedVal + ((pot1_val - smoothedVal) / samples);




with


smoothedVal = averageReading(pot1);




and add this function


int averageReading(int pin)
{
int tot=0;
int n;
for(n=0;n<32;n++)
  tot=tot + analogRead(pin);
tot=tot/32;
return map(tot,0,1023,-20,20);
}

I will try this later and will update you if it solve the problem. Thanks

Hi,
Is the pot linear or log?? in other words does it show even resistance along all the track?, if the pot wiper is in the centre postion then does that equal 1/2 the resistance? If it's log as used for volume control then the track will not be at all even, the first half of the turn may show little resistance where as the last quarter will show most!!

You can easy test this with a DMM... or a simple bit of code to sent pot_val to serial monitor, etc. Just check the reading for centre position, it should be about 5K, most pots rotate about 270 deg.

Hope it helps, regards.

Mel.

How much of the pot's full rotation range actually is used?

Hi, B10K in this country is a 10K linear.
What does the PC program expect the turn signal to be coming from, what was the original sensor?

Tom..... :slight_smile:

I don't know what is the range of rotation when turning the potentio.
But I think it is 270deg.

When testing the potentiometer. I got an output value ranging from -7 deg to +7 deg which is right for controlling the game.
When attaching the pot to the bike. I calibrate it and get the center.
But when testing it, I cannot properly control the game. Maybe the problem is when attaching it to the bike. It is not properly attached.

Can I use a gyro for this application?

But when testing it, I cannot properly control the game. Maybe the problem is when attaching it to the bike. It is not properly attached.

Have you made close observations to see if there is a difference of how the pot is rotated on the bike vs during your off bike testing? How do the on bike and off bike pot outputs differ?

Hi, how have you got the pot connected to the handle bars?
It looks like a belt, is it a toothed belt to keep in sync.
After you have turned the bars from straight to left and back, is the program still showing straight ahead after you return to straight?

The other thing is, has the program got the speed to keep up with the movements?
When you turn from centre to say left quickly, does the program follow as quickly?
Move the bars very slowly and see if it keeps up.
The pot may also be noisy, is it a new pot, you may need to put a 0.1uF capacitor from the centre wiper of the pot to the neg or gnd end of the pot.

Tom...... :slight_smile:

Here is the video while playing the game.

I'd say the program cannot keep up in real time with the real bike handles.
Or you have problems with the 1/2 second delay as you average the input.

You are taking 5 samples then outputting the result, that 5 times 100mS = 0.5Sec between position updates.

Have you tried 10 instead of 100 delay?

Tom... :slight_smile:

Oh! That might be the reason why it is not controlled in real time. I'll try this. Thanks :slight_smile:

Hey! How about using a combination of accelerometer and magnetometer? You can compute the heading by just integrating those two sensors.

Yes, I am planning to use more accurate sensor than potentiometer.
BTW, thanks for your suggestion.
Please explain further information about you're saying.

tacticz03:
I will try this later and will update you if it solve the problem. Thanks

Did you try it?

To compute for the heading using those sensors, you need to use some trigonometry. The output from the accelerometer is always based on earth's gravitational pull.