Is using the Arduino Uno mandatory ? You better use another Arduino board.
I did a simulation test in Tinkercad.com with an Arduino Uno and 4x4 matrix of LDR without and with diodes.
Without diodes:
With one column 5V and the other columns open.
All LDRs dark:
54, 54, 54, 54,
54, 54, 54, 54,
54, 54, 54, 54,
54, 54, 54, 54,
One LDR gets full light:
963, 88, 88, 88,
54, 54, 54, 54,
54, 54, 54, 54,
54, 54, 54, 54,
With one column 5V and the other columns 0V.
All LDRs dark:
47, 47, 47, 47,
47, 47, 47, 47,
47, 47, 47, 47,
47, 47, 47, 47,
One LDR gets full light:
960, 46, 46, 46,
3, 54, 54, 54,
3, 54, 54, 54,
3, 54, 54, 54,
(perhaps I have made a mistake here and the "54" should be "47" ?)
Then I tried to add diodes to every LDR:
With one column 5V and the other columns open.
All LDRs dark:
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
One LDR gets full light:
869, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
With one column 5V and the other columns 0V.
All LDRs dark:
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
One LDR gets full light:
869, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
49, 49, 49, 49,
That confirms that the diodes do help. Without diodes and setting the other columns LOW (0V), the change to the other LDRs is only a little. Perhaps that is good enough. Perhaps someone can make a calculation to get rid of it.
const int n = 4; // matrix size
// columns are digital pins
const int colPins[n] = { 8, 9, 10, 11 };
// rows are analog inputs with 10k resistor to GND.
const int rowPins[n] = { A0, A1, A2, A3 };
int ldr[n][n];
void setup()
{
Serial.begin( 9600);
Serial.println( "LDR Matrix sketch");
for( int i=0; i<n; i++)
{
// pinMode( colPins[i], INPUT);
pinMode( colPins[i], OUTPUT);
digitalWrite( colPins[i], LOW);
}
}
void loop()
{
// retrieve all LDR values.
fillLdrMatrix();
// print all LDR values.
Serial.println( "----------------");
for( int i=0; i<n; i++)
{
for( int j=0; j<n; j++)
{
Serial.print( ldr[i][j]);
Serial.print( ", ");
}
Serial.println();
}
delay( 1000);
}
void fillLdrMatrix()
{
// One colum gets 5V, then four rows can be read.
for( int i=0; i<n; i++)
{
pinMode( colPins[i], OUTPUT);
digitalWrite( colPins[i], HIGH);
for( int j=0; j<n; j++)
{
ldr[i][j] = analogRead( rowPins[j]);
}
// pinMode( colPins[i], INPUT);
pinMode( colPins[i], OUTPUT);
digitalWrite( colPins[i], LOW);
}
}