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:



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 &lt; 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?


Question: How can I make a graph of a function?
Answer: Compile the following code with xelatex or a combo sequence latex-dvips-ps2pdf.



\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-4.25,-1.25)(4.25,2.25)
    \def\f(#1){sin(2*#1)+0.5}
    \psaxes[labelFontSize=\scriptscriptstyle,ticksize=-2pt 2pt]{-&gt;}(0,0)(-4,-1)(4,2)[$x$,0][$y$,90]
    \psplot[linecolor=blue,algebraic]{-\psPi}{\psPi}{\f(x)}
    \rput[tl](*1 {\f(x)+0.5}){$y=\sin(2x)+\frac{1}{2}$}
\end{pspicture}
\end{document}


Explanation


Making diagrams (including function plotting) can be accomplished by using PSTricks (recommended because it is faster and easier to learn yet powerful) or TikZ  or others. The code above is written in PSTricks, you need to load \usepackage{pst-plot}.
To get a tight page, use 

\documentclass[pstricks,border=12pt]{standalone}

Define a canvas on which you draw.

\begin{pspicture}(-4.25,-1.25)(4.25,2.25)
 ... drawing codes go here ...
\end{pspicture}


(-4.25,-1.25) represents the bottom left point of your canvas and (4.25,2.25) is the top right point.
Define the function to plot.

\def\f(#1){sin(2*#1)+0.5}


In this example I chose y=sin(2x)+1/2.
Draw the coordinate axes.

\psaxes[labelFontSize=\scriptscriptstyle,ticksize=-2pt 2pt]{-&gt;}(0,0)(-4,-1)(4,2)[$x$,0][$y$,90]

Plot the graph.

\psplot[linecolor=blue,algebraic]{-\psPi}{\psPi}{\f(x)}

Put a label if necessary.

 \rput[tl](*1 {\f(x)+0.5}){$y=\sin(2x)+\frac{1}{2}$}


In PSTricks, we can specify a point in several ways. (*&lt;x-value&gt; {the value of expression in x for the given x}) is one of them. Thus (*1 {\f(x)+0.5}) mathematically means a point (x,y) where x=1 and y=f(1)+0.5.
Compile the input file with xelatex or the combo sequence latex-dvips-ps2pdf.
Done!


Miscellaneous

\psplot[linecolor=blue,algebraic,plotpoints=100]{Pi neg .5 sub}{Pi .5 add}{\f(x)}



The plotpoints can be used to increase the number of points used to plot the graph. But be wise, the greater its value is, the smoother the plot is but the bigger the file size becomes. For most extreme case, plotpoints=1000 should be more than enough. 
The first and second arguments of \psplot can accept RPN notation. In the example above, I used -π-.5 and π+.5 for both args, respectively. PSTricks gives you many good features, right?