#include <Arduino.h>
#include <Wire.h> // Include the Wire library for I2C communication
#define S0_PIN 2 // Define the S0 pin for multiplexer 1
#define S1_PIN 3 // Define the S1 pin for multiplexer 1
#define S2_PIN 4 // Define the S2 pin for multiplexer 1
#define S3_PIN 5 // Define the S3 pin for multiplexer 1
#define SIG_PIN 0 // Define the SIG pin for multiplexer 1 (analog pin)
#define BUTTON_PIN 10 // Define the button pin
#define thresholdValue 128 // Define the SIG pin for multiplexer 1 (analog pin)
bool gameStarted = false; // Variable to track the game state
void setup() {
Serial.begin(9600); // Initialize serial communication
Wire.begin(); // Initialize I2C communication
pinMode(S0_PIN, OUTPUT);
pinMode(S1_PIN, OUTPUT);
pinMode(S2_PIN, OUTPUT);
pinMode(S3_PIN, OUTPUT);
}
void selectChannel(int channel) {
digitalWrite(S0_PIN, channel & 0x01);
digitalWrite(S1_PIN, (channel >> 1) & 0x01);
digitalWrite(S2_PIN, (channel >> 2) & 0x01);
digitalWrite(S3_PIN, (channel >> 3) & 0x01);
}
void loop() {
if (!gameStarted) {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
// Button is pressed, start the game
gameStarted = true;
Serial.println("Game started!");
// Add any other game initialization logic here
}
} else {
for (int channel = 0; channel < 64; channel++) {
selectChannel(channel); // Select the channel
int sensorValue = analogRead(SIG_PIN); // Read the sensor value
if( sensorValue < 1000) {
Serial.print(channel);
Serial.print(" ");
Serial.println(sensorValue);
}
// You can add your logic here to detect chess piece movement and print notation
// For example, if a piece is detected:
// if (sensorValue > thresholdValue) {
// // Print chess notation for the moved piece
// Serial.print("Piece moved from ");
// Serial.print(getChessSquare(channel)); // Convert channel to chess square notation
// Serial.println(" to ");
// // Wait for some time to avoid duplicate readings
// delay(1000);
// }
}
}
}
String getChessSquare(int channel) {
// Define your chessboard layout here
// Assuming an 8x8 chessboard with rows labeled from 'a' to 'h' and columns from 1 to 8
char column = 'a' + (channel % 8); // Convert channel to column (a to h)
int row = 8 - (channel / 8); // Convert channel to row (1 to 8)
// Create the chess square notation
String square = "";
square += column;
square += String(row);
return square;
}
Loading
cd74hc4067
cd74hc4067
Loading
cd74hc4067
cd74hc4067
Loading
cd74hc4067
cd74hc4067
Loading
cd74hc4067
cd74hc4067