DPDT Switch set up correctly?



Last night I quickly crammed information about toggle switches in order to get some idea for what I plan on doing. Since I am not very savvy with schematics, I instead drew simple diagrams. What I want to do is have two mechanical switches to be toggle-able between two pairs of digital pins on a pro micro board. On one pair of digital pins the switches will signal once per keypress on the PC, and on the other pair of pins they would be programmed so that for every keypress the PC will detect two signals back to back. The PC will see me as pressing the buttons twice rapidly when I still am only pressing them once. I was wondering:

  1. Am I correct that I would need a DPDT switch for this?
  2. Are either of these diagrams correct in how to wire them up?
1 Like

Well, where I tried to organize...

  1. When you press the any buttons on the left on image, Arduino will send some key to your PC.
  2. The key send twice by if changed the toggle switch.

Is that all right?
If in that case the figure is wrong and you don't even need a DPDT switch.
Additionally, It is a project that can be fully realized even with an alternate push switch.
If you want a toggle switch, the SPST one will suffice.
Or there are a lot of unused terminals, but it can also be used with DPDT.
Because basically upward compatible except for the price and sizes.

I fully respect schematic you drawn and suggest this connection.

I’m just having a hard time understanding the logic of it. The switch in that scenario doesn’t seem to be redirecting the paths the switches would take. I sort of pictured the switch serving like a crossroads where the path to the board from the switches could take one of two paths. I guess I just don’t quite grasp this sort of thing.

First of all, my interpretation of #2 is correct?

Arduino is smart enough to read the state of the toggle switch and change the number of keys sent to the PC when the button is pressed.
Circuits that include uC (Microcontroller) such as Arduino, can do different behaviors depending on the code, so it isn't possible to determine what will happen from the schematic only.

EDIT:
In other words, it's like that the button and toggle switch intersected inside the Arduino.

Oh okay I see. Well. I do not know how to code well enough to tell the board to change pin behaviors based on states of toggle switches. I really haven’t hardly ever coded before. So I figured I would use up extra pins and just redirect the mechanical switches to two extra pins which have different permanent coded behaviors instead of using only two total digital pins for the mechanical switches that change based on the toggle switches detected position because I do not know how to do that.
If I had to guess, it would maybe be an if then sort of code where if the circuit connected to the toggle switch is closed, then the pins connected to the mechanical switches would change to signal to the computer an extra time per every switch press. I just don’t know what that code would look like.

Certainly that approach may be achievable...
But now that you've began Arduino!
Try write some code?
I'm here to help with that.

Is it already coded to send the single key when the button is pressed?
Can you post the code here? Don't forget the code tag.
There is also the function to copy for forum post by selecting all the code in the IDE and right clicking.

I dunno if it matters, but the digital pins the mechanical switches in question would be hooked up to would be 8 and 9 in the code, but I can always move them around.

#include <Joystick.h>

Joystick_ Joystick;

int RxAxis_ = 0;
int LxAxis_ = 0;
int LyAxis_ = 0;

const bool initAutoSendState = true;

void setup()
{
pinMode (4, INPUT_PULLUP); 
pinMode (5, INPUT_PULLUP);
pinMode (6, INPUT_PULLUP);
pinMode (7, INPUT_PULLUP);
pinMode (14, INPUT_PULLUP);

pinMode (8, INPUT_PULLUP);
pinMode (9, INPUT_PULLUP); 

pinMode (10, INPUT_PULLUP);
pinMode (16, INPUT_PULLUP);

pinMode (15, INPUT_PULLUP); 

Joystick.begin();
}

void loop() {

{
if (digitalRead(4) == LOW)
Joystick.pressButton(0);
else
Joystick.releaseButton(0);
}

{
if (digitalRead(5) == LOW)
Joystick.pressButton(1);
else
Joystick.releaseButton(1);
}

{
if (digitalRead(6) == LOW)
Joystick.pressButton(2);
else
Joystick.releaseButton(2);
}

{
if (digitalRead(7) == LOW)
Joystick.pressButton(3);
else
Joystick.releaseButton(3);
}

{
if (digitalRead(8) == LOW)
Joystick.pressButton(4);
else
Joystick.releaseButton(4);
}

{
if (digitalRead(14) == LOW)
Joystick.pressButton(5);
else
Joystick.releaseButton(5);
}

{
if (digitalRead(9) == LOW)
Joystick.pressButton(6);
else
Joystick.releaseButton(6);
}

{
if (digitalRead(10) == LOW)
Joystick.pressButton(7);
else
Joystick.releaseButton(7);
}

{
if (digitalRead(16) == LOW)
Joystick.pressButton(8);
else
Joystick.releaseButton(8);
}

{
RxAxis_ = analogRead(A0);
RxAxis_ = map(RxAxis_,1023,0,1023,0);
Joystick.setRxAxis(RxAxis_);
}

delayMicroseconds(1000);
}

My guess based on what tiny amount I think I know, the start of the code for the toggle switch would look like this, and the board would constantly check if the pin for the toggle switch has a closed circuit.

