#include <LiquidCrystal.h>
#include <ezButton.h>
ezButton buttonL(A3);
ezButton buttonR(A2);
ezButton buttonU(A1);
ezButton buttonD(A0);
int ver = A0;
int hor = A1;
int V;
int H;
int i = 0;
int d = 1;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int x = 0; //0-19
int xm = x;
int xmm = xm;
int y = 0; //0-3
int ym = y;
int ymm = ym;
int dir = 0; //0=Left, 1= Right, 2=Up, 3=Down
int dir1;
//SnakePixels
byte snakeBody[8] = {
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b00000
};
byte snakeRight[8] = {
0b00000,
0b11111,
0b11001,
0b11111,
0b11111,
0b11001,
0b11111,
0b00000
};
byte snakeLeft[8] = {
0b00000,
0b11111,
0b10011,
0b11111,
0b11111,
0b10011,
0b11111,
0b00000
};
byte snakeUp[8] = {
0b00000,
0b11111,
0b10101,
0b10101,
0b11111,
0b11111,
0b11111,
0b00000
};
byte snakeDown[8] = {
0b00000,
0b11111,
0b11111,
0b11111,
0b10101,
0b10101,
0b11111,
0b00000
};
void snake(int x, int row, int column){
lcd.setCursor(row,column);
lcd.write((byte)x);
}
void posCheck(){
i = 200;
while(i != 0){
i--;
delay(1);
conCheck();
}
if(dir==0){
x++;
}else if(dir==1){
x--;
}else if(dir==2){
y++;
}else if(dir==3){
y--;
}
if(dir==0 & x==20){
x = 0;
}
if(dir==1 & x==-1){
x = 19;
}
if(dir==2 & y==4){
y = 0;
}
if(dir==3 & y==-1){
y = 3;
}
}
void conCheck(){ //0=Left, 1= Right, 2=Up, 3=Down
V=analogRead(ver);
H=analogRead(hor);
if (V==512 && H==512){ //middle
}
else if (V==512 && H==1023){ //left
x--;
dir = 1;
d = 2;
delay(30);
}
else if (V==512 && H==0){ //right
x++;
dir = 0;
d = 1;
delay(30);
}
else if (V==1023 && H==512){ //up
y--;
dir = 3;
d = 3;
delay(30);
}
else if (V==0 && H==512){ //down
y++;
dir = 2;
d = 4;
delay(30);
}
}
void setup() {
lcd.begin(20, 4);
Serial.begin(9600);
lcd.createChar(0, snakeBody);
lcd.createChar(1, snakeRight);
lcd.createChar(2, snakeLeft);
lcd.createChar(3, snakeUp);
lcd.createChar(4, snakeDown);
pinMode(ver, INPUT);
pinMode(hor, INPUT);
}
void loop() {
posCheck();
lcd.clear();
snake(d, x, y);
}