Welcome! Share code as fast as possible.
- Use the language picker to change the language manually.
- The previous link will get modified. You can then share this new link with others.
- Visit
https://code-dump.vercel.app/<extension>
- For example, to create a link which for a JavaScript code you will visit
https://code-dump.vercel.app/js
Any link generated does not have an expiry.
GitHub
#include "BoardState.h"
#include "ChessGantry.h"
#include "LED8x8Array.h"
#include "Keypad.h"
#include "LimitSwitch.h"
#include "Magnet.h"
#include "StepperMotor.h"
// === Pin Definitions ===
const int X_STEP_PIN = 5;
const int X_DIR_PIN = 2;
const int X_EN_PIN = 8;
const int Y_STEP_PIN = 6;
const int Y_DIR_PIN = 3;
const int Y_EN_PIN = 9;
const int MAGNET_PIN = 26;
const uint8_t KEYPAD_ROWS[4] = {30, 31, 32, 33};
const uint8_t KEYPAD_COLS[3] = {34, 35, 36};
const int SHIFT_DATA_PIN = 22;
const int SHIFT_CLOCK_PIN = 23;
const int SHIFT_LATCH_PIN = 24;
// === Gantry & Board Setup ===
const int STEPS_PER_INCH = 200;
const int GANTRY_X_MAX_STEPS = 3200;
const int GANTRY_Y_MAX_STEPS = 3200;
const float SQUARE_SIZE_IN = 2.0;
const float BOARD_ORIGIN_X = 1.0;
const float BOARD_ORIGIN_Y = 1.0;
const int BOARD_ROWS = 8;
const int BOARD_COLS = 8;
// === Component Instances ===
StepperMotor x_motor(X_STEP_PIN, X_DIR_PIN, X_EN_PIN);
StepperMotor y_motor(Y_STEP_PIN, Y_DIR_PIN, Y_EN_PIN);
Magnet magnet(MAGNET_PIN);
ChessGantry gantry(x_motor, y_motor, magnet,
STEPS_PER_INCH,
GANTRY_X_MAX_STEPS, GANTRY_Y_MAX_STEPS,
SQUARE_SIZE_IN,
BOARD_ROWS, BOARD_COLS,
BOARD_ORIGIN_X, BOARD_ORIGIN_Y);
BoardState board;
Keypad keypad(KEYPAD_ROWS, KEYPAD_COLS);
LED8x8Array led(SHIFT_DATA_PIN, SHIFT_CLOCK_PIN, SHIFT_LATCH_PIN);
void setup() {
Serial.begin(9600);
x_motor.begin();
y_motor.begin();
magnet.begin();
keypad.begin();
led.begin();
board.reset_board();
gantry.go_to_origin();
Serial.println("Check, Mate, Vision started. Awaiting serial input for first player.");
}
// === Helper function to parse input like "7,6 7,4" into 4 integers ===
bool parse_move(const String& input, int& r1, int& c1, int& r2, int& c2) {
int first_comma = input.indexOf(',');
int space = input.indexOf(' ');
int second_comma = input.lastIndexOf(',');
if (first_comma == -1 || space == -1 || second_comma == -1 || second_comma <= space)
return false;
r1 = input.substring(0, first_comma).toInt();
c1 = input.substring(first_comma + 1, space).toInt();
r2 = input.substring(space + 1, second_comma).toInt();
c2 = input.substring(second_comma + 1).toInt();
return r1 < 8 && c1 < 8 && r2 < 8 && c2 < 8;
}
void loop() {
// --- Player 1 (CMV user via serial) ---
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
int r1, c1, r2, c2;
if (parse_move(input, r1, c1, r2, c2)) {
if (board.make_move(r1, c1, r2, c2)) {
gantry.take_piece_to_open_graveyard(r2, c2);
}
gantry.do_move(r1, c1, r2, c2);
} else {
Serial.println("Invalid format. Use: row1,col1 row2,col2");
return;
}
}
// --- Player 2 (Human opponent via keypad) ---
Serial.println("Player 2: Enter origin square (0-7)");
int r3, c3;
keypad.wait_for_square_input(r3, c3);
Serial.println("Enter destination square (0-7)");
int r4, c4;
keypad.wait_for_square_input(r4, c4);
board.make_move(r3, c3, r4, c4);
}