#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int i;
int j;
int clockoboi = 0; //countdown till the next movement of bullets
byte customChar[] = { //stickperson
B00110,
B00110,
B01110,
B10101,
B10101,
B01010,
B01010,
B01010
};
int up = 9; //buttons
int down = 8;
bool pos = 0; //location of the stickman
char bullets[2][15]; //the state of each bullet, '-'-exists, ' '-doesn't
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, customChar);
pinMode(up, INPUT);
pinMode(down, INPUT);
for(i = 0; i < 2; i++){ //set up bullets
for(j = 0; j < 15; j++){
bullets[i][j] = ' ';
}
}
randomSeed(analogRead(0));
}
void loop() {
if(digitalRead(up)){
pos=0;
}else if(digitalRead(down)){
pos=1;
}else{
pos=pos;
}
lcd.setCursor(0,pos); //stickfig movement
lcd.write(byte(0));
delay(200);
lcd.clear();
if(clockoboi==10){ //time to move bullets?
clockoboi=0;
if(bullets[0][pos]=='-'){ //cheks if bullets hit the stickfig
lcd.print("You lost");
delay(1000);
exit(0);
}
for(j = 0; j < 2; j++){ //bullets move forward
for(i = 0; i < 14; i++){
bullets[j][i] = bullets[j][i+1];
}
if(random(5)==1){ //a new bullet appears
bullets[j][15]='-';
}else{
bullets[j][15]=' ';
}
}
for(j = 0; j < 2; j++) //bullet display
{
lcd.setCursor(1,j);
for(i = 0; i < 16; i++)
{
lcd.print(bullets[j][i]);
}
}
}
clockoboi++;
}
I have a problem with this project, please help.
This is supposed to be a game on an lcd, operated with two buttons, you move up and down and you should avoid a rain of bullets. Although the moving works, bullets don't generate properly, in fact they don't generate at all, does somebody know how can I fix this problem and what might be causing it?