0
\$\begingroup\$

I'm trying to build a simple gamepad controller to learn more about multiplexers (in this case a SN74LS251. This is how I'm wiring it:

Circuit

And this is the code I'm using to get the button states:

// Pins.
unsigned int y = 8;
unsigned int a = 9;
unsigned int b = 10;
unsigned int c = 11;

// Gamepad states.
unsigned int gamepad[2][4];

/**
 *  The usual Arduino setup.
 */
void setup() {
  // Set pin modes.
  pinMode(y, INPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);

  // Set select pins LOW.
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);

  // Begin the Serial connection for debugging.
  Serial.begin(9600);
}

/**
 *  Set the select pins in the multiplexer.
 *
 *  @param a_stt Select pin A state.
 *  @param b_stt Select pin B state.
 *  @param c_stt Select pin C state.
 */
void multiplex_set(unsigned int a_stt, unsigned int b_stt, unsigned int c_stt) {
  digitalWrite(a, a_stt);
  digitalWrite(b, b_stt);
  digitalWrite(c, c_stt);
}

/**
 *  Get all the Gamepad states and put them into the result array.
 */
void get_gamepad_states() {
  // Left Gamepad.
  multiplex_set(LOW, LOW, LOW);
  gamepad[0][0] = digitalRead(y);
  multiplex_set(HIGH, LOW, LOW);
  gamepad[0][1] = digitalRead(y);
  multiplex_set(LOW, HIGH, LOW);
  gamepad[0][2] = digitalRead(y);
  multiplex_set(HIGH, HIGH, LOW);
  gamepad[0][3] = digitalRead(y);

  // Right Gamepad.
  multiplex_set(LOW, LOW, HIGH);
  gamepad[1][0] = digitalRead(y);
  multiplex_set(HIGH, LOW, HIGH);
  gamepad[1][1] = digitalRead(y);
  multiplex_set(LOW, HIGH, HIGH);
  gamepad[1][2] = digitalRead(y);
  multiplex_set(HIGH, HIGH, HIGH);
  gamepad[1][3] = digitalRead(y);
}

/**
 *  The usual Arduino loop.
 */
void loop() {
  get_gamepad_states();

  for (unsigned int i = 0; i < 2; i++) {
    if (i == 0) {
      Serial.println("LEFT");
    } else {
      Serial.println("RIGHT");
    }

    Serial.print("  Up: ");
    Serial.println(gamepad[i][0]);
    Serial.print("  Right: ");
    Serial.println(gamepad[i][1]);
    Serial.print("  Down: ");
    Serial.println(gamepad[i][2]);
    Serial.print("  Left: ");
    Serial.println(gamepad[i][3]);
    Serial.println("");
  }

  delay(500);
}

It was working last week when I tried it for the first time, but when I went to rebuild it today I started getting strange (and completely incorrect) readings like these (no buttons were pressed during this test and I still got some HIGHs for no reason):

LEFT
  Up: 0
  Right: 0
  Down: 0
  Left: 0

RIGHT
  Up: 0
  Right: 0
  Down: 0
  Left: 1

LEFT
  Up: 0
  Right: 0
  Down: 0
  Left: 0

RIGHT
  Up: 0
  Right: 0
  Down: 0
  Left: 0

LEFT
  Up: 0
  Right: 0
  Down: 0
  Left: 0

RIGHT
  Up: 0
  Right: 0
  Down: 0
  Left: 0

Also if I press a button nothing happens. Even if I put the Enable pin HIGH I'll still get the same pattern. What's wrong?

\$\endgroup\$
  • \$\begingroup\$ I don't see any bypass caps on that IC. Add a 0.1 uF cap across the power connections to the IC as close as you can manage. \$\endgroup\$ – Connor Wolf Mar 29 '13 at 15:08
  • \$\begingroup\$ I don't see that your pull-ups or pull-downs are connected to anything. I don't know the breadboard connections but something looks wrong? \$\endgroup\$ – Andy aka Mar 29 '13 at 16:05
  • 1
    \$\begingroup\$ A proper schematic is required! \$\endgroup\$ – Leon Heller Mar 29 '13 at 16:39
  • 1
    \$\begingroup\$ Post a schematic. We can't see the circuit easily from that wiring diagram, which is largely useless. \$\endgroup\$ – Olin Lathrop Mar 29 '13 at 17:37
  • 1
    \$\begingroup\$ I upvoted to counter the ridiculous amount of negativity on this one (-3...really?), but please do ditch the Fritzing stuff and post a proper schematic. Nobody here will take such diagrams seriously. \$\endgroup\$ – dext0rb Mar 29 '13 at 18:38
2
\$\begingroup\$

There appears to be connection problems with your breadboard: -

enter image description here

Shouldn't one side of each switch be (say) at 0V whilst the other side has a pull-up?

\$\endgroup\$
  • \$\begingroup\$ Sorry, the bottom ones are all connected to V+ in the breadboard, it was just a little error I made while trying to recreate the circuit in Fritzing. \$\endgroup\$ – Nathan Campos Mar 29 '13 at 16:16
  • 2
    \$\begingroup\$ @NathanCampos dude, how on earth can you expect us to diagnose the problem when you don't send a circuit diagram and the wiring diagram is wrong in a the place where you are having problems. Down-voted on that basis \$\endgroup\$ – Andy aka Mar 29 '13 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.