I have this chess board and pieces with magnets on the bottom. My idea is to place a reed switch below each tile so that Arduino can detect movements by comparing boards before and after one movement.
How do I wire these reed switches to use as less IO pins as possible without using too much code (I need room for the chess logic)?
Gatis:
I have this chess board and pieces with magnets on the bottom. My idea is to place a reed switch below each tile so that Arduino can detect movements by comparing boards before and after one movement.
How do I wire these reed switches to use as less IO pins as possible without using too much code (I need room for the chess logic)?
A basic switch matrix (similar to what's used in the Keypad library) will give you a major reduction in I/O necessary. You'll still need 16 total wires to the chessboard, but provided you don't need a lot of additional I/O for other things it's do able. You could theoretically get it down to a single pin by using a form of serial encoding, but you'd need a non-trivial amount of logic circuitry between the chess board and the Arduino to achieve that.
That would allow you to read in all 64 through just 3 pins (Clock, Data, Latch). The 165's can be daisy chained such that they all share the same clock and latch(PL) pins, and the serial out from one goes into the serial in of the next and so on until the 8th whose serial out goes into the arduino.
You would basically just have to pulse the latch pin, and then call shiftIn() 8 times to get all 64 pins.
I wrote "similar", not "exactly like" a keypad. Read the other link I included, especially the last section titled "Scanning Method". This method polls only one row at a time in quick succession, instead of all rows all the time. This allows the presence or absence of a piece to be accurately detected because the pin attachted to each column can only register the switch for the single row being polled.
In that diagram, an x means switch pressed (connected), o means unconnected. If you turn the row0 on (indicated by -----), then you will read 110 on the columns even though only the first switch on row0
Edit: Ahh, I see the diodes there, that would solve the problem.
When you capture a piece your detection of movement is going to get a little goofy. It's no longer a simple problem of "piece removed from spot X, placed at spot Y". You would have to track whose turn it is or guarantee that the capture piece is always picked up first/second before the capturing piece is laid in its place.
Would be nice to be able to detect the black pieces from the white pieces. I had been looking for a hall sensor that could detect either of N, S, or none but could only find one in a 5sot-553 package (which is puny).
The issue is not the chips you are using. To do a simple matrix like that which requires more than one switch to be pressed (at most 32 are required for chess), you have to use diodes.
The alternative to using diodes for each switch is to use 8 47HC165s as suggested already.
You may need pull up resistors on each of the 74HC165 inputs to ensure a known state when the reed switches are open (unless they are SPDT). But as resistors are 0.1pence a shot, cost isn't much of an issue, and space shouldn't be too bad.
Having said that you would still need a pullup resistor on each input of the 74HC165 for the other method as well for the same reason.
Hm... then sounds like the same amount of soldering trouble, but cheaper? I'm failing to see other benefits. I find soldering one reed switch to each diode and dealing with a single IC easier to do.
I am trying to build a reed switch based chess board too. The scanning matrix aproach seems the most elegent solution.
However there is another way. (I think, I am new to Arduinio and may have missed something) Have you seen the Centipede Shield? See Centipede Shield V2 - Macetech Electronics Store. This is a shield with 64 pins that links to the Arduino boad using only a few pins. It also uses ribbon style connectors so that would simplify wiring. I assume you would wire one end of all the reed switches to ground and the other 64 to ribbon ends.
I assume that this would simplify processing as you would just need to scan for a change of state. (Would you still need a resistor or diode on the Reeds?)
If you looked for two move combinations, ie the "From" move followed by the "To" move, then the first move would always be a change of state from closed (Occupied square) to open (Piece gone). The second move would be to an empty square (Open to closed) unless its a capute then its to a closed and you can assume you will get a combination as the old piece is first lifted (closed to open) and the replaced (Open to closed). But all are on the same square and the final state is closed (occupied).