ignition key

Hi,

I have an ignition key from a truck. It has (like a car) three stages. Accessory, drive and start. Each stage functions like a button. So when the key is in accessoiry it sends a LOW signal and when the key is moved it turns HIGH.
Now is there something wrong internally and I don't know what since I can't get into it to see and I also don't want to spend allot of money again to get a new ignition key. So I was thinking about rewriting the code I made for it. but I want to tell arduino what the last stage was when the key is moved between the different stages.
Here is the idea what I am trying to achieve. Lets say the key is moved from accessory to drive. That way accessory is HIGH and drive is LOW. When the key arives at drive I need to tell arduino that it came from accessory and not from start.

Here is the code so far but I have no idea how to tell arduino the last button press.

  // Accessory
  byte switchStateA = digitalRead (switchPinA);
  if (switchStateA != oldSwitchStateA)
  {
    oldSwitchStateA =  switchStateA;
    delay (debounceTime);
    if (switchStateA == LOW)
    {
      Serial.println("Accessory LOW");
    }
    else
    {
      Serial.println("Accessory HIGH");
    }
  }



  // Drive
  byte switchStateB = digitalRead (switchPinB);
  if (switchStateB != oldSwitchStateB)
  {
    oldSwitchStateB =  switchStateB;
    delay (debounceTime);
    if (switchStateB == LOW)
    {
      Serial.println("Drive LOW");
    }
    else
    {
      Serial.println("Drive HIGH");
    }
  }



  // Start
  byte switchStateC = digitalRead (switchPinC);
  if (switchStateC != oldSwitchStateC)
  {
    oldSwitchStateC =  switchStateC;
    delay (debounceTime);
    if (switchStateC == LOW)
    {
      Serial.println("Start LOW");
    }
    else 
    {
      Serial.println("Start HIGH");
    }
  }

Best regards,

Devon

That sounds like a state machine to me

List the states that the program can be in, the events that will cause it to change state, the target state and what needs to be set up before the code for the target state runs

Using switch/case is a very convenient way to implement a state machine

I've never used switch/case in my codes before. But I'll try it. Do I need to make one long string of cases or do I need to make one set of cases per switch?

I got it working (I hope) without using switch/case.
Here is my code I'm using.
I first thought that if I used a debounce of 50ms it would cancel the technical problems inside the ignition key but I ended up using a debounce of 400ms and so far it cancels it.

  // Accessory
  byte switchStateA = digitalRead (switchPinA);
  if (switchStateA != oldSwitchStateA)
  {
    oldSwitchStateA =  switchStateA;
    delay (400);
    if (countIgnition == 0 && switchStateA == LOW)
    {
      Keyboard.press('c');
      delay(560);
      Keyboard.release('c');
      countIgnition = 1;
      countMedia = 1;
    }
    else if (countIgnition == 2 && switchStateA == HIGH)
    {
      Keyboard.press('d');
      delay(1000);
      Keyboard.release('d');
      countMedia = 0; 
      countIgnition = 0;
    }
  }



  // Drive
  byte switchStateB = digitalRead (switchPinB);
  if (switchStateB != oldSwitchStateB)
  {
    oldSwitchStateB =  switchStateB;
    delay (400);
    if (switchStateB == LOW)
    {
      Joystick.pressButton(0);
      countCC3 = 1;
      countCC1 = 0;
    }
    else if (switchStateB == HIGH)
    {
      Joystick.releaseButton(0);
    }
  }



  // Start
  byte switchStateC = digitalRead (switchPinC);
  if (switchStateC != oldSwitchStateC)
  {
    oldSwitchStateC =  switchStateC;
    delay (400);
    if (switchStateC == LOW)
    {
      Joystick.pressButton(1);
    }
    else 
    {
      Joystick.releaseButton(1);
      countIgnition = 2;
      countCC3 = 0;
      countCCOFF = 0;
      countCCRES = 0;
    }
  }

You say that you got it working so the possible use of switch/case is moot but it is a handy technique. It doesn't do anything more than if/else if/else (in fact less in some ways) but it does result in very clean looking code which helps understand and debugging it

The principle would be to have the switch/case based on the current state of the system then, depending on the current case (waiting for key, key in position 1, key in position 2, whatever) you execute the code for the current case until a trigger event means that the program should change to a different case and therefore execute the code for that state

I note in your code that you seem to be using keyboard input that was not previously mentioned

The keyboard presses I put in there for the final result since I'll be using it for gaming. But it is not necessary for the code to be working, that's why I left it out of the code in the first place. As for the switch/case part. I might use it if I ever plan on adding some components to one of my boards. But thank you for your time and help.