Skip to content

Commit 8179937

Browse files
authored
Merge pull request #1 from Ay1tsMe/auto-calibration
feat: Auto Calibration
2 parents 12e263c + 9537548 commit 8179937

1 file changed

Lines changed: 66 additions & 4 deletions

File tree

examples/serial_binboard/serial_binboard.ino

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
1515

1616
#include "LiBoard.h"
17+
#include <string.h>
1718

19+
// !!! choose mode: 1 = global single value, 0 = per-square array !!!
20+
#define USE_GLOBAL_THRESHOLD 0
21+
22+
#if USE_GLOBAL_THRESHOLD
1823
// Option 1 - Global Threshold (Applies to all photoresistors)
19-
const unsigned short THRESHOLD = 100;
24+
unsigned short THRESHOLD = 100;
2025

26+
#else
2127
// Option 2 - Per-square threshold (Applies to each individual photoresistor)
22-
/*
2328
unsigned short THRESHOLD[64] = {
2429
// A1..H1
2530
150, 150, 150, 150, 150, 150, 150, 150,
@@ -36,9 +41,11 @@ unsigned short THRESHOLD[64] = {
3641
// A7..H7
3742
300, 300, 300, 300, 300, 300, 300, 300,
3843
// A8..H8
39-
300, 300, 300, 300, 300, 300, 300, 300
44+
300, 300, 300, 300, 300, 300, 300, 300,
4045
};
41-
*/
46+
#endif
47+
48+
bool calibrating = false;
4249

4350
LiBoard board = LiBoard();
4451
unsigned long long lastBinBoard = 0;
@@ -49,12 +56,67 @@ void writeBinaryBoard(unsigned long long binBoard) {
4956
Serial.write((unsigned char)(binBoard>>(8*(7-i))));
5057
}
5158

59+
// print a single CSV snapshot of raw ADC values (A1..H8 in LiBoard order)
60+
void printRawSnapshotCSV() {
61+
board.getData(); // fills board.values[0..63]
62+
for (int i = 0; i < 64; ++i) {
63+
Serial.print(board.values[i]);
64+
if (i < 63) Serial.print(',');
65+
}
66+
Serial.println();
67+
}
68+
5269
void setup() {
5370
delay(3000);
5471
Serial.begin(9600);
5572
}
5673

5774
void loop() {
75+
// Serial Monitor arguements
76+
if (Serial.available()) {
77+
int c = Serial.read();
78+
// if host sends '?', reply with one CSV line of raw ADC values
79+
if (c == '?') {
80+
printRawSnapshotCSV();
81+
}
82+
83+
// Calibration Mode for editing THRESHOLD
84+
if (c == 'c') {
85+
calibrating = true;
86+
Serial.println("Calibration Mode Activated!");
87+
}
88+
89+
while (calibrating) {
90+
if (Serial.available()){
91+
// read a whole line the user pasted
92+
String line = Serial.readStringUntil('\n');
93+
line.trim();
94+
if (line.length() == 0) continue;
95+
96+
#if USE_GLOBAL_THRESHOLD
97+
// Global Threshold Calibration
98+
long newThreshold = line.toInt();
99+
THRESHOLD = (unsigned short)newThreshold;
100+
#else
101+
// Per-Square Calibration
102+
char buf[1024];
103+
line.toCharArray(buf, 1024);
104+
char* tok = strtok(buf, ",");
105+
106+
int i = 0;
107+
while (tok && i < 64) {
108+
THRESHOLD[i++] = (unsigned short)atoi(tok);
109+
tok = strtok(nullptr, ",");
110+
}
111+
#endif
112+
calibrating = false;
113+
// eat non-numeric to avoid getting stuck
114+
while (Serial.available()) Serial.read();
115+
}
116+
}
117+
}
118+
119+
// Bitboard logic for normal use
58120
currentBinBoard = board.getBinaryBoard(THRESHOLD);
59121
if (currentBinBoard != lastBinBoard) {
60122
writeBinaryBoard(currentBinBoard);

0 commit comments

Comments
 (0)