arduino-snake/snake.ino
2020-02-12 17:36:02 +00:00

243 lines
5.1 KiB
C++

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:
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;
void setup() {
//Serial.begin(9600); // 9600 bps
// Enable the Z pin
pinMode(z, INPUT_PULLUP);
// 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;
}
}
/*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;
}
void loop() {
delay(300);
if (!move_snake(read_sensors())) {
switch(cur_dir)
{
case 0: // iddle
// Just don't
break;
case 1: // up
// Move
if (snake_y+1 <= 7) {
pixels[snake_x][++snake_y] = LOW;
}
break;
case 2: // right
// Move
if (snake_x+1 <= 7) {
pixels[++snake_x][snake_y] = LOW;
}
break;
case 3: // down
// Move
if (snake_y-1 >= 0) {
pixels[snake_x][--snake_y] = LOW;
}
break;
case 4: // left
// Move
if (snake_x-1 >= 0) {
pixels[--snake_x][snake_y] = LOW;
}
break;
}
}
// draw the screen:
refresh_screen();
}
// true if it has moved, false otherwise
bool move_snake(int dir) {
// read input:
switch(dir)
{
case 0: // iddle
// Just don't
break;
case 1: // up
// Move
if (cur_dir != 3) {
pixels[snake_x][++snake_y] = LOW;
cur_dir = 1;
return true;
}
break;
case 2: // right
// Move
if (cur_dir != 4) {
pixels[++snake_x][snake_y] = LOW;
cur_dir = 2;
return true;
}
break;
case 3: // down
// Move
if (cur_dir != 1) {
pixels[snake_x][--snake_y] = LOW;
cur_dir = 3;
return true;
}
break;
case 4: // left
// Move
if (cur_dir != 2) {
pixels[--snake_x][snake_y] = LOW;
cur_dir = 4;
return true;
}
break;
}
return false;
}
// 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;
}
}
// set the new pixel position low so that the LED will turn on
// in the next screen refresh:
}
/*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;
}
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;
}
void blink(int x, int y)
{
}*/
void refresh_screen() {
// 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);
}
}