2020-02-12 15:00:51 +00:00
|
|
|
const int row[8] = {
|
|
|
|
2, 7, 19, 5, 13, 18, 12, 16
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2-dimensional array of column pin numbers:
|
|
|
|
const int col[8] = {
|
|
|
|
6, 11, 10, 3, 17, 4, 8, 9
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2-dimensional array of pixels:
|
|
|
|
int pixels[8][8];
|
|
|
|
|
|
|
|
// cursor position:
|
2020-02-12 16:53:22 +00:00
|
|
|
int x = 4;
|
|
|
|
int y = 4;
|
|
|
|
int z = 0;
|
|
|
|
|
|
|
|
// up: 1
|
|
|
|
// right: 2
|
|
|
|
// down: 3
|
|
|
|
// left: 4
|
|
|
|
int cur_dir = 0;
|
|
|
|
int snake_x = 4,
|
|
|
|
snake_y = 4;
|
2020-02-12 15:00:51 +00:00
|
|
|
|
|
|
|
void setup() {
|
2020-02-12 16:53:22 +00:00
|
|
|
//Serial.begin(9600); // 9600 bps
|
|
|
|
// Enable the Z pin
|
|
|
|
pinMode(z, INPUT_PULLUP);
|
2020-02-12 15:00:51 +00:00
|
|
|
// initialize the I/O pins as outputs
|
|
|
|
// iterate over the pins:
|
|
|
|
for (int thisPin = 0; thisPin < 8; thisPin++) {
|
|
|
|
// initialize the output pins:
|
|
|
|
pinMode(col[thisPin], OUTPUT);
|
|
|
|
pinMode(row[thisPin], OUTPUT);
|
|
|
|
// take the col pins (i.e. the cathodes) high to ensure that
|
|
|
|
// the LEDS are off:
|
|
|
|
digitalWrite(col[thisPin], HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the pixel matrix:
|
|
|
|
for (int x = 0; x < 8; x++) {
|
|
|
|
for (int y = 0; y < 8; y++) {
|
|
|
|
pixels[x][y] = HIGH;
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 16:53:22 +00:00
|
|
|
|
|
|
|
/*pixels[2][3] = LOW;
|
|
|
|
pixels[3][2] = LOW;
|
|
|
|
pixels[4][2] = LOW;
|
|
|
|
pixels[4][3] = LOW;
|
|
|
|
pixels[4][4] = LOW;*/
|
|
|
|
pixels[snake_x][snake_y] = LOW;
|
2020-02-12 15:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2020-02-12 16:53:22 +00:00
|
|
|
delay(300);
|
2020-02-12 15:00:51 +00:00
|
|
|
// read input:
|
2020-02-12 16:53:22 +00:00
|
|
|
// up: 1
|
|
|
|
// right: 2
|
|
|
|
// down: 3
|
|
|
|
// left: 4
|
|
|
|
switch(read_sensors())
|
|
|
|
{
|
|
|
|
case 0: // iddle
|
|
|
|
// Just don't
|
|
|
|
break;
|
|
|
|
case 1: // up
|
|
|
|
// Move
|
|
|
|
pixels[snake_x][++snake_y] = LOW;
|
|
|
|
break;
|
|
|
|
case 2: // right
|
|
|
|
// Move
|
|
|
|
pixels[++snake_x][snake_y] = LOW;
|
|
|
|
break;
|
|
|
|
case 3: // down
|
|
|
|
// Move
|
|
|
|
pixels[snake_x][--snake_y] = LOW;
|
|
|
|
break;
|
|
|
|
case 4: // left
|
|
|
|
// Move
|
|
|
|
pixels[--snake_x][snake_y] = LOW;
|
|
|
|
break;
|
|
|
|
}
|
2020-02-12 15:00:51 +00:00
|
|
|
|
|
|
|
// draw the screen:
|
2020-02-12 16:53:22 +00:00
|
|
|
refresh_screen();
|
2020-02-12 15:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 16:53:22 +00:00
|
|
|
// iddle: 0
|
|
|
|
// up: 1
|
|
|
|
// right: 2
|
|
|
|
// down: 3
|
|
|
|
// left: 4
|
|
|
|
int read_sensors() {
|
|
|
|
int clicked = digitalRead(z); // if 0, then clicked
|
|
|
|
x = 7 - map(analogRead(A0), 0, 1021, 0, 7);
|
|
|
|
Serial.print("Just read x=: ");
|
|
|
|
Serial.print(x);
|
|
|
|
Serial.print("\n");
|
|
|
|
y = map(analogRead(A1), 0, 1021, 0, 7)+1;
|
|
|
|
Serial.print("Just read y=: ");
|
|
|
|
Serial.print(y);
|
|
|
|
Serial.print("\n");
|
|
|
|
|
|
|
|
if (x == 4 && y == 4) {
|
|
|
|
Serial.print("We're iddle\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (abs(4-x) > abs(4-y)) // X
|
|
|
|
{
|
|
|
|
Serial.print("Let's consider x\n");
|
|
|
|
if (x < 4) {
|
|
|
|
Serial.print("Move right\n");
|
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
Serial.print("Move left\n");
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.print("Let's consider y\n");
|
|
|
|
if (y > 4) {
|
|
|
|
Serial.print("Move up\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
Serial.print("Move down\n");
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 15:00:51 +00:00
|
|
|
// set the new pixel position low so that the LED will turn on
|
|
|
|
// in the next screen refresh:
|
2020-02-12 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*bool to_right() {
|
|
|
|
// read the sensors for X
|
|
|
|
x = 7 - map(analogRead(A0), 0, 1021, 0, 7);
|
|
|
|
Serial.print("rX is: ");
|
|
|
|
Serial.print(x);
|
|
|
|
Serial.print("\n");
|
|
|
|
if ((x > 4 && cur_dir != 4) || // user inputs right and it isn't currently going left
|
|
|
|
(cur_dir == 2 && (x == 4 && y == 4)) // user has no input and cur_dir is set
|
|
|
|
) {
|
|
|
|
cur_dir = 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-12 15:00:51 +00:00
|
|
|
|
2020-02-12 16:53:22 +00:00
|
|
|
bool to_left() {
|
|
|
|
// read the sensors for X
|
|
|
|
x = 7 - map(analogRead(A0), 0, 1021, 0, 7);
|
|
|
|
Serial.print("lX is: ");
|
|
|
|
Serial.print(x);
|
|
|
|
Serial.print("\n");
|
|
|
|
if ((x < 4 && cur_dir != 2) || // user inputs right and it isn't currently going left
|
|
|
|
(cur_dir == 4 && (x == 4 && y == 4)) // user has no input and cur_left is set
|
|
|
|
) {
|
|
|
|
cur_dir = 4;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-02-12 15:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 16:53:22 +00:00
|
|
|
void blink(int x, int y)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
void refresh_screen() {
|
2020-02-12 15:00:51 +00:00
|
|
|
// iterate over the rows (anodes):
|
|
|
|
for (int thisRow = 0; thisRow < 8; thisRow++) {
|
|
|
|
// take the row pin (anode) high:
|
|
|
|
digitalWrite(row[thisRow], HIGH);
|
|
|
|
// iterate over the cols (cathodes):
|
|
|
|
for (int thisCol = 0; thisCol < 8; thisCol++) {
|
|
|
|
// get the state of the current pixel;
|
|
|
|
int thisPixel = pixels[thisRow][thisCol];
|
|
|
|
// when the row is HIGH and the col is LOW,
|
|
|
|
// the LED where they meet turns on:
|
|
|
|
digitalWrite(col[thisCol], thisPixel);
|
|
|
|
// turn the pixel off:
|
|
|
|
if (thisPixel == LOW) {
|
|
|
|
digitalWrite(col[thisCol], HIGH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// take the row pin low to turn off the whole row:
|
|
|
|
digitalWrite(row[thisRow], LOW);
|
|
|
|
}
|
|
|
|
}
|