1
\$\begingroup\$

Im making BattleShip on the Arduino:

2 x grid ((10x10 push buttons) + (10x10 led 5mm)) 2 x grid ((10x10 reed switches) + (10x10 led 5mm))

I made a 10x10 grid of push buttons by setting 10 buttons in a row with resistors on each button. By putting it on 5 Volts, each button give an other value of volt to Arduino when pushed.

In this case I got 10 cables from 1 x 10x10 button grid. total 4 grid (40 cables with analog input)

now my question is how to hook up those analog input wires to the Arduino UNO. Can you help me out here? Thanks

/e

im using 1 analog pin to hook up 10 buttons (in code 5 buttons).

Code:

for (int i = 0; i < 10; i++){

  if      ((voltage[i] >= 0.35) && (voltage[i] <= 0.50) && (gameStatus[i][0] == 0)){ gameStatus[i][0] = 2;}
  else if ((voltage[i] >= 0.35) && (voltage[i] <= 0.50) && (gameStatus[i][0] == 1)){ gameStatus[i][0] = 3;} 
  else if ((voltage[i] >= 1.40) && (voltage[i] <= 1.60) && (gameStatus[i][1] == 0)){ gameStatus[i][1] = 2;}
  else if ((voltage[i] >= 1.40) && (voltage[i] <= 1.60) && (gameStatus[i][1] == 1)){ gameStatus[i][1] = 3;}
  else if ((voltage[i] >= 2.40) && (voltage[i] <= 2.55) && (gameStatus[i][2] == 0)){ gameStatus[i][2] = 2;}
  else if ((voltage[i] >= 2.40) && (voltage[i] <= 2.55) && (gameStatus[i][2] == 1)){ gameStatus[i][2] = 3;}
  else if ((voltage[i] >= 3.65) && (voltage[i] <= 3.80) && (gameStatus[i][3] == 0)){ gameStatus[i][3] = 2;}
  else if ((voltage[i] >= 3.65) && (voltage[i] <= 3.80) && (gameStatus[i][3] == 1)){ gameStatus[i][3] = 3;}
  else if ((voltage[i] >= 4.30) && (voltage[i] <= 4.60) && (gameStatus[i][4] == 0)){ gameStatus[i][4] = 2;}
  else if ((voltage[i] >= 4.30) && (voltage[i] <= 4.60) && (gameStatus[i][4] == 1)){ gameStatus[i][4] = 3;}
}
\$\endgroup\$
7
  • \$\begingroup\$ That sounds painful. Why not use a device with more GPIOs and scan the matrix? \$\endgroup\$ Commented Jun 4, 2015 at 18:06
  • 1
    \$\begingroup\$ A matrix scan is definitely the way to go here. You can use some shift registers to pull it off. \$\endgroup\$ Commented Jun 4, 2015 at 18:08
  • \$\begingroup\$ My first thought (after reading the title) would be to use some ADC chips such as the Ti ADS7830 or ADC128D818, but for your use case I am sure there are more elegant ways of pulling it off \$\endgroup\$ Commented Jun 4, 2015 at 18:09
  • \$\begingroup\$ Please clarify: why does the button have to produce an analog voltage? A sketch of your proposed schematic would help us understand your situation better. Other than that, there are I/O expanders. Some of them have built-in grid scanning. \$\endgroup\$ Commented Jun 4, 2015 at 18:16
  • \$\begingroup\$ I think the analog level is the result of a proposal to multiplex by putting a different value resistor in series with each of 10 buttons that could connect two signals. \$\endgroup\$ Commented Jun 4, 2015 at 18:42

2 Answers 2

1
\$\begingroup\$

As others have said in the comments, there are probably better ways to do this. However, if you are set on needing more analog inputs, the answer is simple: use an analog multiplexer.

You can use the 74HC4067 chip to multiplex the signals into the Arduino. This is how it works:

  • 16 channels go into the chip
  • Only one comes out
  • The Arduino controls the chip, telling which of the 16 channels to output

It's basically like a big switch.

In your case, you have 40 cables. You could use 3 multiplexer chips, giving you a max of 48 total channels, multiplexed into 3 separate Arduino inputs, one for each chip. In your software, you would rapidly scan through each multiplexer, looking for a button press on each of the 16 input signals. Keep in mind however, that the analog multiplexer + Arduino ADC combination is going to be far slower than a digital solution. It takes the Arduino time to convert the analog voltage to a digital value. In the case of a button press, you want to respond quickly. Consider all your options before you go the way of an analog multiplexer.

Here is a relevant Arduino tutorial on the subject, using the same chip.

\$\endgroup\$
1
\$\begingroup\$

Speaking of digital options, you can use TI's PCF8575. It is a 16 channel general I/O expander with i2c interface. It has an interrupt pin that you can use to call an interrupt in your code. The interrupt is reset by reading or writing to the chip (pg.12). You can use multiple chips on the same i2c line and use pin change interrupts to catch the different chips interrupts. Just make the Interrupt Service Routine (ISR) efficient or you might not catch multiple inputs. Careful placement of you inputs will insure you will not have this issue.

http://www.ti.com/lit/ds/symlink/pcf8575.pdf

Good luck with your game. Digital is usually a good way to go with stuff like this. Noise is everywhere lol.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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