126k views
5 votes
Write a C program and build a circuit to implement the following detection and display pattern (make use of the Line Tracker and the LEDs). We are using yellow, green, and red LEDs as detectors. Assume we are using a surface similar to a chess board.

- If there is no motion of the line tracker, all LEDs are off.
- If move from white to white or black to black, all LEDS are off.
- If we move the line tracker from a white square to a black square, the green LED turns on (two other LEDs are off).
- If we move the line tracker from a black square to a white square, the red LED turns on (two other LEDs are off).
- As soon as 5 moves get completed, the yellow LED blinks 3 times (two other LEDs are off).

User Span
by
3.8k points

1 Answer

2 votes

Answer:

see explaination

Step-by-step explanation:

// Defines for chess pieces and spaces

#define BORDER 255

#define EMPTY 0

#define BLACK_PAWN 1

#define BLACK_KNIGHT 3

#define BLACK_BISHOP 4

#define BLACK_ROOK 5

#define BLACK_QUEEN 9

#define BLACK_KING 16

#define WHITE_PAWN 129

#define WHITE_KNIGHT 131

#define WHITE_BISHOP 132

#define WHITE_ROOK 133

#define WHITE_QUEEN 137

#define WHITE_KING 144

byte piecesVal[12][12]=

{{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},

{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},

{255, 255, 5, 3, 4, 9, 16, 4, 3, 5, 255, 255},

{255, 255, 1, 1, 1, 1, 1, 1, 1, 1, 255, 255},

{255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255},

{255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255},

{255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255},

{255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255},

{255, 255, 129, 129, 129, 129, 129, 129, 129, 129, 255, 255},

{255, 255, 133, 131, 132, 137, 144, 132, 131, 133, 255, 255},

{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},

{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}};

if ((piecesValCur[y][x]) == BORDER) // We are off the board in one comparison

if ((y < 0) || (y > 7) || (x < 0) || (x > 7)) // We are off the board in possibly four comparisons

byte piecesCurrent[8][8] = {{1, 1, 1, 1, 1, 1, 1, 1}, // [y][x] Where current piece are located

{1, 1, 1, 1, 1, 1, 1, 1},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{0, 0, 0, 0, 0, 0, 0, 0},

{1, 1, 1, 1, 1, 1, 1, 1},

{1, 1, 1, 1, 1, 1, 1, 1}};

// Read a row of 8 Hall Effect sensors

int readLine(int row, byte piecesTemp[][8])

{

int count = 0;

piecesTemp[row][0] = !digitalRead(2);

count += piecesTemp[row][0];

piecesTemp[row][1] = !digitalRead(3);

count += piecesTemp[row][1];

piecesTemp[row][2] = !digitalRead(4);

count += piecesTemp[row][2];

piecesTemp[row][3] = !digitalRead(5);

count += piecesTemp[row][3];

piecesTemp[row][4] = !digitalRead(6);

count += piecesTemp[row][4];

piecesTemp[row][5] = !digitalRead(7);

count += piecesTemp[row][5];

piecesTemp[row][6] = !digitalRead(8);

count += piecesTemp[row][6];

piecesTemp[row][7] = !digitalRead(12); // Oddball because we skipped 9, 10

count += piecesTemp[row][7]; // for serial RX, TX, Serial2 on Teensy 3.1

return count; // return number of pieces found in this row

}

// Color an x,y square with a specific color

void colorSquare(byte x, byte y, uint32_t color, bool on)

{

if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8)) // is it a legal square ?

{

int num1 = (y << 5) + (x << 1); // same as num1 = y * 32 + x * 2;

int num2 = num1 + 1;

int num3 = (y << 5) + 31 - (x << 1); // same as num3 = (y * 32 + 32) - (x * 2) - 1;

int num4 = num3 - 1;

// Needs a short delay, otherwise intermittent errors on position

pixels.setPixelColor(num1, color); delay(1);

pixels.setPixelColor(num2, color); delay(1);

pixels.setPixelColor(num3, color); delay(1);

pixels.setPixelColor(num4, color); delay(1);

if (on) pixels.show();

}

}

// Color a chessboard with blue (for black) and red squares

void standBoard()

{

for (int y = 0; y < 4; y++)

{

for (int x = 0; x < 4; x++)

{

colorSquare(x * 2, y * 2, colors[RED], false);

colorSquare(x * 2 + 1, y * 2, colors[BLUE], false);

colorSquare(x * 2, y * 2 + 1, colors[BLUE], false);

colorSquare(x * 2 + 1, y * 2 + 1, colors[RED], false);

}

}

pixels.show();

}

void colorBoard(uint32_t color)

{

for (int i = 0; i < NUMPIXELS; i++)

{

pixels.setPixelColor(i, color);

}

pixels.show();

}

// Read all 8 lines of the Hall Effect Sensors

int readHall(byte piecesTemp[][8])

{

int count = 0;

digitalWrite(A0, LOW);

digitalWrite(A1, LOW); digitalWrite(A2, LOW); // 000

delay(1); // let 3144 settle

count += readLine(0, piecesTemp);

digitalWrite(A0, HIGH); // 001

delay(1);

count += readLine(1, piecesTemp);

digitalWrite(A0, LOW); // 010

digitalWrite(A1, HIGH);

delay(1);

count += readLine(2, piecesTemp);

digitalWrite(A0, HIGH); // 011

delay(1);

count += readLine(3, piecesTemp);

digitalWrite(A0, LOW); // 100

digitalWrite(A1, LOW);

digitalWrite(A2, HIGH);

delay(1);

count += readLine(4, piecesTemp);

digitalWrite(A0, HIGH); // 101

delay(1);

count += readLine(5, piecesTemp);

digitalWrite(A0, LOW); // 110

digitalWrite(A1, HIGH);

delay(1);

count += readLine(6, piecesTemp);

digitalWrite(A0, HIGH); // 111

delay(1);

count += readLine(7, piecesTemp);

delay(100);

return count; // return count of all pieces on the board

}

User Jason Ye
by
3.9k points