DISCLAIMER: I'm a noob in programming, I don't know much, so don't blame me please
I found a code on the internet, which lets my Arduino Mega communicate with a simulator, and it emulates an already existing controller in order to be able to create your own, custom controller.
Its a train simulator game, and I basically want to emulate a lever. I want to move the lever in-game with the potentiometer. The lever has 3 main positions. 'Max Brake', 'Off', and 'Max Throttle'. In this order.
So I'd have to be in the 'Max Brake' position when the analog input reads 0, 'Off' when it reads 127.5 and 'Max Throttle' when it reads 255.
The problem is, the emulation starts off at the 'Off' position for some reason. So the 0 analog vallue corresponds to the 'Off' position, and as I increase the vallue, it goes from Off to Max Throttle (127.5), and it loops back to Max Brake and back ot Off (255). Its super weird.
This isn't everything. When I reduce the range of the Map Vallue (The last vallue in the map(Val[1] operator which is currently 255) to 127.5, the alligment works as it should. Its in the Off position when the Potmeter is dead center. The problem is, I only get half the range, so I cannot actually reach the Max Throttle and Max Brake positions because I halved my potmeter's output.
If I increase my max vallue even a little, it immediately goes out of whach and it loses it's alligment again. (So the Off position isn't in the middle of the potmeter anymore)
I'd be greatful if someone could give me any tips which vallues to change. I tried experimenting, and I made some progress, but I wasn't able to get any meaningful results and I'm completely out of ideas at this point.
If something doesn't make sense please let me know! Thank you in advance!
Here is the full code:
The potmeter in question is on the A1 analog pin
//int analogPin0 = A0; // potentiometer wiper (middle terminal) connected to analog pin
int analogPin1 = A1;
int analogPin2 = A2; // Uncomment as needed. I was testing only a few channels at a time
int analogPin3 = A3;
int analogPin4 = A4;
int analogPin5 = A5;
int cycle = 0;
int dir = 1;
int delayTime = 5;
int val[13] = {0}; // variable to store the value read
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop()
{
val[0] = analogRead(analogPin1);
val[0] = cycle;
val[0] = map(val[0], 0, 1023, 0, 255); // I had to remap for how my pot was setup default is 0, 1023, 255, 0
val[0] = val[0] * 4; //4 // Was trying to reduce number of steps here
if (val[0] == 256){val[0] = 255;}
cycle += dir;
if (cycle == 0)
{
dir = 1;
}
else if (cycle == 255)
{
dir = -1;
}
delay(delayTime);
val[1] = cycle;
val[1] = analogRead(analogPin1);
val[1] = map(val[1], 0, 388, 135, 230); //0, 388, 135, 230
val[1] = map(val[1], 0, 1023, 0, 255); //0, 1023, 0, 255 //127.5 gets interesting results
val[1] = val[1] * 4; //4
if (val[1] == 256){val[1] = 254;}
delay(delayTime);
val[2] = cycle;
val[2] = analogRead(analogPin2);
val[2] = map(val[2], 0, 388, 1, 64);
val[2] = val[2] * 4;
if (val[2] == 256){val[2] = 254;}
delay(delayTime);
val[3] = cycle;
val[3] = analogRead(analogPin3);
val[3] = map(val[3], 0, 388, 1, 64);
val[3] = val[3] * 4;
if (val[3] == 256){val[3] = 254;}
delay(delayTime);
val[4] = cycle;
val[4] = analogRead(analogPin4);
val[4] = map(val[4], 0, 388, 1, 64);
val[4] = val[4] * 4;
if (val[4] == 256){val[4] = 254;}
delay(delayTime);
val[5] = cycle;
val[5] = analogRead(analogPin5);
val[5] = map(val[5], 0, 388, 1, 64);
val[5] = val[5] * 4;
if (val[5] == 256){val[5] = 254;}
delay(delayTime);
val[6] = cycle;
delay(delayTime);
// If you require all 7 analog channels you will need to add in the extra one here
// Start of string output
Serial.print("Output: ");
// Padding for numbers
for(int i = 0; i < 13; i++)
{
if(val[i] < 10)
{
Serial.print(" ");
}
else if(val[i] < 100)
{
Serial.print(" ");
}
Serial.print(val[i]);
Serial.print(" ");
}
Serial.println("");
}
The original GitHub link: GitHub - skaako/raildriver: Custom Raildriver DLL for using custom hardware with Train Sim World 2