From 702b6630d9b01e47663cd521fcc09bc574fecd7b Mon Sep 17 00:00:00 2001 From: Adam Wyatt Date: Sat, 18 Oct 2025 14:38:40 +0800 Subject: [PATCH 1/3] feat: Calibration Mode so liboard can receive and edit threshold values --- examples/serial_binboard/serial_binboard.ino | 82 +++++++++++++++----- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/examples/serial_binboard/serial_binboard.ino b/examples/serial_binboard/serial_binboard.ino index 591110c..8336267 100644 --- a/examples/serial_binboard/serial_binboard.ino +++ b/examples/serial_binboard/serial_binboard.ino @@ -16,29 +16,29 @@ #include "LiBoard.h" // Option 1 - Global Threshold (Applies to all photoresistors) -const unsigned short THRESHOLD = 100; +unsigned short THRESHOLD = 100; // Option 2 - Per-square threshold (Applies to each individual photoresistor) -/* -unsigned short THRESHOLD[64] = { - // A1..H1 - 150, 150, 150, 150, 150, 150, 150, 150, - // A2..H2 - 150, 150, 150, 150, 150, 150, 150, 150, - // A3..H3 - 100, 100, 100, 100, 100, 100, 100, 100, - // A4..H4 - 100, 100, 100, 100, 100, 100, 100, 100, - // A5..H5 - 600, 600, 600, 600, 600, 600, 600, 600, - // A6..H6 - 600, 600, 600, 600, 600, 600, 600, 600, - // A7..H7 - 300, 300, 300, 300, 300, 300, 300, 300, - // A8..H8 - 300, 300, 300, 300, 300, 300, 300, 300 -}; -*/ +//unsigned short THRESHOLD[64] = { + // A1-A8 +// 387, 398, 424, 493, 361, 353, 501, 309, + // B1-B8 +// 416, 422, 412, 417, 483, 426, 353, 419, + // C1-C8 +// 288, 266, 297, 229, 298, 267, 269, 309, + // D1-D8 +// 311, 328, 287, 300, 290, 313, 255, 438, + // E1-E8 +// 675, 708, 724, 717, 706, 690, 783, 729, + // F1-F8 +// 708, 707, 707, 686, 719, 722, 710, 662, + // G1-G8 +// 555, 573, 590, 659, 581, 580, 643, 650, + // H1-H8 +// 504, 474, 490, 643, 623, 651, 526, 647, +//}; + +bool calibrating = false; LiBoard board = LiBoard(); unsigned long long lastBinBoard = 0; @@ -49,12 +49,52 @@ void writeBinaryBoard(unsigned long long binBoard) { Serial.write((unsigned char)(binBoard>>(8*(7-i)))); } +// print a single CSV snapshot of raw ADC values (A1..H8 in LiBoard order) +void printRawSnapshotCSV() { + board.getData(); // fills board.values[0..63] + for (int i = 0; i < 64; ++i) { + Serial.print(board.values[i]); + if (i < 63) Serial.print(','); + } + Serial.println(); +} + void setup() { delay(3000); Serial.begin(9600); } void loop() { + if (Serial.available()) { + int c = Serial.read(); + // if host sends '?', reply with one CSV line of raw ADC values + if (c == '?') { + printRawSnapshotCSV(); + } + + if (c == 'c') { + calibrating = true; + Serial.println("Calibration Mode Activated!"); + } + + while (calibrating) { + if (Serial.available()){ + int p = Serial.peek(); + if (isDigit(p)) { + int newValue = Serial.parseInt(); + THRESHOLD = newValue; + Serial.print("THRESHOLD is now: "); + Serial.println(THRESHOLD); + calibrating = false; + } + // eat non-numeric to avoid getting stuck + else { + Serial.read(); + } + } + } + } + currentBinBoard = board.getBinaryBoard(THRESHOLD); if (currentBinBoard != lastBinBoard) { writeBinaryBoard(currentBinBoard); From e16c8de34c77b5301c942c091b6058253b98d8db Mon Sep 17 00:00:00 2001 From: Adam Wyatt Date: Sun, 19 Oct 2025 08:39:38 +0800 Subject: [PATCH 2/3] feat: add calibration mode and correct wrong labels --- examples/serial_binboard/serial_binboard.ino | 78 +++++++++++++------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/examples/serial_binboard/serial_binboard.ino b/examples/serial_binboard/serial_binboard.ino index 8336267..d5bc9d5 100644 --- a/examples/serial_binboard/serial_binboard.ino +++ b/examples/serial_binboard/serial_binboard.ino @@ -14,29 +14,36 @@ * along with this program. If not, see . */ #include "LiBoard.h" +#include +// !!! choose mode: 1 = global single value, 0 = per-square array !!! +#define USE_GLOBAL_THRESHOLD 0 + +#if USE_GLOBAL_THRESHOLD // Option 1 - Global Threshold (Applies to all photoresistors) unsigned short THRESHOLD = 100; +#else // Option 2 - Per-square threshold (Applies to each individual photoresistor) -//unsigned short THRESHOLD[64] = { - // A1-A8 -// 387, 398, 424, 493, 361, 353, 501, 309, - // B1-B8 -// 416, 422, 412, 417, 483, 426, 353, 419, - // C1-C8 -// 288, 266, 297, 229, 298, 267, 269, 309, - // D1-D8 -// 311, 328, 287, 300, 290, 313, 255, 438, - // E1-E8 -// 675, 708, 724, 717, 706, 690, 783, 729, - // F1-F8 -// 708, 707, 707, 686, 719, 722, 710, 662, - // G1-G8 -// 555, 573, 590, 659, 581, 580, 643, 650, - // H1-H8 -// 504, 474, 490, 643, 623, 651, 526, 647, -//}; +unsigned short THRESHOLD[64] = { + // A1-H1 + 10, 10, 10, 10, 361, 353, 501, 309, + // A2-H2 + 416, 422, 412, 417, 483, 426, 353, 419, + // A3-H3 + 288, 266, 297, 229, 298, 267, 269, 309, + // A4-H4 + 311, 328, 287, 300, 290, 313, 255, 438, + // A5-H5 + 675, 708, 724, 717, 706, 690, 783, 729, + // A6-H6 + 708, 707, 707, 686, 719, 722, 710, 662, + // A7-H7 + 555, 573, 590, 659, 581, 580, 643, 650, + // A8-H8 + 504, 474, 490, 643, 623, 651, 526, 10, +}; +#endif bool calibrating = false; @@ -65,6 +72,7 @@ void setup() { } void loop() { + // Serial Monitor arguements if (Serial.available()) { int c = Serial.read(); // if host sends '?', reply with one CSV line of raw ADC values @@ -72,6 +80,7 @@ void loop() { printRawSnapshotCSV(); } + // Calibration Mode for editing THRESHOLD if (c == 'c') { calibrating = true; Serial.println("Calibration Mode Activated!"); @@ -79,22 +88,35 @@ void loop() { while (calibrating) { if (Serial.available()){ - int p = Serial.peek(); - if (isDigit(p)) { - int newValue = Serial.parseInt(); - THRESHOLD = newValue; - Serial.print("THRESHOLD is now: "); - Serial.println(THRESHOLD); - calibrating = false; + // read a whole line the user pasted + String line = Serial.readStringUntil('\n'); + line.trim(); + if (line.length() == 0) continue; + +#if USE_GLOBAL_THRESHOLD + // Global Threshold Calibration + long newThreshold = line.toInt(); + THRESHOLD = (unsigned short)newThreshold; +#else + // Per-Square Calibration + char buf[1024]; + line.toCharArray(buf, 1024); + char* tok = strtok(buf, ","); + + int i = 0; + while (tok && i < 64) { + THRESHOLD[i++] = (unsigned short)atoi(tok); + tok = strtok(nullptr, ","); } +#endif + calibrating = false; // eat non-numeric to avoid getting stuck - else { - Serial.read(); - } + while (Serial.available()) Serial.read(); } } } + // Bitboard logic for normal use currentBinBoard = board.getBinaryBoard(THRESHOLD); if (currentBinBoard != lastBinBoard) { writeBinaryBoard(currentBinBoard); From 953754845b6c806b1bde18af9061ab78e358012c Mon Sep 17 00:00:00 2001 From: Adam Wyatt <110982014+Ay1tsMe@users.noreply.github.com> Date: Sun, 19 Oct 2025 10:35:21 +0800 Subject: [PATCH 3/3] Update threshold values for photoresistors --- examples/serial_binboard/serial_binboard.ino | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/serial_binboard/serial_binboard.ino b/examples/serial_binboard/serial_binboard.ino index d5bc9d5..4d7eeb6 100644 --- a/examples/serial_binboard/serial_binboard.ino +++ b/examples/serial_binboard/serial_binboard.ino @@ -26,22 +26,22 @@ unsigned short THRESHOLD = 100; #else // Option 2 - Per-square threshold (Applies to each individual photoresistor) unsigned short THRESHOLD[64] = { - // A1-H1 - 10, 10, 10, 10, 361, 353, 501, 309, - // A2-H2 - 416, 422, 412, 417, 483, 426, 353, 419, - // A3-H3 - 288, 266, 297, 229, 298, 267, 269, 309, - // A4-H4 - 311, 328, 287, 300, 290, 313, 255, 438, - // A5-H5 - 675, 708, 724, 717, 706, 690, 783, 729, - // A6-H6 - 708, 707, 707, 686, 719, 722, 710, 662, - // A7-H7 - 555, 573, 590, 659, 581, 580, 643, 650, - // A8-H8 - 504, 474, 490, 643, 623, 651, 526, 10, + // A1..H1 + 150, 150, 150, 150, 150, 150, 150, 150, + // A2..H2 + 150, 150, 150, 150, 150, 150, 150, 150, + // A3..H3 + 100, 100, 100, 100, 100, 100, 100, 100, + // A4..H4 + 100, 100, 100, 100, 100, 100, 100, 100, + // A5..H5 + 600, 600, 600, 600, 600, 600, 600, 600, + // A6..H6 + 600, 600, 600, 600, 600, 600, 600, 600, + // A7..H7 + 300, 300, 300, 300, 300, 300, 300, 300, + // A8..H8 + 300, 300, 300, 300, 300, 300, 300, 300, }; #endif