Coding an ignition switch

I took a quick video of how that code works with joy.cpl

I turn the ignition from off-run-ign-run-off. It might be a little hard to see with the focus, but on the screen, you should be able to see the "1" light up red when IGN on, stay red when in RUN, and then turn off when switch is turned to OFF. This is exactly what I want, but the game does not recognize. If you would notice something wrong in the code that would explain why the game does not like it, please let me know, I feel like this is so close to complete.

I hope the link works for you to see the video

I can't log in to see a video. And any reasoning I do here is with total ignorance of the joystick library.

But it seems that what you need to do is make a button look like it gets pressed, then released, when your ignition OFF part contacts open up.

So I think your code is doing what you think, but not what needs to be done.

The '1' circle is the indicator for a pushbutton. It would be on when the button is pressed, and off when not pressed.

This is pure, or rank, speculation, so I hope some joystick/button box heavies are following along, but I think this is what you need to do to fake a button press:

    if (buttonState1 == HIGH) {  // **turn the IGN on**
           Joystick.setButton(0, 1);  // "press" button 0

// delay needed here? I hope not, but it will otherwise appear as a very short stab

           Joystick.setButton(0, 0);  // "release" button 0
    } 

As you turn the ignition switch from 0 to 1 to 2 to 1 to 0, there are four fully detectable state transitions. Any of them that needs to be turned into a faked button press can just execute code that presses, then releases an appropriate "pushbutton".

A similar problem on these fora from some time ago was how to turn a toggle switch into an on/off pushbutton signal, and something like this was the solution. I'll try to find it.

Now that raised an interesting problem, as the code has no way of knowing the state of the game. So it has to assume that when it starts, it can be assured that the engine in the game is off.

Kinda like how your TV goes off or on when you hit the power button. If you've ever gotten two things meant to obey that one power change command where one is off, and one is on, you see the problem. Toggling the power just turns one on and the other off, there is no "turn on" command, only a toggle.

OK, this post the entire thread, and a linked thread


HTH

Oh, there's a link to a simulation I wrote that is broken, it may be that I deleted a project not realizing it was supposed to last forever...

a7

yes, I agree with you that there are 4 changes in state, but the ones that are not circled in your picture don't need to show change in the game, so I would think they can be ignored???

I am not sure the linked forum post is going to help in this situation. The tester shows the code works, but there is something missing for the game to do something with it.

EDIT...
I got the game to recognize the code, but I think the code got further from what I want by doing so. All I did was complete the "else" statement with the opposite of what was in the "if" statement.

This is only going to work one way with how the game takes information... and I am not sure it is possible.

#include <Joystick.h>

// Define the Joystick
Joystick_ Joystick;

// Define pin numbers for buttons
const int buttonPin1 = 5; // Button 1 pin
const int buttonPin2 = 6; // Button 2 pin

// Variables to store button states
int buttonState1 = 0;
int buttonState2 = 0;

int lastButtonState1 = 0; // previous state of button1
int lastButtonState2 = 0; // previous state of button2

void setup() {
  // Initialize Joystick library
  Joystick.begin();

  // Set button pins as INPUT_PULLUP
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}

void loop() {
  // Read the state of the buttons
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  // Compare the buttonState1 to its previous state....  **THIS IS IGN**
  if (buttonState1 != lastButtonState1) {
    // The state has changed, check what it is now
    if (buttonState1 == LOW) {
      // If the current state is LOW, then the button went from off to on
      Joystick.setButton(0, HIGH); // **Turn the IGN on**
    } else {
      // If the current state is HIGH, then the button went from on to off
      Joystick.setButton(0, LOW); // **Turn the IGN off**
    }
  }

  // Update the last state for button1
  lastButtonState1 = buttonState1;


  // Compare the buttonState2 to its previous state
  if (buttonState2 != lastButtonState2) {
    // The state has changed, check what it is now
    if (buttonState2 == LOW) {
      // If the current state is LOW, then the button went from off to on
      Joystick.setButton(1, HIGH); // **Turn the RUN on**
    } else {
      // If the current state is HIGH, then the button went from on to off
      Joystick.setButton(1, LOW); // **Turn the RUN off**
    }
  }

  // Update the last state for button2
  lastButtonState2 = buttonState2;

  // Add a small delay to avoid button bouncing
  delay(10);
}

Yes, why I circled the two that seemed to need any response.

I have to assume you read both the thread I linked and the thread I linked from that thread. They address what seems to be exactly your issue.

You write

and I am not sure it is possible.

But if you can turn something on with a press of a real button, and turn it off with another press on the same button, then the idea I presented should work. Just fake both the press and release, as my snippet illustrates.

The delay may be necessary if the joystick library rejects things that look like button presses that are too short.

delay() between is a cheap and ugly hack. In some of the code from the threads Imlinked, Imshow a way to get around to impressing a button the next time through the loop, which in that case is throttled and operates at 50 Hz, so the events look like a press lasting 20 milliseconds. Or was it 20 Hz and 50 milliseconds? Either way, long enough to appear as a legit event.

When you see an edge (one of the four arrows in my diagram) that wants to make a button looked like it has been pressed and released, that's exactly the calls you must make to the joystick thing… a call that makes the "button" look pressed, followed by a call that makes the "button" looked unpressed.

I'm tapping out and sitting on my thumbs now. I am way out on a limb here, just looking at it logically and as I have said, no direct use of button boxes or joystick libraries.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.