16.- The code: the board scan

Es tracta d'anar recorrent cada columna d'una fila i fer totes les files (o al revés).

La idea és agafar una fila, posar-la a 1 (activa) i recórrer totes les columnes d'aquesta fila per mirar quines estan actives, en les que tingui una peça a sobre, la intersecció de la fila que estic mirant amb la columna per on passa el bucle, em marcarà que està activa. Quan a cabo de recòrrer totes les 8 columnes de la fila, la poso a zero i poso a 1 la següent fila per recòrren les 8 columnes, així fins que s'acavin les files que serà quan hauré recorregut tot el taulell havent fet una lectura. Això em proporcionarà una matriu d 'uns i zeros que m'indicaran on hi ha peça i on no.

És IMPORTANT comentar que el taulell només reconeix a ON HI HA PEÇA però NO QUINA PEÇA HI HA. A partir daquí la comunicació amb l'ordinador serà a través de l'enviament pel port serie de la casella de sortida i arribada en coordenades com si es fes la entrada del moviment PER TECLAT.

He fet servir lògica directa, que vol dir que un 1 és on hi ha peça i un 0 on no n'hi ha.

El codi de l'escaneig estarà format per 2 bucles, al 1er entro la fila, llavors ressegueixo columnes, acabo i passo a la següent fila.
El trocet que penjo a sota és NOMÉS la part de la lectura del taulell evidentment falta molt més codi perquè el programa sencer funcioni però el penjo perquè és una de les parts que pot ser que costi més de veure.


Se trata de ir recorriendo cada columna de una fila y hacer todas las filas (o al revés).

La idea es coger una fila, ponerla a 1 (activa) y recorrer todas las columnas de esta fila para mirar cuáles están activas, en las que tenga una pieza encima, la intersección de la fila que estoy mirando con la columna por donde pasa el bucle, me marcará que está activa. Cuando acabo de recorrer todas las 8 columnas de la fila, la pongo a cero y pongo a 1 la siguiente fila para recorrer las 8 columnas, así hasta que se acaben las filas que será cuando habré recorrido todo el tablero habiendo hecho una lectura . Esto me proporcionará una matriz de unos y ceros que me indica donde hay pieza y donde no.

Es IMPORTANTE comentar que el tablero sólo reconoce a DONDE HAY PIEZA pero NO QUÉ PIEZA HAY. A partir de aquí la comunicación con el ordenador será a través del envío por el puerto serie de lass casillas de salida y llegada en coordenadas como si se hiciera la entrada del movimiento POR TECLADO.

El código del escaneado estará formado por 2 bucles, al 1er entro la fila, entonces recorro columnas, termino y paso a la siguiente fila.
El trozo que cuelgo abajo es SOLO la parte de la lectura del tablero evidentemente falta mucho más código para que el programa entero funcione pero lo cuelgo porque es una de las partes que puede que cueste más de ver.

It's about going through each column in a row and doing all the rows (or vice versa).

The idea is to take a row, set it to 1 (active) and go through all the columns in that row to see which ones are active, in which you have a piece on top, the intersection of the row I'm looking at with the column where the loop passes, it will mark me as active. When I finish going through all 8 columns of the row, I set it to zero and set the next row to 1 to go through the 8 columns, so that until the rows are finished it will be when I have gone through the whole board having made a reading . This will provide me with an array of ones and zeros that will tell me where there is a piece and where it is not.

It is IMPORTANT to comment that the board only recognizes WHERE THERE IS A PIECE but NOT WHICH PIECE IS THERE. From here the communication with the computer will be sending trough the serial port the of exit and arrival squares in coordinates as if the entrance of the movement BY KEYBOARD was made.

The scan code will consist of 2 loops, in the 1st I enter the row, then I trace columns, I finish and move on to the next row.
The code I put below is ONLY the part of the board reading, obviously lacks much more code for the whole program to work.

This is ONLY the code to scan the board, more code will be needed to run the program of course but I think is the most dificult part to write.

//after the initialization code where 
//matrix1=first scan
//matrix 2=penultimate scan
//matrix3=last scan


bool lectura_taulell()
{
bool haCanviat=false; //bool hasChanged to know if someone has moved
for(byte fila=0; fila<4; fila++) {pinMode(rowPins[fila], INPUT_PULLUP);} //pinMode in INPUT_PULLUP to avoid use external resistors
for(byte col=0;col<8;col++) //starting column reading
{
pinMode(columnPins[col],OUTPUT);
digitalWrite(columnPins[col],LOW); //Every Col to LOW and read the value of every Row

for (byte fila=0;fila<8;fila++) //rows
{
if(!digitalRead(rowPins[fila])!=bitRead(scan1[fila],col))
{
haCanviat=true;
for (int i =0;i<8;i++)
{for(int x=0;x<8;x++)
{matriu2[i][x]=matriu1[i][x];} //matrix2=matrix1
}
}
// rewriting scan1 byte with the current board reading
bitWrite(scan1[fila],col,!digitalRead(rowPins[fila]));

        //writing the current board reading to matrix1, this will be later the matrix3
        matriu1[col][fila]=!digitalRead(rowPins[fila]);
}
digitalWrite(columnPins[col],HIGH); //reset the ports to reescan the board
pinMode(columnPins[col],INPUT);
}
return haCanviat; //move done
}

Comentaris

Entrades populars d'aquest blog

45.- Weighting the pieces

3.- Sensors II

21.- Electronic scheme (updated 26/08/2020)