Option 1
Seems you say you can make the balls out of whatever you want, I would suggest the spheres be made of a metal and then you can either suspend them by wire spot welded or soldered onto the balls. Then what you do is you connect all the balls to inputs of an Arduino ( this is if you are doing this with one). Then connect all inputs to pull-up resistors.
Then to read if a ball is touching a neighbour you start with ball n
and set it as an output
and use digitalWrite(n,LOW);
and then poll all the other balls as inputs to see which one reads LOW/0V
and then record that in an array, you the move onto ball n+1
and scan the rest of the balls. There is also no need to read the previous ball to n+1
as if it did not register a touch, then it won't register with the next ball.
Then at the end you reset everything and start at the beginning again. Using the data how you wish at the end.
Below code is just to show, not tested.
for(int n =0; n < total_N; n++){
pinMode(n, OUTPUT);
for(int i = 0; i < total_inputs; i++){
if(!(digitalRead(n)))// ball is touching
{
myArray[i] = 1;// store a point in the list, replace with a way you want
}
else myArray[i] = 0;
}
pinMode(n, INPUT);
}
Option 2
(probably to much to go wrong)
You could use material that will allow IR rays to pass through and make you balls with that. Then in each ball is have a IR transmitter and a IR receiver, making a transceiver basically and then have each ball echo a specific ID and let the other balls pick it up.
To solve the issue that IR travels a good distance you will need to limit the power/voltage/current to the IR transmitter(LED) so that they only work within a certain range.
Option 3
You could place reed switches or hall effect sensors in each ball, with a magnet. Place a niece of metal behind the sensor so that the magnet is drawn to it. The only issue with this is that either the magnet in each ball will not only affect its neighbour but also itself. So they will need to be spaced and also the right strength of magnet.
Option 4
A combination of option 2 and 3 would be to surround a IR receiver setup with a piece of metal disk(like a washer) and also a reed switch or hall effect sensor, and then have a ball with a magnet and a IR transmitter by a magnet, so that when the two are pulled together the transmitter and receiver match up in the same view and are able to tell them that they are connected, you would thus have each ball with a set, one to receive placed on one side and another on the opposite side to transmit.