-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_on_laser_tag.ino
148 lines (131 loc) · 3.39 KB
/
i2c_on_laser_tag.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//GUN 1
#include<IRremote.h>
#include <LiquidCrystal_I2C.h>
#include<EEPROM.h>
int i;
int rec = 12;
int buzzer = 2;
int rumble = 11;
int button = 4;
int flag = 0;
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
int resett = 3276; //Decimal for Reset Hex
IRsend irsend;
IRrecv IRrec(rec);
decode_results results;
int ammo_addr = 0; //addr of ammo variable
int health_addr = 1; //addr of health variable
int health = 20;
int ammo = 90;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
IRrec.enableIRIn();
pinMode(button, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
//digitalWrite(button,HIGH);
pinMode(buzzer, OUTPUT);
pinMode(rumble, OUTPUT);
digitalWrite(buzzer, LOW);
digitalWrite(rumble, LOW);
//reading previous value from EEPROM
ammo = EEPROM.read(ammo_addr);
health = EEPROM.read(health_addr);
//Overwriting previous value
EEPROM.write(ammo_addr, ammo);
EEPROM.write(health_addr, health);
}
void loop()
{
digitalWrite(LED_BUILTIN, LOW);
//reading previous value from EEPROM
// ammo = EEPROM.read(ammo_addr);
//health = EEPROM.read(health_addr);
//Sending IR signal
int a = digitalRead(button);
if (a == 0)
{
digitalWrite(rumble, HIGH);
delay(500);
if (ammo != 0)
{
Serial.print("shooting");
digitalWrite(LED_BUILTIN, HIGH);
irsend.sendNEC(0xAAAAAAAA, 32); //Send IR code AAAAAAAA
ammo--;
EEPROM.write(ammo_addr, ammo); //Saving updated ammo
}
IRrec.enableIRIn();
}
//Receiving IR signal through TSOP
else
{
delay(100);
if (IRrec.decode(&results))
{
Serial.println(results.value, HEX);
if (results.value == 0xBBBBBBBB || results.value == 0x6EF0EB64)
{ //check for infrared signal
if (health != 0)
{
Serial.println(results.value, HEX ); //print ir hex code
delay(100);
health--;
Serial.println(health);
EEPROM.write(health_addr, health); //Saving updated health
if (health == 0 ) {
lcd.setCursor(0, 1);
lcd.print("DEAD ");
digitalWrite(buzzer, HIGH);
}
}
delay(100);
}
//Resetting Ammo and Health
if (results.value == resett) {
health = 20;
ammo = 90;
digitalWrite(buzzer, LOW);
EEPROM.write(ammo_addr, ammo);
EEPROM.write(health_addr, health);
ammo = EEPROM.read(ammo_addr);
health = EEPROM.read(health_addr);
IRrec.resume();
}
IRrec.resume();
}
}
//Printing values on LCD
if (ammo > 0)
{
lcd.setCursor(0, 0);
lcd.print("AMMO:");
lcd.print(ammo);
lcd.print(" ");
}
else
{
lcd.setCursor(0, 0);
lcd.print("NO AMMO LEFT ");
if (flag == 0)
{
for (i = 0; i < 5; i++)
{
digitalWrite(buzzer, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
delay(500);
}
flag = 1;
}
}
if (health > 0)
{
lcd.setCursor(0, 1);
lcd.print("HEALTH:");
lcd.print(health);
lcd.print(" ");
}
digitalWrite(rumble, LOW);
}