If (digitalRead() == LOW

I just don’t know how to write the code that would come after that to change the pins for the mech switches, but I assume maybe PulseIn or something is involved I don’t know.

Thanks for the provide the code.
When I read it, it seems that if you hold down and keep pressing the button, it will be sent to the PC correctly even as well.
When you change to a toggle switch to be sent twice, you need to decide what happens if you hold down the button.

When the mechanical switches/buttons are the mode where they will send it to the PC twice rapidly, I would like it where it only sends it twice and then stops and waits until I press the buttons again, which it will then send two more rapid signals.

That, or if possible another way maybe is to make it so the PC reads me pressing the buttons as one signal, and then letting go of the button as another, but I wouldn’t know how to do that either.

I checked the contents of the library..

Joystick.pressButton(x);

And

Joystick.releaseButton(x);

This is sent key to the PC instantly.
by you setting below.

const bool initAutoSendState = true;

In other words

Joystick.pressButton(x);
Joystick.releaseButton(x);

This is will be button is pressed once.

For the time being, forget once about the toggle switch...

  {
    static bool isNotPressed = true;
    if (digitalRead(8) == LOW)
    {
      if (isNotPressed)
      {
        Joystick.pressButton(4);
        Joystick.releaseButton(4);
        Joystick.pressButton(4);
        Joystick.releaseButton(4);
        isNotPressed = false;
      }
    } else {
      isNotPressed = true;
    }
  }

Please possible to check if the key sent twice with the this code?
Try replacing the "8-button" in your code with this one.
There was an extra curly brace, but I dare to leave it as it is.

I tried using the code, and if I placed it in the right spot, it doesn't seem to work consistently. Whenever I press and hold down the button, it will activate on the pc multiple times quickly, but in a choppy and inconsistent way. It will pause slightly in random intervals.

OK, for the time being, We need a correct code that will be send twice correctly.
I thought to respect the code you wrote as much as possible, but it seems that I need to rewrite the whole thing a little.
I will post it later thinking about take what method as easy as possible to make achieve.

Also, since the loop period is (roughly) 1ms, the switch still has enough time to leave a bounce, which also needs to be addressed.
Do you have a small ceramic capacitor of about 0.1uF? If you have one, you can also debounce it in hardware.

I don’t have a capacitor unfortunately. Also that code was made by someone else specifically for a game controller run by arduino connected to the computer. It isn’t anything I made. Also thank you for the help. I am not asking that you take such time to figure this out, but if it is something you want to do then by all means.

I experimented with the code myself. The closest thing to the result I could achieve was put in double if conditions. If (digitalRead(pin connected to toggle) && digitalRead(pins for switches) == LOW) then I told the code if those conditions were true that whenever the buttons were pressed, it would press the button, delay a few milliseconds, release the button, delay, etc. however, all that did was make it continue inputting the button on the computer at a faster rate and wouldn’t stop until I let go of the button. Plus I have read recently delay() isn’t good to use since it keeps the arduino from doing anything else more or less.

I'm doing this as a enjoy so don't worry about anything.
Perhaps, for those who post answers in these forums, a decent response is enough to reward.

I understood it you don't have a any capacitor and that the hardware is complete.

Excellent, It is good to challenge each other code development.
I'm not saying that delay should never be used, but it's certainly not very good.
Especially for this application.

By the way, I came up with a another new button code before the much change the code.
can you try it?

The full code will be displayed when expanded this.
#include <Joystick.h>

#define duration 5

Joystick_ Joystick;

int RxAxis_ = 0;
int LxAxis_ = 0;
int LyAxis_ = 0;

void setup()
{
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);
  pinMode (7, INPUT_PULLUP);
  pinMode (14, INPUT_PULLUP);

  pinMode (8, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);

  pinMode (10, INPUT_PULLUP);
  pinMode (16, INPUT_PULLUP);

  pinMode (15, INPUT_PULLUP);

  Joystick.begin(false);
}

void loop() {

  {
    if (digitalRead(4) == LOW)
      Joystick.pressButton(0);
    else
      Joystick.releaseButton(0);
  }

  {
    if (digitalRead(5) == LOW)
      Joystick.pressButton(1);
    else
      Joystick.releaseButton(1);
  }

  {
    if (digitalRead(6) == LOW)
      Joystick.pressButton(2);
    else
      Joystick.releaseButton(2);
  }

  {
    if (digitalRead(7) == LOW)
      Joystick.pressButton(3);
    else
      Joystick.releaseButton(3);
  }

  //-----------------------------------------------------------
  {
    static unsigned char stateSequence = 0;
    switch (++stateSequence)
    {
      case 1:
        if (digitalRead(8) == HIGH) stateSequence--;
        break;
      case duration * 1:
      case duration * 3:
        Joystick.pressButton(4);
        break;
      case duration * 2:
      case duration * 4:
        Joystick.releaseButton(4);
        break;
      case duration * 5:
        stateSequence--;
        if (digitalRead(8) == HIGH) stateSequence = 0;
    }
  }
  //-----------------------------------------------------------

  {
    if (digitalRead(14) == LOW)
      Joystick.pressButton(5);
    else
      Joystick.releaseButton(5);
  }

  {
    if (digitalRead(9) == LOW)
      Joystick.pressButton(6);
    else
      Joystick.releaseButton(6);
  }

  {
    if (digitalRead(10) == LOW)
      Joystick.pressButton(7);
    else
      Joystick.releaseButton(7);
  }

  {
    if (digitalRead(16) == LOW)
      Joystick.pressButton(8);
    else
      Joystick.releaseButton(8);
  }

  {
    RxAxis_ = analogRead(A0);
    RxAxis_ = map(RxAxis_, 1023, 0, 1023, 0);
    Joystick.setRxAxis(RxAxis_);
  }

  Joystick.sendState();
  delayMicroseconds(1000);
}

