Read sensors is now done.
First steps on moves were taken as well
This commit is contained in:
parent
44cf7ec231
commit
7375e8baca
142
snake.ino
142
snake.ino
@ -11,10 +11,22 @@ const int col[8] = {
|
||||
int pixels[8][8];
|
||||
|
||||
// cursor position:
|
||||
int x = 5;
|
||||
int y = 5;
|
||||
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++) {
|
||||
@ -32,29 +44,133 @@ void setup() {
|
||||
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);
|
||||
// read input:
|
||||
readSensors();
|
||||
// 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;
|
||||
}
|
||||
|
||||
// draw the screen:
|
||||
refreshScreen();
|
||||
refresh_screen();
|
||||
}
|
||||
|
||||
void readSensors() {
|
||||
// turn off the last position:
|
||||
pixels[x][y] = HIGH;
|
||||
// read the sensors for X and Y values:
|
||||
x = 7 - map(analogRead(A0), 0, 1022, 0, 7);
|
||||
y = map(analogRead(A1), 0, 1022, 0, 7);
|
||||
// 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:
|
||||
pixels[x][y] = LOW;
|
||||
|
||||
}
|
||||
|
||||
void refreshScreen() {
|
||||
/*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:
|
||||
|
Loading…
Reference in New Issue
Block a user