// TEST ChessBoard Sensor & MUX Test
//
// For Linear Hall Sensor like 49E
// It can use to adapt Code to the Wire
// Jule 2023 - experiment for aboid H-Bridge
// All Magnets same polarity, White one size, Black other Size
// For Example Try with D9H3mm for White and D6H3mm for Black or square magnet
// See weight of the piece, gap with Electromagnet , Board Surface, Ext Diameter of the Piece
// Output: The value of each hall sensor on a Matrix.
//
#include <Wire.h>
#include "LedControl.h" // Added for Matrix Led based on MAX7219
LedControl lc=LedControl(51,52,53,1); // For A Mega DIN=51 , CLK=52, CS=53, 1 Matrix Led
// Chessboard
int sensor_record[8][8];
int sensor_status_memory[8][8];
int hall_value[8][8]; // SPK added for store each Hall value
// Multiplexer
const byte MUX_ADDR[4] = {A3, A2, A1, A0};
const byte MUX_SELECT[4] = {25, 24, 23, 22};
int MUX_OUTPUT(A8); // SPK Adapted for Linear Hall
const byte MUX_CHANNEL[16][4] = {
{0, 0, 0, 0}, // 0
{1, 0, 0, 0}, // 8
{0, 1, 0, 0}, // 4
{1, 1, 0, 0}, // 12
{0, 0, 1, 0}, // 2
{1, 0, 1, 0}, // 10
{0, 1, 1, 0}, // 6
{1, 1, 1, 0}, // 14
{0, 0, 0, 1}, // 1
{1, 0, 0, 1}, // 9
{0, 1, 0, 1}, // 5
{1, 1, 0, 1}, // 13
{0, 0, 1, 1}, // 3
{1, 0, 1, 1}, // 11
{0, 1, 1, 1}, // 7
{1, 1, 1, 1} // 15
};
// Button - Switch
const byte BUTTON_WHITE_SWITCH_MOTOR_WHITE(A11);
// **************************************** SETUP
void setup() {
Serial.begin(9600);
Serial.println("FOR TEST");
Serial.println("PRESS WHITE BUTTON");
Serial.println("+ - - - - - - - -+");
// Multiplexer
for (byte i = 0; i < 4; i++) {
pinMode(MUX_ADDR[i], OUTPUT);
digitalWrite(MUX_ADDR[i], LOW);
pinMode(MUX_SELECT[i], OUTPUT);
digitalWrite(MUX_SELECT[i], HIGH);
}
pinMode(MUX_OUTPUT, INPUT_PULLUP);
// Arcade button - Limit Switch
pinMode(BUTTON_WHITE_SWITCH_MOTOR_WHITE, INPUT_PULLUP);
// Matrix Led
lc.shutdown(0, false); // Turn on matrix Led
lc.setIntensity(0, 4); // Set Intensity of Led
lc.clearDisplay(0); // Turn off leds
}
// ***************************************** LOOP
void loop()
{
if (digitalRead(BUTTON_WHITE_SWITCH_MOTOR_WHITE) != HIGH)
{
sensor_status();
sensor_display();
delay(500);
}
}
// ************************************** DISPLAY
void sensor_display() {
lc.clearDisplay(0); // Turn off leds
Serial.println("+ - - - - - - - - -+");
for (int i = 0; i < 8; i++) {
Serial.print(' ');
Serial.print(8 - i);
Serial.print("| ");
for (int j = 0; j < 8; j++) {
Serial.print(hall_value[7-i][j]); // V4 Print the measure of Hall sensor
if (sensor_status_memory[i][j] == 1 || sensor_status_memory[i][j] == -1) {
lc.setLed(0, i, j, true); // Turn on Led where there is a piece
}
Serial.print(" ");
}
Serial.println('|');
}
Serial.println("+ - - - - - - - - -+");
Serial.println(" a b c d e f g h");
}
// ************************ STATUS
void sensor_status()
{
// Record the reed switches status
byte column = 6;
byte row = 0;
for (byte i = 0; i < 4; i++)
{
digitalWrite(MUX_SELECT[i], LOW);
for (byte j = 0; j < 16; j++)
{
for (byte k = 0; k < 4; k++)
{
digitalWrite(MUX_ADDR[k], MUX_CHANNEL[j][k]);
delay(5);
int hallMeasure = analogRead(MUX_OUTPUT);
if (hallMeasure > 540 && hallMeasure <= 740) { // 540 & 740 for Simulide Set the Value manualy after test
sensor_record[column][row] = 1;
hall_value[column][row]=hallMeasure; // SPK Added for take the Hall measure
}
else if (hallMeasure > 840 && hallMeasure <= 1100) { // 840 & 1100 for Simulide, Set the Value manualy after test
sensor_record[column][row] = -1;
hall_value[column][row]=hallMeasure; // SPK Added for take the Hall measure
}
else { // Any other Value means no piece. Set the Value manualy after test
sensor_record[column][row] = 0;
hall_value[column][row]=hallMeasure; // SPK Added for take the Hall measure
}
}
row++;
if (j == 7) {
column++;
row = 0;
}
}
for (byte l = 0; l < 4; l++) {
digitalWrite(MUX_SELECT[l], HIGH);
}
if (i == 0) column = 4;
if (i == 1) column = 2;
if (i == 2) column = 0;
row = 0;
}
for (byte i = 0; i < 8; i++) {
for (byte j = 0; j < 8; j++) {
sensor_status_memory[7-i][j] = sensor_record[i][j];
}
}
}