#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define start_T 1000
#define bull_T 200
#define mx 800
#define mn 200
#define mxb 12
#define mxc 5
int cnt = 0;
int bullets = mxb;
long tm, gtm;
bool game = false;
byte sqr[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
class timer{
private:
long tmr, dtmr;
bool flag;
public:
timer(){
tmr = 0;
}
void zero_t(){
flag = false;
tmr = 0;
}
void start_t(){
flag = true;
dtmr = millis() - tmr;
}
void stop_t(){
flag = false;
tmr = millis() - dtmr;
}
long get_t(){
return (flag ? millis() - dtmr : tmr);
}
};
timer tmr;
void setup() {
lcd.init();
lcd.backlight();
lcd.createChar(0, sqr);
pinMode(12, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
//общение с мишенью
pinMode(6, INPUT);
pinMode(13, OUTPUT);
pinMode(11, INPUT_PULLUP);
pinMode(14, INPUT);
attachInterrupt(0, push, FALLING);
attachInterrupt(1, pull, RISING);
digitalWrite(13, LOW);
}
void loop() {
while(digitalRead(11) && !game){}
game = true;
for(int i = 3; i >= 0; i--){
lcd.clear();
if(i) lcd.print(i);
else lcd.print("Start!..");
delay(start_T);
}
lcd.clear();
gtm = millis();
for(int i = 0; i < bullets; i++) lcd.write(0);
tm = tmr.get_t();
while(game){
if(tmr.get_t() - tm > bull_T){
tm = tmr.get_t();
bullets--;
lcd.scrollDisplayLeft();
}
if(digitalRead(6)){
game = false;
lcd.clear();
lcd.print("Easy Win in ");
lcd.setCursor(0, 1);
long tmp = millis() - gtm;
lcd.print(tmp/1000);
lcd.print(".");
lcd.print(tmp%1000);
lcd.print(" seconds!");
delay(3000);
}
if(!bullets){
while(analogRead(14) < mx){}
while(analogRead(14) > mn){}
bullets = mxb; lcd.clear();
cnt++;
if(cnt == mxc){
lcd.print("Bullets off!");
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
game = false;
break;
}
for(int i = 0; i < bullets; i++)lcd.write(0);
tm = tmr.get_t();
}
}
}
void push(){
if(bullets){ digitalWrite(12, HIGH);}
tmr.start_t();
}
void pull(){
digitalWrite(12, LOW);
tmr.stop_t();
}