#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int backlightState = HIGH;
int e = 0; //used for erasing pacman
int i = 0; //used for positioning pacman
int x; //food for line 0
byte pacman[8] = { //pacman
B00000,
B01110,
B11011,
B11110,
B11100,
B11111,
B01110,
B00000};
byte pacman_food[8] = { //pacman food
B00000,
B00000,
B00100,
B01110,
B01110,
B00100,
B00000};
byte blank[8] = { //blank pixels
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000 };
byte reversed_pacman[8] = //reversed pacman
{
B00000,
B01110,
B11011,
B01111,
B00111,
B11111,
B01110,
B00000 };
void move() //moving pacman to the right
{
lcd.setCursor(i,0);
lcd.write (3);
i=i+1;
delay(400);
lcd.setCursor(e,0);
lcd.write(2);
e=e+1;
delay(50);
}
void move2() //moving pacman to the left
{
if(i>0){
lcd.setCursor(i,0);
lcd.write(4);
i=i-1;
delay(400);
lcd.setCursor(e,0);
lcd.write(2);
e=e-1;}
}
void food() //filling line 0 with pacman food
{
for (x=0;x<16;x++)
{
lcd.setCursor(x,0);
lcd.write(1);
}
}
void setup() {
Serial.begin(9600);
pinMode(A1, INPUT);
lcd.begin(16, 2);
lcd.createChar(1, pacman_food);
lcd.createChar(2, blank);
lcd.createChar(3, pacman);
lcd.createChar(4, reversed_pacman);
food();
}
void loop() {
lcd.setCursor(0,0);
int timi=analogRead(A1);
Serial.println(timi);
if(timi>512){
move();}
if(timi<512){
move2();}
if(i>=16){
tone(8, 1000);
delay(50);
tone(8, 100);
}}