Need Help With A Switch Please

Hi All,
I would consider myself an intermediate beginner with the Arduino code, I have been working on this issue for several days now and can’t get past it. Here is what I am building for my son.

He plays a game called American Truck Simulator and i have been building a button box that has a 3 wire ignition switch in it. Here is how i have it wired to the Leonardo.
Battery - Ground
Run - Pin 2
Start - Pin 3
It works fine when you turn the key on the truck dash powers up like it should, you turn it to start and the engine starts up and runs like it should, the problem is when you go to shut the truck off you turn the key to off and the power stays on and the truck stays running, If you turn the key back to run then to off again then it shuts down like it should.
I have searched forums and the internet for answers and examples but have found nothing

Attached is my ino in text form, any help would be appreciated I’m trying to get this done for my son’s Christmas

// modified sketch
// by Barry Wingler
// 2020-11-20

#include <Joystick.h>

Joystick_ Joystick;

int button = 2;
int status = false;

void setup() {
  // Initialize Button Pins
  // for digitalRead pins A0-A5 = 18-23
  pinMode(button, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  //  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[19] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 18, 19, 20, 21, 22};

int lastButtonState[19] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void loop() {

  for (int index = 0; index < 19; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  if (digitalRead(button) == true) {
    status = !status;
  } 
  delay(50);
}

Please post your code, in code tags.

Have you got a pullup or pulldown resistor in place to keep the input at a known state at all times or is it floating at an unknown, perhaps HIGH, perhaps LOW, perhaps changing level ?

no i am using the internal resistors of the board

Please read the sticky post at the top of the forum and learn how to properly post your code using code tags. It will help people help you. Then, it looks like this

// modified sketch
// by Barry Wingler
// 2020-11-20

#include <Joystick.h>

Joystick_ Joystick;

int button = 2;
int status = false;

void setup() {
  // Initialize Button Pins
  // for digitalRead pins A0-A5 = 18-23
  pinMode(button, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  //  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[19] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 18, 19, 20, 21, 22};

int lastButtonState[19] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void loop() {

  for (int index = 0; index < 19; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  if (digitalRead(button) == true) {
    status = !status;
  } while (digitalRead(button) == true);
  delay(50);
}

And people can see it without downloading it. If button 2 is your run button, defined as INPUT_PULLUP, your while() statement will block until you turn on the truck (button 2 == LOW). Then you turn it to start which makes it block again. When you turn it off, it makes it block yet again which may or may not be after your button processing has happened. You really don’t want to be blocking using the while() loop.

// modified sketch
// by Barry Wingler
// 2020-11-20

#include <Joystick.h>

Joystick_ Joystick;

int button = 2;
int status = false;

void setup() {
  // Initialize Button Pins
  // for digitalRead pins A0-A5 = 18-23
  pinMode(button, INPUT_PULLUP);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  //  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// defining the total [#] of buttons and their pins
const int ButtonToPinMap[19] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 18, 19, 20, 21, 22};

int lastButtonState[19] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void loop() {

  for (int index = 0; index < 19; index++)
  {
    int currentButtonState = !digitalRead(ButtonToPinMap[index]);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  if (digitalRead(button) == true) {
    status = !status;
  } 
  delay(50);
}

I removed the while statement and it stile has the same issue as before, The switch maps fine in the game controls, just can’t understand why it is doing this.
A momentary push button turns on the electric and starts the truck and when you push it again it turns it off, i don’t know if that will shed any light on the matter or not.

If i am thinking of the 3 wire ignition switch right would it be a on/off toggle switch with a momentary switch?

I ditched the Arduino firmware and flashed the firmware to mmjoy2, so much easier to work with on setting up switches, encoders and everything. Now the ignition works like it should, took 2 minutes to setup. No need for this forum anymore.

bwingler:
No need for this forum anymore.

Maybe you could contact the admins and ask them to shut it down.