0

There is probably another way to achieve what I want without connecting pins to each other, so I will explain the scenario:

I am trying to build an Arduino based Enigma Machine. The cryptography is working fine, it runs from the serial monitor. Now I want to make the interface more real.

The original machine has a plugboard: 26 terminals (labeled from A to Z) where you can connect a cable from one letter to another, and the user can make up to 10 of these connections.

I am stuck in this part. How could I do it electronically? How to tell the microcontroller if a pin is connected to any other pin?

If I assign each terminal to a separated pin on the board, I can't have these pins in output and input mode at the same time.

My first idea was to make an array of resistors to make uniques associations and read the different analog values, but it is impractical.

I also tried to draw a logic circuit to read exact associations, but also doesn't work.

I know it can be done with an Arduino Mega, as there are replicas being sold online based on this board. Is there any other hardware suitable for the situation? I wiil be glad to hear.

Diego Dyan
  • 23
  • 1
  • 1
  • 6

2 Answers2

3

If you have enough pins for all 26 terminals, you can directly through all connection iteratively:

  • Set all pins to INPUT_PULLUP
  • Set one pin to OUTPUT and LOW
  • Read all pins beside that one output pin and check, if one reads LOW (meaning connection between output pin and this one)
  • Repeat for all pins (each pin being an output pin in one iteration)

You could save this in an array of 26 ints. The position in the array would be the "start pin" of the connection, the value would be the target pin.

With the above algorithm you of course get each connection twice. Either just ignore the second one in whatever you are doing with the data, or you can check, if that pin already got a connection and set it's value to -1 (meaning "not important/not needed").

If you don't have enough pins, you can use a multiplexer, where you can connect multiple terminals to one pin on the Arduino, one at a time, iterating again over very terminal.

chrisl
  • 16,622
  • 2
  • 18
  • 27
0

You of course don't need to get each one twice.

In your second loop, start at the index of the current pin.

Like this:

#define START 22
#define END   45

static int lasti = -1; static int lastj = -1; static bool bFound;

static char buf [1024];

void setup () { // put your setup code here, to run once: int i;

for (i = START; i <= END; i++) { pinMode (i, INPUT_PULLUP); }

Serial.begin (115200); }

void loop () { bFound = false; int i, j;

for (i = START; i <= END; i++) { pinMode (i, OUTPUT); digitalWrite (i, LOW);

// This is the scan loop
// Notice that we start at i + 1, not zero
for (j = i + 1; j &lt;= END; j++)
{
  if (digitalRead (j) == LOW)
  {
    // Found a winner
    bFound = true;
    if (i != lasti || j != lastj)
    {
      lasti = i;
      lastj = j;

      sprintf (buf, &quot;%d / %d&quot;, i - START + 1, j - START + 1);
      Serial.println(buf);
    }
  }
}

pinMode (i, INPUT_PULLUP);

}

// If we get here and bFound is false - all keys are up if (!bFound) { lasti = -1; lastj = -1; } }