Hello,
I do a prototype of checkers board with a teensy++ 2.0, I use reed switch to detect the move of pieces.
With the IDE Arduino, I have no problem to display exactly what I want(in red in the Arduino code), so the move like "33-28 ".
But when I use a python script, nothing appears in my Terminal.
I tried to display more data than my move. And in this case, the display is not in live, because I need to move another piece to display the move previous, but something is display.
So I don't understand how to get data in live and how just get my move.
Here is my simplified code :
Arduino :
#define rows 10 // define number of rows
#define columns 10 //define number of columns
int rowpins[rows] = {0,1,10,11,12,13,14,15,16,17} ;
int columnpins[columns] = {45,44,43,42,41,40,39,38,20,21} ;
int currentState[rows][columns]; // array for current state during pick
int previousState[rows][columns]; // array for comparing state during pick
int reading[rows][columns]; // array for pin read value
int damier[rows][columns] = {{0},{0}};
int turnColor; // 0=White 1=Black
String move;
int moveOk;
int colorPlayer;
int longueur=0;
int nb_rafle=0;
unsigned long debounceDelay = 50;
unsigned long lastDebounceTime[rows][columns]; // array for saving debounce times
enum{VIDE,B,N,DB,DN};
const String positionArray [rows][columns] = {{"", "1", "", "2", "", "3", "", "4", "", "5"},
{"6", "", "7", "", "8", "", "9", "", "10", ""},
{"", "11", "", "12", "", "13", "", "14", "", "15"},
{"16", "", "17", "", "18", "", "19", "", "20", ""},
{"", "21", "", "22", "", "23", "", "24", "", "25"},
{"26", "", "27", "", "28", "", "29", "", "30", ""},
{"", "31", "", "32", "", "33", "", "34", "", "35"},
{"36", "", "37", "", "38", "", "39", "", "40", ""},
{"", "41", "", "42", "", "43", "", "44", "", "45"},
{"46", "", "47", "", "48", "", "49", "", "50", ""}}; // set array position names
int colorArray [10][10]={{0},{0}};
typedef struct Direc Direc;
struct Direc{
int i,j;
};
int cases=0;
int max_longueur=0;
int max_cases=0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600); // Start serial comms
//delay (2000);
initialise (); // initialise function
}
void loop() {
// put your main code here, to run repeatedly:
//Serial.print("Hello");
searchMove();
moveVerification();
}
void initialise () {
[color=limegreen]Serial.write ("Board initialising");[/color]
for (int i = 0; i < rows; i++) { // Set rows to LOW INPUT
pinMode (rowpins[i], INPUT);
digitalWrite (rowpins[i], LOW);
}
for (int j = 0; j < columns; j++) { // set columns to INPUT with PULLUP
pinMode (columnpins[j], INPUT);
digitalWrite (columnpins[j], HIGH);
}
for (int i = 0; i < rows; i++) {
pinMode (rowpins[i], OUTPUT); // set row to LOW OUTPUT
for (int j = 0; j < columns; j++) {
reading[i][j] = digitalRead (columnpins[j]); // read board positions and set for initialise
currentState[i][j] = reading[i][j];
previousState[i][j] = reading[i][j];
}
pinMode (rowpins[i], INPUT); // set row back to INPUT
}
[color=limegreen]Serial.println (" ");
Serial.println ("Current Layout:");
Serial.println (" ");[/color]
for (int a = 0; a < rows; a++) {
for (int b = 0; b < columns; b++) {
if (reading[a][b] == 0) {
if (a < 4) {
colorArray[a][b] = N;
} else if (a > 5) {
colorArray[a][b] = B;
} else {
colorArray[a][b] = VIDE;
}
}
}
}
colorPlayer = B;
}
void setCases(int a){
cases=a;
}
int getCases(){
return cases;
}
int findMove(int i, int j, int colorPlayer) {
/*Search all enables moves*/
}
int findMoveDame(int i, int j, int colorPlayer) {
/*Search all enables moves for draughts*/
}
int trouver_rafle(int i, int j, int carte[][10],int longueur){
/*Search the biggest path with piece*/
}
int trouver_rafle_dame(int i, int j, int carte[][10],int longueur){
/*Search the biggest path with draughts*/
}
void searchMove() {
/*Search if the piece played is a good move*/
}
void moveVerification() {
if (moveOk == 1) {
[color=red]Serial.print(move);
Serial.print(" ");[/color]
moveOk = 0;
setCases(0);
longueur = 0;
max_longueur=0;
max_cases=0;
nb_rafle=0;
if (colorPlayer == B) {
colorPlayer = N;
} else {
colorPlayer = B;
}
}
}
Python :
import serial
serial_port = '/dev/cu.usbmodem12341';
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
write_to_file_path = "test2.pdn";
output_file = open(write_to_file_path, "w+");
ser = serial.Serial(serial_port, baud_rate)
while True:
print("test");
line = ser.readline();
line = line.decode("utf-8") #ser.readline returns a binary, convert to string
print(line);
output_file.write(line);
Thanks a lot for your help.
Quentin