It's not very beautiful, so other poster may give pointed out.

At last, what pin did the toggle switch connect to?

I’ll say the toggle will be plugged into digital pin 15. I don’t know if I will keep it to that, but I can always change it.

You seem to be really, really close. That code does cause the switches to trigger twice each per keypress and then stop which is what I wanted, however I was hoping that it would be faster. With what I plan on using the switches for in this game I plan on using them very fast, so they need to squeeze in two inputs per keypress in the amount of time it takes for me to alternate to the other switch, which is a matter of milliseconds. I can alternate between the two switches on this controller a few times just in one second, so the time it takes to cause a second trigger per switch has to be really fast to keep up. I was using the switches in game and was able to press the buttons faster than the code could keep up. But I consider this progress nonetheless lol.
Sorry I may not be as much help as some other people. My knowledge on this stuff is extremely limited. But thanks.

Speed-up faster is not particularly difficult.
Arduino Pro Micro and I can do that if you wish.

By the way, did the new "twice key press" code is it complete work?

If that now it's time to remember the toggle switch before speeding up.
Well... I thought as follows...
Read the toggle switch before reading the button and use "if ~ else ~" to swap the entire button code.

Please expand.
#include <Joystick.h>

#define duration 5

Joystick_ Joystick;

int RxAxis_ = 0;
int LxAxis_ = 0;
int LyAxis_ = 0;

void setup()
{
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);
  pinMode (7, INPUT_PULLUP);
  pinMode (14, INPUT_PULLUP);

  pinMode (8, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);

  pinMode (10, INPUT_PULLUP);
  pinMode (16, INPUT_PULLUP);

  pinMode (15, INPUT_PULLUP);

  Joystick.begin(false);
}

void loop() {

  {
    if (digitalRead(4) == LOW)
      Joystick.pressButton(0);
    else
      Joystick.releaseButton(0);
  }

  {
    if (digitalRead(5) == LOW)
      Joystick.pressButton(1);
    else
      Joystick.releaseButton(1);
  }

  {
    if (digitalRead(6) == LOW)
      Joystick.pressButton(2);
    else
      Joystick.releaseButton(2);
  }

  {
    if (digitalRead(7) == LOW)
      Joystick.pressButton(3);
    else
      Joystick.releaseButton(3);
  }

  //-----------------------------------------------------------
  {
    static unsigned char stateSequence = 0;
    // Check toggle switch to selected mode.
    // If you swap HIGH and LOW then even swap the mode of the lever position.
    if (digitalRead(15) == HIGH)
    { // Start Normal-mode Code block
      if (digitalRead(8) == LOW)
        Joystick.pressButton(4);
      else
        Joystick.releaseButton(4);
    } // End of Normal-mode Code block
    else
    { // Start Twice-mode Code block
      switch (++stateSequence)
      {
        case 1:
          if (digitalRead(8) == HIGH) stateSequence--;
          break;
        case duration * 1:
        case duration * 3:
          Joystick.pressButton(4);
          break;
        case duration * 2:
        case duration * 4:
          Joystick.releaseButton(4);
          break;
        case duration * 5:
          stateSequence--;
          if (digitalRead(8) == HIGH) stateSequence = 0;
      }
    } // End of Twice-mode Code block
  }
  //-----------------------------------------------------------

  {
    if (digitalRead(14) == LOW)
      Joystick.pressButton(5);
    else
      Joystick.releaseButton(5);
  }

  {
    if (digitalRead(9) == LOW)
      Joystick.pressButton(6);
    else
      Joystick.releaseButton(6);
  }

  {
    if (digitalRead(10) == LOW)
      Joystick.pressButton(7);
    else
      Joystick.releaseButton(7);
  }

  {
    if (digitalRead(16) == LOW)
      Joystick.pressButton(8);
    else
      Joystick.releaseButton(8);
  }

  {
    RxAxis_ = analogRead(A0);
    RxAxis_ = map(RxAxis_, 1023, 0, 1023, 0);
    Joystick.setRxAxis(RxAxis_);
  }

  Joystick.sendState();
  delayMicroseconds(1000);
}

How is it?

By the way, the code is purposely slowed down by me. :roll_eyes: