ignore a button

hi

i have a switch (blinker switch from a DAF XF105) connected to my Arduino. the wiper switch has 4 modes in the order 0 - 1 - 2 - 3 (0 does nothing). when i switch it from 0 to 3 it presses 1, 2 and 3. but when i switch it back it should ignore position 2 and only press on 1. is that possible and if so what code is the best to use for it?

greetings,

devon

Image from Original Post so we don't have to download it. See this Simple Image Guide

...R

How have you the switch contacts connected to your Arduino?

If you have been using a test program to check the working of the switch please post it.

...R

i soldered jumper wires to the connector and plugged that into a breadboard. i use this for ETS2 and other driving sim games

void loop() {
  int buttonPressed = getButtonPress();
  static int lastButtonPressed;

  switch(buttonPressed) {
    case 0:
      pressButton(0);
    break;
    case 1:
      pressButton(1);
    break;
    case 2:
      if(lastButtonPressed!=3) pressButton(2);
    break;
    case 3:
      pressButton(3);
    break;
  }
  lastButtonPressed = buttonPressed;
}

Without seeing your code, the names of the get... and press... functions are my own invention. You should probably use different names to avoid confusion between input and output. Maybe one side is called a "switch"?

MorganS:

void loop() {

int buttonPressed = getButtonPress();
 static int lastButtonPressed;

switch(buttonPressed) {
   case 0:
     pressButton(0);
   break;
   case 1:
     pressButton(1);
   break;
   case 2:
     if(lastButtonPressed!=3) pressButton(2);
   break;
   case 3:
     pressButton(3);
   break;
 }
 lastButtonPressed = buttonPressed;
}




Without seeing your code, the names of the get... and press... functions are my own invention. You should probably use different names to avoid confusion between input and output. Maybe one side is called a "switch"?

i'm using this code

{
  // see if switch A is OFF or ON
  byte switchStateA = digitalRead (switchPinA);

  // has it changed since last time?
  if (switchStateA != oldSwitchStateA)
  {
    oldSwitchStateA =  switchStateA;  // remember for next time
    delay (debounceTime1);   // debounce
    if (switchStateA == LOW)
    {
      Keyboard.print("a");
    }  // end if switchStateA is LOW
    else
    {
      Keyboard.print("a");
    }  // end if switchStateA is HIGH
  }  // end of state change
[code]

[/code]

delay() is pretty crude for debouncing. I would put the delay after the keyboard.print()'s.

Make a new variable so that when it gets to 3 you set that to true and then position 2 can see that the previous valid position was 3. Only reset that back to false when you get to position 1. So position 2 knows that it entered 2 from the 1 direction and not the 3 direction.

mgysgtdevon:
i soldered jumper wires to the connector and plugged that into a breadboard. i use this for ETS2 and other driving sim games

I wanted you to provide a diagram.

...R

I was thinking. Would it work if i use.

/If(lastswitchState=switchStateA)
   /keyboard.press "b"
   Else
   //do nothing
[Code]

MorganS:
delay() is pretty crude for debouncing. I would put the delay after the keyboard.print()'s.

Make a new variable so that when it gets to 3 you set that to true and then position 2 can see that the previous valid position was 3. Only reset that back to false when you get to position 1. So position 2 knows that it entered 2 from the 1 direction and not the 3 direction.

and what do you mean with making a new variable? or how can i do that cause i tried this method and it didn't work

  // has it changed since last time?
  if (switchStateG != oldSwitchStateG)
  {
    oldSwitchStateG =  switchStateG;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateG == LOW)
    {
      Keyboard.print("g");
      digitalWrite (SwitchStateJ, true);
    }  // end if switchStateG is LOW
  }  // end of state change

  // see if switch H is OFF or ON
  byte switchStateH = digitalRead (switchPinH);
  byte switchStateJ = digitalRead (switchStateJ);

  // has it changed since last time?
  if (switchStateH != oldSwitchStateH)
  {
    oldSwitchStateH =  switchStateH;  // remember for next time
    delay (debounceTime);   // debounce
    if (switchStateH == LOW  && SwitchStateJ == HIGH)
    {
      Keyboard.print("h");
      Keyboard.print("h");
      digitalWrite (SwitchStateJ, LOW);
    }  // end if switchStateH is LOW
    else if (switchStateH == LOW)
    {
      Keyboard.print("h");
    }
    else
    {
      Keyboard.print("h");
      digitalWrite (SwitchStateJ, HIGH);
    }
  }  // end of state change
[code]
  byte switchStateJ = digitalRead (switchStateJ);

That's obviously not going to work. At best it will read the state of pin 0, the serial data RX pin. At worst, it will read a completely random pin number, since this is a local variable and it's un-initialised when you use it on the right-hand side of that assignment operation.

Then later on you use that in a digitalWrite(). It may write to pin 0 or pin 1, depending on the value.

I've lost which switch belongs to which position on the knob you are trying to work with. We started with position 1,2,3 and now its 'h' and 'g'.

The new variable to remember that the previous position was 'special' should be global or it should be static, so that its value is remembered. Creating a new, temporary, local variable like you did in the quote above isn't helpful.