Close

Retarder Handle Part 2

A project log for Real Scania Truck Home Simulator

The Scrapyard and Arduino make a great combination!

jeroen-van-der-veldenJeroen van der Velden 12/11/2015 at 13:260 Comments

Still working on the Retarder Lever, this code is driving me crazy!!!

I made slide 1 below to clear things up for you a bit. My Retarder handle is basically one big Potentiometer (5V, GND and A0). To Simulate this lever with Arduino, I made a breadboard with 6 Potentiometers (slide 2) on it. The pot`s are set to my levers resistance values *** to test my code. Although my lever has only 5 positions (1-5), there is also position 0 so I need 6 potentiometers on my breadboard.

Values***

With 5v connected to PIN 2 and 6 (PIN 2 +) (PIN 6 -):

Pos 0 = 0.53v
Pos 1 = 1.30v
Pos 2 = 2.12v
Pos 3 = 2.94v
Pos 4 = 3.69v
Pos 5 = 4.37v

Without 5v connected to PIN 2 and 6 (Resistance measured between PIN 7 and 2)

Pos 0 = no reading or infinite 4k+
Pos 1 = 1207 ohm
Pos 2 = 582 ohm
Pos 3 = 300 ohm
Pos 4 = 156 ohm
Pos 5 = 052 ohm

So far my code looks like:

byte handlePos;
int val = analogRead(0);

if(val < 100) handlePos = 0;
if(val < 187) handlePos = 1;
if(val < 350) handlePos = 2;
if(val < 518) handlePos = 3;
if(val < 679) handlePos = 4;
if(val < 861) handlePos = 5;

VAL (handle value) explained:


To determine val:

val = volts x 1023 / 5 = :


So:

Pos 0 = 0.53v * 1023 / 5 = 108 val

Pos 1 = 1.30v * 1023 / 5 = 266 val

Pos 2 = 2.12v * 1023 / 5 = 433 val

Pos 3 = 2.94v * 1023 / 5 = 602 val

Pos 4 = 3.69v * 1023 / 5 = 755 val

Pos 5 = 4.37v * 1023 / 5 = 967 val


So:

Pos 0 = 108
187
Pos 1 = 266
350
Pos 2 = 433
518
Pos 3 = 602
679
Pos 4 = 755
861
Pos 5 = 967

Take averages (not sure if needed by now):

Pos 0 no val (infinite ohms or zero, needs further investigation)

Pos 1 (266 - 108) / 2 + 108 = 187

Pos 2 (433 - 266) / 2 + 266 = 350

Pos 3 (602 - 443) / 2 + 443 = 518

Pos 4 (755 - 602) / 2 + 602 = 679

Pos 5 (967 - 755) / 2 + 755 = 861


Now the handlePos`s are stored (byte) in the Arduino, I need to find code to:

When the lever goes DOWN one position, I need one HID keyboardPrint("+");

When the lever goes DOWN two positions, I need two HID keyboardPrint("+");`s

When the lever goes DOWN three positions, I need three HID keyboardPrint("+");`s

When the lever goes DOWN four positions, I need four HID keyboardPrint("+");`s

When the lever goes DOWN five positions, I need five HID keyboardPrint("+");`s

When the lever goes UP one position, I need one HID keyboardPrint("-")

When the lever goes UP two positions, I need two HID keyboardPrint("-");`s

When the lever goes UP three positions, I need three HID keyboardPrint("-");`s

When the lever goes UP four positions, I need four HID keyboardPrint("-");`s

When the lever goes UP five positions, I need five HID keyboardPrint("-");`s

When the lever is in position 3, and I move the lever to position five, I need two HID keyboardPrint("+");

Slide 1

Slide 2

More details in Retarder Handle Part 3!

Discussions